0

I want to use map mechanism, based on property attribute marking, like this:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class DomainSignatureAttribute : Attribute { }

public class SomeThing {
    [DomainSignature]
    public virtual string SomePropertyForMapping {get;set;}
    [DomainSignature]
    public virtual int OtherPropertyForMapping {get;set;}

    public virtual string OtherPropertyWithoutMapping {get;}
}

So in ClassMap children I want to realise this method, that map all properties, marked by DomainSignatureAttribute attribute:

protected void MapPropertiesWithStandartType()
{
    foreach (PropertyInfo property in typeof(T).GetProperties())
    {
        if (property != null)
        {
            object[] domainAttrs = property.GetCustomAttributes(typeof (DomainSignatureAttribute), true);

            if (domainAttrs.Length > 0)
                Map(x => property.GetValue(x, null));
        }
    }
}

But there is a small problem with Linq. When FluentNHibernate builds mapping (Cfg.BuildSessionFactory()) it break with

Tried to add property 'GetValue' when already added.

exception. As I undestand I need to rebuild Linq expression: x => property.GetValue(x, null) into correct form.

Please don't suggest to use AutoMap feature and don't suggest to use manual mapping.

user809808
  • 779
  • 1
  • 10
  • 23
  • You can use auto-mapping + auto-mapping override (IAutoMappingOverride) and then defining mapping.IgnoreProperty(...) just for the properties that you don't want to map. – VahidN Nov 05 '11 at 19:37

1 Answers1

1
var domainproperties = typeof(T).GetProperties()
    .Where(prop => prop.GetCustomAttributes(typeof (DomainSignatureAttribute), true).Length > 0);

foreach (var property in domainproperties)
{
    Map(Reveal.Member<T>(property.Name));
}
Firo
  • 30,626
  • 4
  • 55
  • 94