Domanda

This question is related to Mapping interface or abstract class component I'm also trying to map a component declared as an interface, but I'm using the built-in mapping-by-code/Conformist approach.

Let's say I have an entity Login (C#):

public class Login
{
    public virtual int Id { get; set; }
    public virtual string UserName { get; set; }
    public virtual IPassword Password { get; set; }
}

I want to abstract away how the password is stored, so I've defined a simple interface IPassword

public interface IPassword
{
    bool Matches(string password);
}

An example implementation is HashedPassword:

public class HashedPassword : IPassword
{
    public virtual string Hash { get; set; }
    public virtual string Salt { get; set; }

    public virtual bool Matches(string password){ /* [snip] */ }
}

I want to map Login.Password as a component rather than a many-to-one or one-to-one relation. Using XML I'd map it like this:

<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="..." namespace="...">
  <class name="Login">
    <id name="Id">...</id>
    <property name="UserName" .../>
    <component name="Password" class="HashedPassword">
      <property name="Hash" not-null="true" length="32"/>
      <property name="Salt" not-null="true" length="32"/>
    </component>
  </class>
</hibernate-mapping>

This works as expected.

Here's my attempt to map it using NHibernate's built-in mapping-by-code facilities:

public class LoginMapping : ClassMapping<Login>
{
    public LoginMapping()
    {
        Id(x => x.Id, map => map.Generator(Generators.HighLow));
        Property(x => x.UserName, map => map.Length(32));
        Component(x => x.Password, comp =>
            {
                comp.Class<HashedPassword>();
                comp.Property("Salt", map => map.Length(32));
                comp.Property("Hash", map => map.Length(32));
            });
    }
}

When I use this mapping I get the following exception:

NHibernate.MappingException: Member not found. The member 'Salt' does not exists in type IPassword

While it's true that Salt isn't a member of IPassword, it is a member of the class that I set with comp.Class<HashedPassword>()

Do you know how I can map this scenario without getting the exception?


So far there I haven't found a solution to the question itself. There are two work-arounds at the moment:

  1. Resort to XML mapping or FluentNHibernate. This could probably done for the "problematic" mappings only.

  2. Instead of a component, use a user type. This is what I'm doing right now. The type in my case (the hashed password) is immutable and can be stored as a single column, so the user type is fairly simple.

Here's the user type I'm currently using (for sake of completion). I use PBKDF2 to create secure hashes. Note that in my application all data (salt, hash and PBKDF2 iteration count) is stored in one property (simply named Hash) of the HashedPassword.

public abstract class ImmutableValue<T> : IUserType where T : class
{
    public abstract SqlType[] SqlTypes { get; }

    public virtual Type ReturnedType
    {
        get { return typeof (T); }
    }

    public bool IsMutable
    {
        get { return false; }
    }

    bool IUserType.Equals(object x, object y)
    {
        return InternalEquals(x, y);
    }

    protected virtual bool InternalEquals(object x, object y)
    {
        return Equals(x, y);
    }

    public virtual int GetHashCode(object x)
    {
        return x == null ? 0 : x.GetHashCode();
    }

    public virtual object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        return Load(rs, names, owner);
    }

    protected abstract T Load(IDataReader rs, string[] names, object owner);

    public virtual void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        Save(cmd, (T) value, index);
    }

    protected abstract void Save(IDbCommand cmd, T value, int index);

    public virtual object DeepCopy(object value)
    {
        return value;
    }

    public virtual object Replace(object original, object target, object owner)
    {
        return original;
    }

    public virtual object Assemble(object cached, object owner)
    {
        return cached;
    }

    public virtual object Disassemble(object value)
    {
        return value;
    }

    protected void SetParameter(IDbCommand cmd, int index, object value)
    {
        var parameter = (IDataParameter) cmd.Parameters[index];
        var parameterValue = value ?? DBNull.Value;
        parameter.Value = parameterValue;
    }
}

public class HashedPasswordType : ImmutableValue<HashedPassword>
{
    public override SqlType[] SqlTypes
    {
        get { return new SqlType[] {SqlTypeFactory.GetString(HashedPassword.ContentLength)}; }
    }

    protected override HashedPassword Load(IDataReader rs, string[] names, object owner)
    {
        var str = NHibernateUtil.String.NullSafeGet(rs, names[0]) as string;
        return HashedPassword.FromContent(str);
    }

    protected override void Save(IDbCommand cmd, HashedPassword value, int index)
    {
        SetParameter(cmd, index, value == null ? null : value.Hash);
    }
}

The required mapping is then comparatively simple:

Property(x => x.Password, map =>
    {
        map.Type<HashedPasswordType>();
        map.NotNullable(true);
    });
È stato utile?

Soluzione

As I haven't found a solution using component mapping yet I'll add the workarounds as an answer for now.

Possible workarounds:

  1. Use XML mapping for the types in question. This supports my scenario without an issue. See the question for an example.
  2. Use FluentNHibernate. It just supports this kind of mapping out of the box, see example below.
  3. Use a user type instead of a component. This is comparatively much more work, but at least it will work with mapping-by-code.

Example of the desired mapping in FluentNH. The concrete type is passed in as the generic argument.

Component<HashedPassword>(x => x.Password, comp =>
{
    comp.Map(x => x.Hash);
    comp.Map(x => x.Salt);
});

An example of a user type is given at the end of the question. In the real example I use PBKDF2 to create secure hashes. Note that in the user type shown in the question all data (salt, hash and PBKDF2 iteration count) are stored in one column to keep the type simple.

My current conclusion is that mapping-by-code simply does not support what I'm asking for. I'm now considering moving from mapping-to-code to FluentNH.

If there's news related to this issue I'll update the answer and/or question accordingly.

Altri suggerimenti

I'm not really familiar with mapping-by-code, but when using Fluent NHibernate, I've had success with

Map(x => (x as HashedPassword).Hash)

You could try

comp.Property(x => (x as HashedPassword).Hash)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top