epitka
epitka

Reputation: 17637

StructureMap: CacheBy(InstanceScope.Singleton) question

I have a question about how InstanceScope.Singleton works, because I am getting some unexpected results here:

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.

Upvotes: 1

Views: 582

Answers (1)

Chris Marisic
Chris Marisic

Reputation: 33098

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.

Upvotes: 1

Related Questions