2010-08-06 12 views
10

मैं Autofac के साथ काफी परिचित हूँ और एक विशेषता यह है कि मैं वास्तव में Autofac के बारे में प्यार मॉड्यूल के पंजीकरण है। क्या कोई जानता है कि मैं इसे एकता के साथ कैसे कर सकता हूं? मुझे मुश्किल समय मिल रहा है कि Google में कौन सी शर्तों का उपयोग एकता के साथ आने के लिए किया जाता है यदि कोई है।क्या मैं अपने प्रकार को यूनिटी में मॉड्यूल में पंजीकृत कर सकता हूं जैसे कि मैं ऑटोफैक में कर सकता हूं?

 

public class Global : HttpApplication, IContainerProviderAccessor 
{ 
    private static IContainerProvider _containerProvider; 

    protected void Application_Start(object sender, EventArgs e) 
    { 
     var builder = new ContainerBuilder(); 
     builder.RegisterModule(new MyWebModule()); 

     _containerProvider = new ContainerProvider(builder.Build()); 
    } 

[...] 

    public IContainerProvider ContainerProvider 
    { 
     get { return _containerProvider; } 
    } 
} 

public class MyWebModule: Module 
{ 
    protected override void Load(ContainerBuilder builder) 
    { 
     builder.RegisterModule(new ApplicationModule()); 
     builder.RegisterModule(new DomainModule()); 
    } 
} 

public class ApplicationModule: Module 
{ 
    protected override void Load(ContainerBuilder builder) 
    { 
     builder.Register(c => new ProductPresenter(c.Resolve<IProductView>())) 
       .As<ProductPresenter>() 
       .ContainerScoped(); 
    } 
} 
 

उत्तर

3

आप नहीं कर सकते। बस ऑटोफैक या विंडसर का उपयोग करें। आपको बहुत कुछ मिलेगा एकता में लापता है और अप्रत्याशित तरीकों से क्या काम करता है। यह सिर्फ आपके समय के लायक नहीं है।

+0

मैं जितना अधिक धन्यवाद कर रहा था, धन्यवाद। – Adam

+5

यूनिटी में क्या विशेष रूप से गायब है? ऐसा क्या करता है जो अप्रत्याशित है? –

32

असल में, आप एकता कंटेनर एक्सटेंशन के साथ तुच्छता से कर सकते हैं।

public class Global : HttpApplication, IContainerProviderAccessor 
{ 
    private static IContainerProvider _containerProvider; 

    protected void Application_Start(object sender, EventArgs e) 
    { 
     var container = new UnityContainer(); 
     container.AddNewExtension<MyWebModule>(); 

     _containerProvider = new ContainerProvider(container); 
    } 

[...] 

    public IContainerProvider ContainerProvider 
    { 
     get { return _containerProvider; } 
    } 
} 

public class MyWebModule : UnityContainerExtension 
{ 
    protected override void Initialize() 
    { 
     Container.AddNewExtension<ApplicationModule>(); 
     Container.AddNewExtension<DomainModule>(); 
    } 
} 

public class ApplicationModule: UnityContainerExtension 
{ 
    protected override void Initialize() 
    { 
     Container.RegisterType<ProductPrensenter>(
      new ContainerControlledLifetimeManager(), 
      new InjectionFactory(c => new ProductPresenter(c.Resolve<IProductView>()))); 
    } 
} 
संबंधित मुद्दे

 संबंधित मुद्दे