views:

90

answers:

2

I'm looking for a FluentNH (Fluent NHibernate) convention or configuration that ignores all properties that have no setter:

It would still map these:

public class foo{
  public virtual int bar {get; private set;}
}

And omit these:

public class foo{
  public virtual int fizz{get;private set;}
  public virtual int bar{get {return fizz;}} //<-------
}
+2  A: 

You should use a custom mapping configuration

public class DefaultMappingConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Member member)
    {
        return member.CanWrite;
    }
}

Usage :

var nhConfiguration = new Configuration().Configure();
var mappingConfiguration = new DefaultMappingConfiguration();

var.fluentConfiguration = Fluently.Configure(nhConfiguration );
    .Mappings(m => m.AutoMappings.Add(
        AutoMap.AssemblyOf<MappedType>(mappingConfiguration)
    ));

var sessionFactory = this.fluentConfiguration.BuildSessionFactory();

However, private setters won't get mapped. You should get them as protected

mathieu
This omits props with `private set;`.
Arnis L.
ok updated my answer
mathieu
+1: simple, fine and as it should be done with FluentNH (but remove that stroked first line... ;). PS: note that `CanWrite` returns false when there's a private setter (I think).
Abel
+1  A: 

Use this:

public class DefaultMappingConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Member member)
    {
        if (member.IsProperty && !member.CanWrite)
        {
            return false;
        }

        return base.ShouldMap(member);
    }
}

That should handle the case of no setter and private setter.

Daniel T.
I kind a started using methods instead. Would like to accept it but a bit lazy to test it atm. :)
Arnis L.
Well it works for me and my production code, but I don't use any private setters. I'm not sure if NHibernate can deal with properties with private setters.
Daniel T.