Rhino.Commons + 流畅的NHibernate

发布于 2024-07-11 20:44:30 字数 97 浏览 13 评论 0原文

有没有办法将 Rhino.Commons 与 Fluent Nhibernate 一起使用(特别是 AutoMapping)?

非常感

谢弗拉诺

Is there a way to use Rhino.Commons with Fluent Nhibernate, (in particular AutoMapping)?

Many thanks

fromano

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦开始←不甜 2024-07-18 20:44:30

# # # # # # # # # #

更新:
该答案是在 2009 年 2 月写的。按照 FluentNHibernate 项目变化的速度,该方法很可能已被重命名,或者更改约定的机制已被移动。

# # # # # # # # # #

我想使用来自 Rhino.Commons.ForTesting 的 DatabaseTextFixtureBase 和 FluentNHibernate。 我一直遇到的问题是 FluentNHibernate 将它的魔力添加到 PersistenceModel 类中的 NHibernate 配置中,并且一旦您调用 DatabaseTestFixtureBase.InitializeNHibernateAndIoC() Rhino.Commons 私下就可以掌握 NHibernate 配置。并且您不再有权访问 NHibernate 配置。

事实证明,我最后的假设是错误的。 如果您向容器注册 INHibernateInitializationAware 服务,则 NHibernateUnitOfWorkTestContext 在调用 CreatConfigs() 时将拾取该服务。

我正在使用的 INHibernateInitializationAware 实现如下所示:

public class FluentNHibernateInitializationAwareConfigurator : INHibernateInitializationAware
{
    public void BeforeInitialization(){}

    public void Configured(Configuration cfg)
    {
        var persistenceModel = new PersistenceModel
                                   {
                                       Conventions =
                                           {
                                               GetForeignKeyName = (prop => prop.Name + "Id"),
                                               GetForeignKeyNameOfParent = (prop => prop.Name + "Id")
                                           }
                                   };
        persistenceModel.addMappingsFromAssembly(typeof(OneOfMyMappingClasses).Assembly);
        persistenceModel.Configure(cfg);

    }

    public void Initialized(Configuration cfg, ISessionFactory sessionFactory){}
}

当然,约定可以替换为您碰巧使用的任何约定。

享受!


如果您使用最新版本的 FluentNHibernate,您可能会使用 Fluently 类来配置映射。 这是 FluentNHibernateInitializationAwareConfigurator 的另一个版本:

public class FluentNHibernateInitializationAwareConfigurator : INHibernateInitializationAware  
{  
    public void BeforeInitialization(){}  

    public void Configured(Configuration cfg)  
    {
        Fluently.Configure().Mappings(m =>
        {
            m.FluentMappings.AddFromAssemblyOf<User>()
                .AlterConventions(convention =>
                    {
                        convention.GetForeignKeyName = (prop => prop.Name + "Id");
                        convention.GetForeignKeyNameOfParent = (prop => prop.Name + "Id");
                    }
                );
            m.Apply(cfg);
        });
    }  

    public void Initialized(Configuration cfg, ISessionFactory sessionFactory){}
}

# # # # # # # # # #

UPDATE:
That answer was written in Feb of 2009. With the rate that the FluentNHibernate project was changing, it's very likely that the method has been renamed, or the mechanism for changing Conventions has been moved.

# # # # # # # # # #

I wanted to use DatabaseTextFixtureBase from Rhino.Commons.ForTesting with FluentNHibernate. The problem that I kept coming up against was the fact that FluentNHibernate adds it's magic to the NHibernate configuration in the PersistenceModel class, and getting your hands on the NHibernate configuration once you call DatabaseTestFixtureBase.InitializeNHibernateAndIoC() Rhino.Commons does a bunch of stuff privately and you no longer have access to the NHibernate config.

As it turns out, my last assumption was wrong. If you register an INHibernateInitializationAware service with the container, it will be picked up by the NHibernateUnitOfWorkTestContext when it calls CreatConfigs().

The INHibernateInitializationAware implementation that I am using looks like this:

public class FluentNHibernateInitializationAwareConfigurator : INHibernateInitializationAware
{
    public void BeforeInitialization(){}

    public void Configured(Configuration cfg)
    {
        var persistenceModel = new PersistenceModel
                                   {
                                       Conventions =
                                           {
                                               GetForeignKeyName = (prop => prop.Name + "Id"),
                                               GetForeignKeyNameOfParent = (prop => prop.Name + "Id")
                                           }
                                   };
        persistenceModel.addMappingsFromAssembly(typeof(OneOfMyMappingClasses).Assembly);
        persistenceModel.Configure(cfg);

    }

    public void Initialized(Configuration cfg, ISessionFactory sessionFactory){}
}

Of course the Conventions can be replaced by any that you happen to be using.

Enjoy!


If you are using the latest version of FluentNHibernate you may be using the Fluently class to configure your mappings. Here's another version of the FluentNHibernateInitializationAwareConfigurator:

public class FluentNHibernateInitializationAwareConfigurator : INHibernateInitializationAware  
{  
    public void BeforeInitialization(){}  

    public void Configured(Configuration cfg)  
    {
        Fluently.Configure().Mappings(m =>
        {
            m.FluentMappings.AddFromAssemblyOf<User>()
                .AlterConventions(convention =>
                    {
                        convention.GetForeignKeyName = (prop => prop.Name + "Id");
                        convention.GetForeignKeyNameOfParent = (prop => prop.Name + "Id");
                    }
                );
            m.Apply(cfg);
        });
    }  

    public void Initialized(Configuration cfg, ISessionFactory sessionFactory){}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文