मैं एक एएसपी.नेट एमवीसी प्रोजेक्ट पर काम कर रहा हूं जो बाहरी प्लगइन का समर्थन करता है, अब, मैं यूनिटी से ऑटोफैक में जा रहा हूं और मुझे ऑटोफैक की आजीवन वस्तुओं को लपेटने की ज़रूरत है ताकि प्लगइन्स को इसका संदर्भ, एकता में मैं कुछ ऐसा कर सकता था।ऑटोफैक लाइफटाइम प्रबंधन
public sealed class UnityScopeFactory : IDependencyScopeFactory
{
private HttpRequestScope _httpRequest;
private SingletonScope _singleton;
private TransientScope _transient;
public IDependencyScope HttpRequest()
{
return _httpRequest ?? (_httpRequest = new HttpRequestScope());
}
public IDependencyScope Singleton()
{
return _singleton ?? (_singleton = new SingletonScope());
}
public IDependencyScope Transient()
{
return _transient ?? (_transient = new TransientScope());
}
private class HttpRequestScope : IDependencyScope
{
public object CreateScope()
{
return new HttpPerRequestLifetimeManager();
}
}
private class SingletonScope : IDependencyScope
{
public object CreateScope()
{
return new ContainerControlledLifetimeManager();
}
}
private class TransientScope : IDependencyScope
{
public object CreateScope()
{
return new TransientLifetimeManager();
}
}
}
मैं Autofac में इसी तरह की बात की है लेकिन मुझे यकीन है कि क्या यह है कि, मैं Autofac के RegistrationBuilder जो (दुर्भाग्य से) आंतरिक है में देखा है और मैं इस के साथ आया था ऐसा करने का सही तरीका नहीं है नहीं कर रहा हूँ।
public class AutofacScopeFactory : IDependencyScopeFactory
{
private HttpRequestScope _httpRequest;
private SingletonScope _singleton;
private TransientScope _transient;
public IDependencyScope HttpRequest()
{
return _httpRequest ?? (_httpRequest = new HttpRequestScope());
}
public IDependencyScope Singleton()
{
return _singleton ?? (_singleton = new SingletonScope());
}
public IDependencyScope Transient()
{
return _transient ?? (_transient = new TransientScope());
}
private class HttpRequestScope : IDependencyScope
{
public object CreateScope()
{
return new CurrentScopeLifetime();
}
}
private class SingletonScope : IDependencyScope
{
public object CreateScope()
{
return new RootScopeLifetime();
}
}
private class TransientScope : IDependencyScope
{
public object CreateScope()
{
return new CurrentScopeLifetime();
}
}
}
इसके अलावा, के बाद मैं इस काम मिल गया, मैं कैसे ContainerBuilder को इसे पारित कर सकते हैं?
एकता में मैं ऐसा कुछ कर सकता था।
public sealed class UnityDependencyContainer : IDependencyContainer
{
private readonly IUnityContainer _container;
public UnityDependencyContainer()
{
_container = new UnityContainer()
}
public void Register<TContract, TImplementation>(IDependencyScope scope) where TImplementation : TContract
{
LifetimeManager manager = scope.CreateScope() as LifetimeManager;
if (manager != null)
{
_container.RegisterType<TContract, TImplementation>(manager);
}
}
}
मैं विधि श्रृंखला में IComponentLifetime का उदाहरण कैसे पास करूं? क्या यह एक मृत अंत है?
public class AutofacContainer : IDependencyContainer
{
private static readonly ContainerBuilder Builder;
static AutofacContainer()
{
Builder = new ContainerBuilder();
}
public void RegisterType<TContract, TImplementation>(IDependencyScope scope) where TImplementation : TContract
{
IComponentLifetime manager = scope.CreateScope() as IComponentLifetime;
if (manager != null)
{
Builder.RegisterType<TImplementation>().As<TContract>();
}
}
}
वाह! समय लेने के लिए बहुत बहुत धन्यवाद, वास्तव में सराहना की। यह वास्तव में इस चरण में बदलने के लिए पिटा है, मैं ऑटोफैक में बदलने के लिए उत्सुक था और इसे आज़माएं लेकिन अगर मैं इसका उपयोग नहीं कर सकता क्योंकि यह अलग-अलग काम करता है तो मुझे एक और कंटेनर का उपयोग करना होगा या लागत के साथ रहना होगा इसे एटीएम बदलने के लिए बहुत अच्छा है। –
अच्छी व्याख्या +1! –