InstanceScope.Singleton question

14 views
Skip to first unread message

epitka

unread,
Dec 24, 2009, 11:17:39 AM12/24/09
to structuremap-users
I have this posted on the stackoverflow, but maybe I'll have question
answered faster here.

I am getting some unexpected results when using
InstanceScope.Singleton :

I have a service that is dependent on Dao

public MetaProjectService(IMetaProjectDao metaProjectDao)
{
_metaProjectDao = metaProjectDao;

}
and I have Dao that is dependent on ISession (Nhibernate)

public MetaProjectDao(ISession nhSession)
: base(nhSession)
{}
and for structure map I have following:

ForRequestedType<IMetaProjectService>()
.TheDefaultIsConcreteType<MetaProjectService>();
//.CacheBy(InstanceScope.Singleton);


ForRequestedType<IMetaProjectDao>()
//.TheDefaultIsConcreteType<MetaProjectDao>()
//.CacheBy(InstanceScope.Singleton)
.TheDefault
.Is.ConstructedBy(() =>
{
var session = ObjectFactory
.GetNamedInstance<ISessionFactory>(
MetaProjectDao.SESSION_FACTORY_NAME)
.GetCurrentSession();
return new MetaProjectDao(session);
});
Question: If I uncomment definition for IMetaProjectService to be
cached as InstanceScope.Singleton it will not work on subsequent
requests as it will still hold reference to the IMetaProject instance
build on the first request. There are also other dependencies that I
omitted for clarity. So the question is how do I build IMetaProjectDao
on each request and have this new instance be referenced by
IMetaProjectService while having IMetaProjectService as singleton.

Ryan Heath

unread,
Dec 25, 2009, 8:19:47 PM12/25/09
to structure...@googlegroups.com
I do not know the exact answer to your question but I think you could
achieve it via property injection.
However, I strongly object against having a singleton holding
reference to non-shareble state, your nhsession. More sooner than
later it will bite you.
What are the reasons to have a singleton service? If it is just to
save some allocation cycles (premature optimizations) then let go the
singleton, it is not worth headache.
If the service actually is expensive to create then try to split that
very part into another class which you register as a singleton. The
nonsingleton service should reference that class and the dao.

HTH
// Ryan

Chris Marisic

unread,
Dec 26, 2009, 12:19:08 AM12/26/09
to structuremap-users
From reading your code and understanding NHibernate everything is
working exactly as expected.

var session = ObjectFactory
.GetNamedInstance<ISessionFactory>(
MetaProjectDao.SESSION_FACTORY_NAME)
.GetCurrentSession();

Will only ever get called once if you cache IMetaProjectDao as a
singleton, which is absolutely what you DO NOT WANT. A singleton
instance of ISession from NHibernate is the biggest anti-pattern you
can implement (unless this is WinForms which I doubt from your usage
of GetCurrentSession()). You should cache your Dao either in Hybrid or
HybridSession depending whether you're implementing session per
request or long sessions.

Reply all
Reply to author
Forward
0 new messages