views:

185

answers:

1

Hi, 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.

+1  A: 

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.

Chris Marisic
Thanks Chris, I know that that is absolute no no with ISession. I guess I did not realize (did not think clearly) that that If make Dao singleton the reference to ISession will never get refreshed.
epitka
Also, I was not actually making Dao singleton, but Service, but it is the same thing, just one level higher.
epitka