简体   繁体   English

C#NHibernate映射

[英]C# NHibernate mapping

Let's say I've got some fluently configured NHibernate settings, which is using both Fluent mappings and Automappings . 假设我已经流畅地配置了NHibernate设置,该设置同时使用了Fluent mappingsAutomappings

        Configuration = Fluently.Configure()
            .Database(SQLiteConfiguration.Standard.ShowSql().InMemory)
            .Mappings(x =>
                      {
                          x.FluentMappings.AddFromAssemblyOf<RepositoryEntity>();
                          x.AutoMappings.Add(autoPersistenceModel);
                      });

Now - is it possible to check that some arbitrary type T is mapped (or not mapped) to this configuration? 现在-是否可以检查某个任意类型T是否已映射(或未映射)到此配置?

I am attempting to make some bulletproof repository and I think this moment is kind of crucial. 我正在尝试建立一些防弹库,我认为这一刻至关重要。

Thank you 谢谢

Yes. 是。 After creating your SessionFactory, keep the Configuration around, and set up this method in your repository: 创建SessionFactory之后,保留Configuration,并在存储库中设置此方法:

public bool IsMapped (Type testType)
{
   return MyConfiguration.ClassMappings.Any(m => m.EntityName == testType.FullName);
}

AFAIK this can be used to detect both fluently- and XML-mapped classes. AFAIK可用于检测流利的和XML映射的类。 You may need to compare more closely if you have similarly-named classes in different namespaces, but this should get you started. 如果在不同的命名空间中具有相似名称的类,则可能需要更紧密地比较,但这应该可以帮助您入门。

Something you may also be able to use in developing a "bulletproof" Repo is the EntityNotFoundDelegate, which allows you to define a custom method for dealing with entities given to a repository for which it doesn't have a mapping. 您可能还可以在开发“防弹”仓库中使用的是EntityNotFoundDelegate,它使您可以定义自定义方法来处理提供给没有映射的存储库的实体。 You could, potentially, use this to ask another repository if it can handle the entity, or kick it back to a Strategy pattern that may have several possible repos to try. 您可能会用它来询问另一个存储库是否可以处理该实体,或将其踢回可能有几种可能的回购尝试的Strategy模式。

Keith has pretty much answered your question, however for more bulletproofing you could check out the PersistenceSpecification class. Keith几乎回答了您的问题,但是,要获得更多的防弹效果,可以查看PersistenceSpecification类。 (It requires the knowledge of type T though) (尽管它需要了解类型T的知识)

http://www.packtpub.com/article/using-fluent-nhibernate-persistence-tester-and-ghostbusters-test http://www.packtpub.com/article/using-fluent-nhibernate-persistence-tester-and-ghostbusters-test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM