我有两个接口说明服务必须是单例的还是瞬态的:
public interface ITransient {}
public interface ISingleton {}
我在其他接口和类中实现了这个接口:
public interface ISession : ISingleton
{
int? UserId {get;set;}
}
public class Session : ISession
{
public int? UserId {get;set;}
}
然后我在其他服务中注入 Session:
public interface IBookService : ITransient
{
...
}
public class BookService : IBookService
{
public BookService(ISession session) { ... }
...
}
如何配置 StructureMap 以使实现 ISingleton 类型的所有实例请求都必须使用 Singleton 生命周期创建?
我试过了:
Container.Configure(conf => {
conf.For<ITransient>().Transient();
conf.For<ISingleton>().Singleton();
}
但是什么都没有......不工作,创建一个会话对象作为瞬态。
我也试过了:
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)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
...
===============================================================================================================================================================================================================================================================================
我能解决这个吗?