Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Even if you don't use a distinct service layer, you can accomplish what you want, which is to decouple the MVC application from the DAL project using DI.</p> <p>The way to do this is to add a couple of projects/assemblies in between that wires up your IoC container with specific instances of the interfaces you have defined.</p> <p>I typically use this naming convention: </p> <ul> <li><p>MyCompany.MyProject.Infrastructure </p></li> <li><p>MyCompany.MyProject.Abstract</p></li> </ul> <p>Your main MVC project would then have a reference to your Abstract and Infrastructure projects. Your Infrastructure project would have a reference to the Abstract and instance specific projects like the Business and DAL projects. Within Infrastructure project you wire up the dependencies.</p> <p>You'll have to setup a mechanism for your MVC project to bootstrap your IoC in the Infrastructure assembly. You can do that in your global.asax or as an App_Start method and call a Registration class within your Infrastructure assembly.</p> <p>We use StructureMap, but the concept is the same. Here's some sample code.</p> <p>In your MVC App, create a App_Start method to setup the DI.</p> <pre><code>public static class StructuremapMvc { public static void Start() { // Create new Structuremap Controller factory so Structure map can resolve the parameter dependencies. ControllerBuilder.Current.SetControllerFactory(new StructuremapControllerFactory()); IContainer container = IoC.Initialize(); DependencyResolver.SetResolver(new StructureMapDependencyResolver(container)); GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container); } } </code></pre> <p>In your Infrastructure assembly, wire up the dependencies.</p> <pre><code>public static class IoC { public static IContainer Initialize() { ObjectFactory.Initialize(x =&gt; { x.Scan(scan =&gt; { scan.TheCallingAssembly(); scan.WithDefaultConventions(); }); x.For&lt;IRepositoryNum1&gt;().Use&lt;Num1Repository&gt;(); x.For&lt;IRepositoryNum2&gt;().Use&lt;Num2Repository&gt;(); x.For&lt;IRepositoryNum3&gt;().Use&lt;Num3Repository&gt;(); }); return ObjectFactory.Container; } } </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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