サービスがシングルトンまたは一時的である必要があるかどうかを示す2つのインターフェースがあります。
public interface ITransient {}
public interface ISingleton {}
このインターフェイスを他のインターフェイスとクラスに実装します。
public interface ISession : ISingleton
{
int? UserId {get;set;}
}
public class Session : ISession
{
public int? UserId {get;set;}
}
次に、他のサービスにセッションを挿入します。
public interface IBookService : ITransient
{
...
}
public class BookService : IBookService
{
public BookService(ISession session) { ... }
...
}
ISingleton を実装する型のすべてのインスタンス リクエストを Singleton ライフサイクルで作成する必要があるように StructureMap を構成する方法は??
私はそれを試しました:
Container.Configure(conf => {
conf.For<ITransient>().Transient();
conf.For<ISingleton>().Singleton();
}
しかし、何も...動作しません。SessionオブジェクトをTransientとして作成してください。
私も試してみました:
Container.Configure(conf =>
{
conf.Scan(s =>
{
s.Assembly(assembly);
s.LookForRegistries();
s.AddAllTypesOf<ISingletonDependency>();
s.AddAllTypesOf<ITransientDependency>();
});
conf.For<ITransientDependency>().Transient();
conf.For<ISingletonDependency>().Singleton();
});
そして何も...
ウィンザー城を使用してそれを行う方法を見てきました:
context.IocContainer.Register(
Classes.FromAssembly(context.Assembly)
.IncludeNonPublicTypes()
.BasedOn<ITransient>()
.WithService.Self()
.WithService.DefaultInterfaces()
.LifestyleTransient()
);
//Singleton
context.IocContainer.Register(
Classes.FromAssembly(context.Assembly)
.IncludeNonPublicTypes()
.BasedOn<ISingleton>()
.WithService.Self()
.WithService.DefaultInterfaces()
.LifestyleSingleton()
しかし、StructureMap を使用する方法がわかりません...
他の可能性は、規則 (IRegistrationConvention) を使用することですが、その方法がわかりません。例:
public class LifecycleConvention : IRegistrationConvention
{
public void Process(Type type, Registry registry)
{
if (type.GetInterface(typeOf(ISingleton) != null)
**???? what to do ??!!**
}
}
誰か助けてください。
アップデート
私はコンベンションを構築しました:
public class BasicConvention : IRegistrationConvention
{
public void Process(Type type, Registry registry)
{
if (!type.IsAbstract && typeof(ISingleton).IsAssignableFrom(type))
{
registry.For(type, new SingletonLifecycle());
}
if (!type.IsAbstract && typeof(ITransient).IsAssignableFrom(type))
{
registry.For(type, new TransientLifecycle());
}
}
}
それは機能しているように見えますが、各クラスをプラグインタイプとして登録します。この場合:
セッション => セッション [シングルトン] BookService => BookService [一時的]
しかし、SessionをISessionとして注入すると... ISessionが登録されていないためインスタンスが見つかりません...しかし、デフォルトの変換を使用できます...そして、動作しますが、インスタンスを一時的なものとして取得します...
WhatDoIHave() を呼び出すと、次のように表示されます。
===============================================================================================================================================================================================================================================================================
PluginType Namespace Lifecycle Description Name
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
....
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ISession Paf.Application.Session Transient Paf.Application.Session ('Paf.Application.Session, Paf.Modules.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null') Paf.Application.Session,... (Default)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
.....
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Session Paf.Application Singleton Paf.Application.Session (Default)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
...
===============================================================================================================================================================================================================================================================================
私はこれを解決できますか?