2011-02-25 109 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