简体   繁体   中英

C# NHibernate mapping

Let's say I've got some fluently configured NHibernate settings, which is using both Fluent mappings and Automappings .

        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?

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:

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. 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. 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.

Keith has pretty much answered your question, however for more bulletproofing you could check out the PersistenceSpecification class. (It requires the knowledge of type T though)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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