Note that there are some explanatory texts on larger screens.

plurals
  1. POMy MVC Custom ControllerFactory works but could it be better?
    primarykey
    data
    text
    <p>I've looked at Ninject, StructureMap and Other Dependency Injection and Service Locator frameworks, but this question is more about learning how it works and what can be better. More to the point, I’m not interesting in looking at a Framework’s source code for Dependency Injection, but understanding how it’s achieved from beginning to end in practice/code.</p> <p>The code below is for a small internal project, so with that in mind let me begin.</p> <p>Here is my interface for returning Domain Models to my controllers. I've decided that due to the size of the project (small), a single interface for Controllers was acceptable.</p> <pre><code>interface IModelFactory { IEnumerable&lt;User&gt; GetUsers(); User GetUser(Guid UserID); bool Add(User User); bool Delete(User User); bool Update(User User); IEnumerable&lt;Car&gt; GetCars(); Car GetCar(Guid CarID); bool Add(Car Car); bool Delete(Car Car); bool Update(Car Car); } </code></pre> <p>Each controller has inherits from <code>DIBaseController</code> so I didn't have to create private members for every controller.</p> <pre><code>public abstract class DIBaseController : Controller { protected IModelFactory ModelFactory { get; set; } public DIBaseController(IModelFactory ModelFactory) { this.ModelFactory = ModelFactory; } } public class HomeController : DIBaseController { public HomeController (IModelFactory ModelFactory) : base(ModelFactory) { } } </code></pre> <p>Created my own Controller Factory that allows me to inject my ModelFactory into Controllers.</p> <pre><code>internal class DIControllerFactory : DefaultControllerFactory { private IModelFactory _ModelFactory; internal DIControllerFactory(IModelFactory ModelFactory) { this._ModelFactory = ModelFactory; } public override IController CreateController(RequestContext requestContext, string controllerName) { IController result = null; string thisnamespace = this.GetType().Namespace; //This could be improved I bet. Type controller = Type.GetType(thisnamespace.Substring(0, thisnamespace.IndexOf('.')) + ".Controllers." + controllerName + "Controller"); if (controller != null &amp;&amp; controller.IsSubclassOf(typeof(DIBaseController))) { result = (IController)Activator.CreateInstance(controller, new object[] { this._ModelFactory }); } else { result = base.CreateController(requestContext, controllerName); } return result; } } </code></pre> <p>And finally added the code to Inject the Concreate class into the Factory to inject into created Controllers.</p> <pre><code> protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); ControllerBuilder.Current.SetControllerFactory(new DIControllerFactory(new LinqSqlModelFactory())); } </code></pre> <p>The only area I haven't explored (and I don't think I'm interested in at the moment) is to create a <code>web.config</code> section to dynamically create the <code>ModelFactory</code>. This works, but I'm interested if I've completely missed the boat, come close, or if I'm spot on?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload