2011-02-25 108 views
2

我開始使用最新的s#arp架構(1.9.5.0)。尖銳的架構問題

Repository<OriginalSequence> SequenceRepository = new Repository<OriginalSequence>(); 
... 
SequenceRepository.SaveOrUpdate(Sequence); 
SequenceRepository.DbContext.CommitChanges(); 

NHibernateSession.Current.Flush(); 
NHibernateSession.Current.Clear(); 

不幸的是,我得到:我有時也會出現一些數據通過一個控制檯應用程序中使用這樣的事情添加到數據庫

服務定位器未初始化;我試圖檢索SharpArch.Data.NHibernate.ISessionFactoryKeyProvider

這和我想的DI有關。通常只有當我在網絡應用程序中使用它或者根本改變了某些東西時才需要它?謝謝。

基督教

PS:

請注意,我開始了與:

string[] mappingAssemblies = new string[] { "Bla.Data" }; 
       string configFile = "NHibernate.config"; 
       NHibernate.Cfg.Configuration config = NHibernateSession.Init(
         new SimpleSessionStorage(), 
         mappingAssemblies, 
         new AutoPersistenceModelGenerator().Generate(), 
         configFile); 

過去,這很好地工作。

回答

1

這已在1.9.6版本中得到修復,但您需要在Windsor容器中註冊ISessionFactoryKeyProvider的默認實現。

在「網絡」項目的CastleWindsor文件夾ComponentRegistrar.cs這個代碼添加到AddGenericRepositoriesTo方法:

container.Register(
    Component 
    .For(typeof(ISessionFactoryKeyProvider)) 
    .ImplementedBy(typeof(DefaultSessionFactoryKeyProvider)) 
    .Named("sessionFactoryKeyProvider")); 
+0

您好,感謝我看到了一個類似的帖子在其他地方。一切都已經在那裏。請注意,我在控制檯應用程序中使用過的東西很好。我不認爲這個東西會被調用... – cs0815 2011-02-25 16:26:44

+0

這是1.9.5中引入的一個變化,現在需要在Windsor容器中註冊DefaultSessionFactoryKeyProvider以使其工作。無論如何,你應該這樣做其他組件,例如IEntityDuplicateChecker,IValidator。 – ChrisR 2011-02-25 16:43:44

+1

在控制檯應用程序中,您必須明確地調用 'ComponentRegistrar.AddComponentsTo'並傳入一個Wi​​ndsor容器中,您還應該使用ServiceLocator.SetLocatorProvider(()=> new WindsorServiceLocator(container))初始化ServiceLocator; ' 在一個網絡應用程序中,這是在MvcApplication.Application_Start的 Global.asax.cs中完成的,但您必須將其添加到您的控制檯應用程序的啓動代碼 。 – ChrisR 2011-02-25 16:58:31