외부 플러그인을 지원하는 ASP.NET MVC 프로젝트에서 작업 중입니다. 이제 Unity에서 Autofac으로 이동하고 플러그인의 평생 객체를 래핑해야합니다. 그것을 참조, Unity에서 나는 이것을 할 수 있습니다.Autofac 평생 관리
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에 전달할 수 있습니까?
Unity에서 나는 이와 같이 할 수 있습니다.
어떻게 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>();
}
}
}
와우! 시간을내어 주셔서 대단히 감사합니다. 정말로 감사드립니다. 정말 PITA가이 단계에서 바뀌 었습니다. 저는 Autofac으로 바꾸고 그것을 시도하고 싶습니다.하지만 다르게 작동하기 때문에 사용할 수 없다면 다른 컨테이너를 사용해야하거나 비용이 그대로 유지되어야합니다. 그것을 atm 변경하기에는 너무 좋습니다. –
좋은 설명 +1! –