You could use that as a base class for another controller, but not as a controller itself.
Unbound generic types like that are a bit like abstract classes in the sense that you can't use them until you provide more info (in this case, it needs to know what TModel is going to be). MVC cannot "guess" what your TModel is going to be, but you could
explicitly create controllers for specific model types:
public class MyModelController : ReflectiveController<MyModel> { }
Marked as answer by ricka6 on Jan 14, 2010 06:28 PM
Sounds great. I'll try this if I can reach what I want to achive.
What I want to achieve in short is: I have built now say 10 Business Objects. Most Actions are repetetive (like Create, Edit, Delete, Detail, ....) I want to standardize them.
One option is that each Controller implements a certain interface
Then I have to do some generic processing within these controller implementations in order to fill a generic view pattern.
The other possibility is the one you just suggested. So I would have a generic controller implementation for the generic stuff and specific actions for the specific controller(s) which inherits the generic part from the generic controller. This seems quite
better in my eyes. I have to see how this works for the views.
I would definitely look into creating some sort of abstract controller base class that your controllers inherit from. That way, you could at least implement some of the shared functionality through the abstract class. I do something similar to log actions
called and parameters passed.
~Pharcyde
.NET Developer and MVC Advocate
Marked as answer by ricka6 on Jan 14, 2010 06:28 PM
FMK
Member
6 Points
55 Posts
Is it possible by any means to use generic controllers?
Sep 28, 2009 04:16 PM|LINK
Is it possible to use generic controllers?
public class ReflectiveController<TModel> : Controller {I can declare it. However the remaining question is how to instantiate it or call it.
Because normally the Controller is called by the MVC Framework. And I do not know how to instruct the framework to do this. If it is even possible.
pharcyde
Member
538 Points
109 Posts
Re: Is it possible by any means to use generic controllers?
Sep 28, 2009 05:54 PM|LINK
I don't think what you are doing is going to work. What problem are you trying to solve here?
.NET Developer and MVC Advocate
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: Is it possible by any means to use generic controllers?
Sep 28, 2009 07:34 PM|LINK
You could use that as a base class for another controller, but not as a controller itself.
Unbound generic types like that are a bit like abstract classes in the sense that you can't use them until you provide more info (in this case, it needs to know what TModel is going to be). MVC cannot "guess" what your TModel is going to be, but you could explicitly create controllers for specific model types:
public class MyModelController : ReflectiveController<MyModel> { }FMK
Member
6 Points
55 Posts
Re: Is it possible by any means to use generic controllers?
Sep 29, 2009 11:27 AM|LINK
Sounds great. I'll try this if I can reach what I want to achive.
What I want to achieve in short is: I have built now say 10 Business Objects. Most Actions are repetetive (like Create, Edit, Delete, Detail, ....) I want to standardize them.
One option is that each Controller implements a certain interface
Something like this:
public interface IPatternController { ActionResult Index(); ActionResult Create(String super); ActionResult Detail(String id); ActionResult Edit(String id); ActionResult Delete(String id); }Then I have to do some generic processing within these controller implementations in order to fill a generic view pattern.
The other possibility is the one you just suggested. So I would have a generic controller implementation for the generic stuff and specific actions for the specific controller(s) which inherits the generic part from the generic controller. This seems quite better in my eyes. I have to see how this works for the views.
pharcyde
Member
538 Points
109 Posts
Re: Is it possible by any means to use generic controllers?
Sep 29, 2009 12:18 PM|LINK
I would definitely look into creating some sort of abstract controller base class that your controllers inherit from. That way, you could at least implement some of the shared functionality through the abstract class. I do something similar to log actions called and parameters passed.
.NET Developer and MVC Advocate
FMK
Member
6 Points
55 Posts
Re: Is it possible by any means to use generic controllers?
Jan 14, 2010 11:01 AM|LINK
Good idea. That worked. Thanks.