2012-10-16 242 views
3

我想添加一個類型的多個實例使用StructureMap, NHibernate and multiple databases爲我設計的來源結構映射StructureMap添加多個實例不工作

這是我如何加入他們在我註冊地:

foreach (ConnectionStringSettings location in ConfigurationManager.ConnectionStrings) 
     { 
      For<INHibernateSessionFactory>() 
       .Add(x => new NHibernateSessionFactory(location.Name, GetSessionFactory(location.ConnectionString))); 
     } 

然後在我的AppSessionFactory:

public AppSessionFactory(IEnumerable<NHibernateSessionFactory> factories) 
    { 
     _factories = new List<INHibernateSessionFactory>(factories); 
    } 

無論這樣或那樣的工作:

_factories = ObjectFactory.GetAllInstances<INHibernateSessionFactory>().ToList(); 

這兩個調用都返回兩個相同的對象,相同的位置和相同的SessionFactory。

回答

0

我不完全確定,但這可能與您訪問修改後的封閉的方式有關。

我會嘗試創建變量的局部範圍的複製像這樣:

 
foreach (ConnectionStringSettings location in ConfigurationManager.ConnectionStrings) 
{ 
    ConnectionStringSettings localLocation = location; 
    For<INHibernateSessionFactory>() 
     .Add(x => new NHibernateSessionFactory(localLocation.Name, GetSessionFactory(localLocation.ConnectionString))); 
}