外部プラグインをサポートするASP.NET MVCプロジェクトに取り組んでいますが、今はUnityからAutofacに移行しています.Autofacのライフタイムオブジェクトをラップする必要があります。それを参照して、私はUnityで何かこれをすることができました。Autofac Lifetime Management
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! –