【问题标题】:Castle Windsor, Non-Fluent NHibernate, and a PersistenceFacilityCastle Windsor、非流利的 NHibernate 和 PersistenceFacility
【发布时间】:2013-05-05 13:35:14
【问题描述】:

有没有人使用非流利的 NHibernate(NHibernate 中的 xml 映射)与 Castle Windsor 和代码作为配置(没有用于 Castle Windsor 的 XML)的 PersistenceFacility 示例? (ASP.NET MVC)

通过本教程,他们使用的是 fluent-NHibernate,而我不能使用 fluent(我将通过 *.hbm.XML 配置 NHibernate 类)。

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

Fluent 的具体例子> 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 配置来设置没有 Fluent 的 NHibernate。如果有人可以演示为非流利的 NHibernate 设置 NHibernate 等的代码,或者将我指向一个很棒的示例/教程/博客!

【问题讨论】:

    标签: c# asp.net-mvc nhibernate castle-windsor


    【解决方案1】:

    它似乎像这样工作。我还没有尝试从数据库中保存或检索数据,但是初始化控制器等是没有错误的。其余代码就像教程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()
            );
        }
    }
    

    本教程的区别在于不需要 Fluent 配置,而是使用 NHibernates Configuration().Configure() 工作。

    【讨论】:

      猜你喜欢
      • 2011-06-27
      • 2012-01-23
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-21
      • 1970-01-01
      相关资源
      最近更新 更多