FluentNHibernate Overrides: UseOverridesFromAssemblyOf non-generic version

Posted by ThiagoAlves on Stack Overflow See other posts from Stack Overflow or by ThiagoAlves
Published on 2010-04-02T00:36:11Z Indexed on 2010/04/02 0:43 UTC
Read the original article Hit count: 462

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)

© Stack Overflow or respective owner

Related posts about c#

Related posts about fluent-nhibernate