How can I create a Fluent NHibernate Convention that ignores properties that don't have setters
Asked Answered
E

4

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;}} //<-------
}
Emmyemmye answered 23/8, 2010 at 11:19 Comment(2)
+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).Diarist
@Diarist that's acceptable workaround though.Emmyemmye
R
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

Recrudesce answered 23/8, 2010 at 11:26 Comment(2)
This omits props with private set;.Emmyemmye
+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).Diarist
A
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.

Andreandrea answered 10/9, 2010 at 21:11 Comment(2)
I kind a started using methods instead. Would like to accept it but a bit lazy to test it atm. :)Emmyemmye
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.Andreandrea
R
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;
}
Ribbonwood answered 18/11, 2011 at 22:57 Comment(0)
A
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);
    }
}
Alcalde answered 30/5, 2019 at 4:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.