views:

118

answers:

1

Hi,

I have a repository class that inherits from a generic implementation:

public namespace RepositoryImplementation {
    public class PersonRepository : Web.Generics.GenericNHibernateRepository<Person>
}

The generic repository implementation uses Fluent NHibernate conventions. They're working fine. One of those conventions is that all properties are not nullable.

Now I need to define that specific properties may be nullable outside the conventions. Fluent NHibernate has an interesting override mechanism:

public namespace RepositoryImplementation {
    public class PersonMappingOverride : IAutoMappingOverride<Person> {
        public void Override(FluentNHibernate.Automapping.AutoMapping<Funcionario> mapping)
        {
            mapping.Map(x => x.PhoneNumber).Nullable();
        }
    }
}

Now I need to register the override class into Fluent NHibernate. I have the following code in the Web.Generics.GenericNHibernateRepository generic class:

AutoMap.AssemblyOf<Person>()
  .Where(type => type.Namespace == "Entities")
  .UseOverridesFromAssemblyOf<PersonMappingOverride>();

The problem is: UseOverridesFromAssemblyOf is a generic method, and I can't do something like that:

.UseOverridesFromAssemblyOf<PersonMappingOverride>();

Because that would cause a circular reference. I don't want the generic repository to know the either repository or the mapping override class, because they vary from project to project.

I see another solution: in the GenericNHibernateRepository class I can do this.GetType() and get the repository implementation type (e.g.: PersonRepository). However I can't call UseOverridesFromAssemblyOf() passing a type.

Is there another way to configure overrides in FluentNHibernate? If not, how could I call UseOverridesFromAssemblyOf<T> without making the generic repository depend upon the repository implementation or the mapping override class?

(Source: http://wiki.fluentnhibernate.org/Auto_mapping#Overrides)

A: 

Here is the way I do it (source over on http://github.com/ToJans/MVCExtensions/blob/master/src/MvcExtensions/Services/Impl/FluentNHibernate/Database.cs )

    am1.GetType()
   .GetMethod("UseOverridesFromAssemblyOf")
   .MakeGenericMethod(mappings.GetType())
   .Invoke(am1,null);
Tom
It worked like a charm :)Thanks a lot!
ThiagoAlves