Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Dependencies should be resolved in cascade down from the top layer classes, not using static or singleton container in constructors.</p> <p>Check this <a href="http://www.devtrends.co.uk/blog/how-not-to-do-dependency-injection-the-static-or-singleton-container" rel="nofollow">link</a>.</p> <p>If your app does not provide any hook into the pipeline to resolve the dependencies (as <code>Unity.Mvc3</code> does with controllers) the best aproach is resolve the <code>Manager</code> (your top layer class) class through Unity (you must register it first) and let the DI container inject <code>IRepository</code>. This alows you to keep the static/singleton container in one place and keep things under control. No other layer in your app should know anything about DI container.</p> <p>An example of resolving complete dependency graph could be the next piece of code:</p> <pre><code>Public Class ClasificationManagement //application layer service, top layer class Implements IClasificationManagement Private _servicio As IClasificacionesService //inject domain serice for bussines Private _repositorio As IClasificationRepository //inject repository for perisitence Public Sub New(ByVal servicio As IClasificacionesService, ByVal repositorio As IClasificationRepository) _servicio = servicio _repositorio = repositorio End Sub Public sub SwapDescrition(ByVal clasificationOrigenID As String, ByVal clasificationDestinoID As String) Implements IClasificationManagement. SwapDescrition //code using domain services and repositories End sub Public class ClasificacionesService implements IClasificacionesService private _tracer as ITracer //inject tracer to service domain public sub new(tracer as ITracer) _tracer = tracer end sub //not relevant code using ITracer End Class </code></pre> <p>With the prorper configuration of Unity in XML or runtime I just need to resolve ClasificationManagement and Unity do the dirty work of resolving all dependecy chain.</p> <pre><code>//resolve manager manager = ServiceLocator.Current.GetInstance(Of IClasificationManager)() //use manager, all dependencies (bussines service, repositories, tracers) were injected by Unity manager.SwapDescrition("123-ABC", "456-DEF") </code></pre> <p>For Unity config I like xml configuration because you can change the dependencies and no compilantion is needed. Just include this function somewhere in the initialization logic of your app.</p> <pre><code>Imports Microsoft.Practices.Unity Imports Microsoft.Practices.Unity.Configuration Imports Microsoft.Practices.ServiceLocation Private Sub InitServiceLocator() Dim container As IUnityContainer = New UnityContainer() container.LoadConfiguration() //read xml config //container.RegisterType&lt;Of ...&gt;(...) //config in runtime Dim provider = New UnityServiceLocator(container) ServiceLocator.SetLocatorProvider(Function() provider) End Sub </code></pre> <p>A XML config should looks like:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;unity xmlns="http://schemas.microsoft.com/practices/2010/unity"&gt; &lt;alias alias="IClasificationManagement" type="AppPrincipal.IClasificationManagement, AppPrincipal" /&gt; &lt;alias alias="IClasificationRepository" type="X509ValDAL.IClasificationRepository, X509ValDAL" /&gt; &lt;alias alias="IClasificacionesService" type="Entidades.IClasificacionesService, Entidades" /&gt; &lt;alias alias="IUnitOfWork" type="X509ValDAL.IUnitOfWork, X509ValDAL" /&gt; &lt;alias alias="ObjectContext" type="System.Data.Objects.ObjectContext, System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /&gt; &lt;alias alias="ClasificationManagement" type="AppPrincipal.ClasificationManagement, AppPrincipal" /&gt; &lt;alias alias="ClasificationRepository" type="X509ValDAL.ClasificationRepository, X509ValDAL" /&gt; &lt;alias alias="ClasificacionesService" type="Entidades.ClasificacionesService, Entidades" /&gt; &lt;alias alias="UnitOfWork" type="X509ValDAL.UnitOfWork, X509ValDAL" /&gt; &lt;alias alias="X509VALEntities" type="Entidades.X509VALEntities, Entidades" /&gt; &lt;assembly name="AppPrincipal" /&gt; &lt;assembly name="X509ValDAL" /&gt; &lt;assembly name="Entidades" /&gt; &lt;assembly name="System.Data.Entity" /&gt; &lt;namespace name="AppPrincipal" /&gt; &lt;namespace name="X509ValDAL" /&gt; &lt;namespace name="Entidades" /&gt; &lt;namespace name="System.Data.Objects" /&gt; &lt;container&gt; &lt;register type="IClasificationManagement" mapTo="ClasificationManagement"&gt; &lt;constructor&gt; &lt;param name="servicio"&gt; &lt;dependency/&gt; &lt;/param&gt; &lt;param name="repositorio"&gt; &lt;dependency/&gt; &lt;/param&gt; &lt;/constructor&gt; &lt;/register&gt; &lt;register type="IClasificationRepository" mapTo="ClasificationRepository"&gt; &lt;constructor&gt; &lt;param name="uow"&gt; &lt;dependency/&gt; &lt;/param&gt; &lt;/constructor&gt; &lt;/register&gt; &lt;register type="IClasificacionesService" mapTo="ClasificacionesService" /&gt; TODO: Inject ITracer &lt;register type="ObjectContext" mapTo="X509VALEntities" /&gt; &lt;register type="IUnitOfWork" mapTo="UnitOfWork" &gt; &lt;constructor&gt; &lt;param name="context"&gt; &lt;dependency /&gt; &lt;/param&gt; &lt;/constructor&gt; &lt;/register&gt; &lt;/container&gt; &lt;/unity&gt; </code></pre> <p>I hope this example hepls you to understand the phylosophy of DI. Even when I use a static container (ServiceLocator); the container is used just to load top level layer classes to avoid servicelocator antipattern.</p>
    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