1
votes

I'm trying to get fNH to map to a custom type, but I'm having difficulties.

I want fNH to assign its value, via an interface to the custom type. I also need nHibernate to preserve the instance of the custom type on the entity. It will always be instantiated when the property is accessed, don't overwrite the instance, just set the wrapped value.

When I try the below mapping, it throws an exception "Could not find a getter for property 'Value' in class 'Entities.User"

Ideas?

fNH mapping:

Map(x =>((IBypassSecurity<string>)x.SecuredPinNumber).Value,"[PinNumber]");

Domain example:

public class User
{
 public SecureField<string> SecuredPinNumber {get;private set;}
}

public class SecureField<T> : IBypassSecurity<T>
{
 public T Value { get; set; } // would apply security rules, for 'normal' use
 T IBypassSecurity<T>.Value {get;set;} // gets/sets the value directy, no security.
}

// allows nHibernate to assign the value without any security checks
public interface IBypassSecurity<T>
{
 T Value {get;set;}
}
1

1 Answers

2
votes

The Map() method is an expression builder to extract property names as strings. So your mapping tells NH, that you want to map the property "Value" in class User, which does not exist, of course. If you want to use your custom type, read the NH reference documentation about this and use the CustomType() method in your mapping.

You could also use a protected property for the PinNumber, that allows direct access.

public class User
{
    protected virtual string PinNumber { get; set; }  // mapped for direct access
    public string SecuredPinNumber
    {
        get { /* get value with security checks */ }
        set { /* set value with security checks */ }
    }
}

You can read this post about mapping protected properties with Fluent.