2

I'm starting a new project using conformist mappings. I have a common set of fields pulled out into a component mapping as such:

public class AuditTrailMap : ComponentMapping<AuditTrail>
{
    public AuditTrailMap()
    {
        Property(x => x.Created, xm => xm.NotNullable(true));
        Property(x => x.Updated, xm => xm.NotNullable(true));
        Property(x => x.UpdatedBy, xm => { xm.NotNullable(true); xm.Length(128); });
        Property(x => x.UpdatedBySID, xm => { xm.NotNullable(true); xm.Length(128); });
        Property(x => x.OnBehalfOf, xm => { xm.NotNullable(true); xm.Length(128); });
        Property(x => x.OnBehalfOfSID, xm => { xm.NotNullable(true); xm.Length(128); });
    }
}

However in my class mapping there doesn't seem to be any overload of the Component method which would accept one of these nor is it magically picked up. How do I use this mapping?

JeffreyABecker
  • 2,724
  • 1
  • 25
  • 36

1 Answers1

0

I would create an extension method that helps mapping the component:

public static class AuditTrailMapingExtensions
{
    public static void AuditTrailComponent<TEntity>(
        this PropertyContainerCustomizer<TEntity> container,
        Func<TEntity, AuditTrail> property)
    {
        container.Component(
            property,
            comp => 
            {
                    comp.Property(x => x.Created, xm => xm.NotNullable(true));
                    comp.Property(x => x.Updated, xm => xm.NotNullable(true));
                    comp.Property(x => x.UpdatedBy, xm => { xm.NotNullable(true); xm.Length(128); });
                    comp.Property(x => x.UpdatedBySID, xm => { xm.NotNullable(true); xm.Length(128); });
                    comp.Property(x => x.OnBehalfOf, xm => { xm.NotNullable(true); xm.Length(128); });
                    comp.Property(x => x.OnBehalfOfSID, xm => { xm.NotNullable(true); xm.Length(128); });
            });
    }

Then use it like this:

class MyEntityDbMapping : ClassMapping<MyEntity>
{
    public MyEntityDbMapping ()
    {
        // ...
        this.AuditTrailComponent(x => x.AuditTrail);
    }
}

There is a big advantage of such a helping method: You can add additional arguments, for instance to conditionally set not-null constraints.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • That's what I eventually did, my question has more to do with "How do I use this thing". As far as I can tell, I can create this ComponentMapping class but there isn't any feature that would let me *use it*. – JeffreyABecker Oct 08 '12 at 12:51
  • Could be ... I never tried to use it. You may find it when reading the NH source. – Stefan Steinegger Oct 08 '12 at 13:24