4

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;}} //<-------
}
Abel
  • 56,041
  • 24
  • 146
  • 247
Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
  • 1
    +1 Interesting question. I normally just create a getter-method: `GetBar()`, which in addition makes its intend clearer to its users (but that's not a solution to your issue, of course). – Abel Aug 23 '10 at 11:23
  • @Abel that's acceptable workaround though. – Arnis Lapsa Aug 23 '10 at 11:27

4 Answers4

7

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
  • 30,974
  • 4
  • 64
  • 90
  • 2
    This omits props with `private set;`. – Arnis Lapsa Aug 23 '10 at 11:38
  • +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 Aug 23 '10 at 16:02
3

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.
  • 37,212
  • 36
  • 139
  • 206
  • I kind a started using methods instead. Would like to accept it but a bit lazy to test it atm. :) – Arnis Lapsa Sep 11 '10 at 11:47
  • 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. Sep 13 '10 at 19:14
0

Another way is to use an attribute.

public class MyEntity
{
    [NotMapped]
    public bool A => true;
}

public class AutomappingConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Member member)
    {
        if (member.MemberInfo.GetCustomAttributes(typeof(NotMappedAttribute), true).Length > 0)
        {
            return false;
        }
        return base.ShouldMap(member);
    }
}
keithyip
  • 985
  • 7
  • 21
0

I know this is old question but code below do well with private setters.

public override bool ShouldMap(Member member)
{
    var prop = member.DeclaringType.GetProperty(member.Name);
    bool isPropertyToMap = 
        prop != null &&
        prop.GetSetMethod(true) != null &&
        member.IsProperty;

    return
        base.ShouldMap(member) && isPropertyToMap;
}
dariol
  • 1,959
  • 17
  • 26