Yippie-Ki-Yay
Yippie-Ki-Yay

Reputation: 22854

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

Upvotes: 0

Views: 390

Answers (2)

Raghu
Raghu

Reputation: 2768

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

Upvotes: 0

KeithS
KeithS

Reputation: 71591

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.

Upvotes: 4

Related Questions