سؤال

I have been spending a couple of days now to get to know the Fluent NHibernate automapping working model. It is quite nice, but I keep detecting new details missing from my schemas. Now I want to add extra properties to my classes, but not have them mapped to the database. A typical case is when I need extra properties with internal logic.
So I read the examples and scanned StackOverflow and found out that this was not another convention to be added, but rather a matter of inheriting the DefaultAutomappingConfiguration and override the ShouldMap method.
Fine, no problem, one minute later I had something like this:

public class CustomAutomappingConfiguration : DefaultAutomappingConfiguration
{

    public override bool ShouldMap(Member member)
    {
        var explicitSkip = member.PropertyType.GetCustomAttributes(typeof(SkipMap), false).Length > 0;
        if ((member.IsProperty && !member.CanWrite) || explicitSkip)
        {
            return false;
        }
        return base.ShouldMap(member);
    }
}


/// <summary>
/// Don't map this property to database.
/// </summary>
public class SkipMap : Attribute
{
}


public class DemoClass
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual MyBitwiseEnum Status { get; set; }

    public virtual bool IsValid
    {
        get
        {
            return (int)Status > 3;
        }
    }

    [SkipMap]
    public virtual bool IsBad
    {
        get
        {
            return MyBitwiseEnum.HasFlag(MyBitwiseEnum.Bad);
        }
        set
        {
            MyEnum = value ? MyBitwiseEnum | MyBitwiseEnum.Bad : MyBitwiseEnum ^ MyBitwiseEnum.Bad;
        }
    }
}

I know that my demo class is kind of stupid, but it will illustrate my point.
The idea is that I want to manually decide what properties to map to database.

The readonly property works fine because the ShouldMap method will look for property.CanWrite. But the custom attribute that definitely is set will not be detected. Why is that!?
In the convention methods I have used the same approach frequently and there it works fine. Why is the property not able to detect defined attributes here, when it obviously can in the convention setting. Is there a workaround?

هل كانت مفيدة؟

المحلول

have you added your new automapconvention to Automap?

AutoMap.AssemblyOf<>(new CustomAutomappingConfiguration())

Update: you are getting the skip attribute from Boolean class instead of the property

member.PropertyType.GetCustomAttributes(typeof(SkipMap), false)

should be

member.MemberInfo.GetCustomAttributes(typeof(SkipMap), false)

نصائح أخرى

Just to be sure the custom attribute is applicable to properties, try adding [AttributeUsage(AttributeTargets.Property)] to your SkipMap class.

Another possibility is an attribute name clash with another attribute that applies to different targets. Try renaming the class to something like MyVerySpecialSkipMap and retest to verify you don't have an attribute clash. At the very least, write some simple reflection code to test for the SkipMap attribute outside the context of your application to ensure it can be found.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top