2013-05-10 107 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()作品。