Short Answer
Complete Explanation
The phrase “closed to MVC” describes a technical limitation in the ASP.NET MVC framework: the controller factory can only discover and create instances of generic controller classes when the generic type parameters have been supplied with concrete types. Such a controller is called a closed generic controller. An open generic controller, which still contains unspecified type parameters, is not eligible for automatic routing and will be ignored by MVC during the controller discovery process.
- Definition:
A closed generic controller is a class that inherits from a generic base controller with all type arguments specified, e.g.,public class ProductController : GenericController<Product>. - Framework Support:
ASP.NET MVC’s controller factory scans assemblies for types that implementIController. It only registers types that are concrete (non‑abstract) and closed, ensuring the framework can instantiate them without further type resolution. - Typical Use Cases:
Developers use closed generic controllers to share common CRUD logic across multiple entity types while preserving type safety and eliminating duplicated code. - Limitation:
Open generic controllers, such aspublic class GenericController<TEntity> : Controller, are not automatically routed. They must be manually instantiated or wrapped in a closed subclass for MVC to handle requests. - Example:
public class OrderController : GenericController<Order> { }is a closed controller that MVC can map to routes like/Order/Index.
Common Misconceptions
“Closed to MVC” means a controller cannot be edited.
It refers only to the generic type being fully specified, not to editability of the controller code.
All generic controllers work out of the box in MVC.
Only those that are closed (concrete) are recognized; open generic controllers require additional configuration.
FAQ
Can I use an open generic controller without creating a closed subclass?
Not directly with the default MVC controller factory. You would need to implement a custom controller factory or use a dependency injection container that can resolve open generic types at runtime.
Does "closed to MVC" affect unit testing of generic controllers?
No. Unit tests can instantiate either open or closed generic controllers directly. The restriction applies only to the MVC runtime's automatic discovery and routing.
Is the "closed to MVC" rule specific to ASP.NET MVC only?
The rule is specific to the ASP.NET MVC controller discovery mechanism. Other frameworks may have different conventions, but many also require concrete types for automatic controller registration.
Leave a Reply