I have two interfaces that says if a service have to be singleton or transient:
public interface ITransient {}
public interface ISingleton {}
I implement this interfaces in other interfaces and classes:
public interface ISession : ISingleton
{
int? UserId {get;set;}
}
public class Session : ISession
{
public int? UserId {get;set;}
}
Then I inject Session in others services:
public interface IBookService : ITransient
{
...
}
public class BookService : IBookService
{
public BookService(ISession session) { ... }
...
}
How to configure StructureMap to make that all instance requests of types that implements ISingleton have to create with Singleton lifecycle??
I have tried it:
Container.Configure(conf => {
conf.For<ITransient>().Transient();
conf.For<ISingleton>().Singleton();
}
But nothing ... don't work, create a Session object as Transient.
I have tried it too:
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();
});
And nothing ...
I have seen how to do it using Windsor Castle:
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()
But I don't know how to do using StructureMap ...
Other posibility is using conventions (IRegistrationConvention), but I don't know how to do, example:
public class LifecycleConvention : IRegistrationConvention
{
public void Process(Type type, Registry registry)
{
if (type.GetInterface(typeOf(ISingleton) != null)
**???? what to do ??!!**
}
}
Somebody can help me please?
UPDATE
I have build a convention:
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());
}
}
}
And that seem work but it register each class as plugin type, in this case:
Session => Session [Singleton] BookService => BookService [Transient]
But if I inject the Session as ISession ... don't found the instance due to ISession is not registered ... But I can use default convetions ... and then work BUT retrieve the instance as transient ...
Calling WhatDoIHave() I can see it:
===============================================================================================================================================================================================================================================================================
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)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
...
===============================================================================================================================================================================================================================================================================
Can I solve this it?
BookService
depend onSession
instead ofISession
. Is that an error? I think that is an accidental Short Circuited Dependency. – Steven