2013-05-10 86 views
0

有沒有人有一個使用非流暢NHibernate(NHibernate中的XML映射)與Castle Windsor和代碼作爲配置(Castle Windsor沒有XML)的PersistenceFacility的例子? (ASP.NET MVC)Castle Windsor,Non-Fluent NHibernate和PersistenceFacility

貫穿本教程,他們使用流利NHibernate,而我不能使用流利(我將通過* .hbm.XML配置NHibernate類)。

教程> http://docs.castleproject.org/Windsor.Windsor-Tutorial-Part-Six-Persistence-Layer.ashx

與流利具體例>https://github.com/kkozmic/ToBeSeen/blob/master/src/ToBeSeen/Plumbing/PersistenceFacility.cs

public class PersistenceFacility : AbstractFacility 
{ 
protected virtual void ConfigurePersistence(Configuration config) 
{ 
    SchemaMetadataUpdater.QuoteTableAndColumns(config); 
} 

protected virtual AutoPersistenceModel CreateMappingModel() 
{ 
    var m = AutoMap.Assembly(typeof(EntityBase).Assembly) 
     .Where(IsDomainEntity) 
     .OverrideAll(ShouldIgnoreProperty) 
     .IgnoreBase<EntityBase>(); 

    return m; 
} 

protected override void Init() 
{ 
    var config = BuildDatabaseConfiguration(); 

    Kernel.Register(
     Component.For<ISessionFactory>() 
     .UsingFactoryMethod(_ => config.BuildSessionFactory()), 
     Component.For<ISession>() 
     .UsingFactoryMethod(k => k.Resolve<ISessionFactory>().OpenSession()) 
     .LifestylePerWebRequest() 
    ); 
} 

protected virtual bool IsDomainEntity(Type t) 
{ 
    return typeof(EntityBase).IsAssignableFrom(t); 
} 

protected virtual IPersistenceConfigurer SetupDatabase() 
{ 
    return MsSqlConfiguration.MsSql2008 
     .UseOuterJoin() 
     .ConnectionString(x => x.FromConnectionStringWithKey("ApplicationServices")) 
     .ShowSql(); 
} 

private Configuration BuildDatabaseConfiguration() 
{ 
    return Fluently.Configure() 
     .Database(SetupDatabase) 
     .Mappings(m => m.AutoMappings.Add(CreateMappingModel())) 
     .ExposeConfiguration(ConfigurePersistence) 
     .BuildConfiguration(); 
} 

private void ShouldIgnoreProperty(IPropertyIgnorer property) 
{ 
    property.IgnoreProperties(p => p.MemberInfo.HasAttribute<DoNotMapAttribute>()); 
} 
} 

我需要的是PersistenceFacility配置設置的NHibernate不流利。如果有人可以演示代碼來爲不流暢的NHibernate設置NHibernate等,或者將我指向一個很棒的示例/教程/博客!

回答

0

它看起來像這樣工作。我仍然沒有嘗試從數據庫中保存或檢索數據,但它是無錯初始化控制器等。其餘代碼就像在教程http://docs.castleproject.org/Windsor.Windsor-tutorial-part-one-getting-Windsor.ashx中一樣。

NHibernate配置位於配置文件(web.config或nhibernate.cfg.xml)中。

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
    <session-factory> 
     ... 
    </session-factory> 
</hibernate-configuration> 

然後PersistenceFacility可以簡單地這樣。

public class PersistenceFacility : AbstractFacility 
{ 

    protected override void Init() 
    { 
     // NHibernate configures from the <hibernate-configuration> section of a config file 
     var config = new Configuration().Configure(); 

     Kernel.Register(
      Castle.MicroKernel.Registration.Component.For<ISessionFactory>() 
       .UsingFactoryMethod(_ => config.BuildSessionFactory()), 
      Castle.MicroKernel.Registration.Component.For<ISession>() 
       .UsingFactoryMethod(k => k.Resolve<ISessionFactory>().OpenSession()) 
       .LifestylePerWebRequest() 
     ); 
    } 
} 

教程之間的區別是沒有必要的良好的配置和使用,而不是NHibernates Configuration().Configure()作品。