在流畅的NHibernate中将IUserType映射到组件属性

Jos*_*son 2 nhibernate fluent-nhibernate iusertype

我正在尝试实现IUserType状态和国家/地区代码,这将允许我访问两个字母的代码(存储在数据库中的代码)以及全名.我正在关注NHibernate 3.0 Cookbook中的示例(p.225),但我的问题是我的StreetAddress类当前被映射为我的自动配置中的一个组件:

public override bool IsComponent(Type type)
{
    return type == typeof(StreetAddress);
}
Run Code Online (Sandbox Code Playgroud)

将此类标识为组件,我不知道如何使用IUserType组件类的属性,因为该类未显式映射.我无处可以告诉流利的NHibernate使用IUserType规范.

Jos*_*son 6

@Firo很接近,但事实证明这是一个更容易的解决方案.这里有两个步骤.首先,我不得不告诉Fluent NHibernate不要映射驻留在我的域层中的StateCountry类:

public override bool ShouldMap(Type type)
{
    return type.Name != "State" && type.Name != "Country";
}
Run Code Online (Sandbox Code Playgroud)

接下来,我只需要为IUserType类创建约定.事实证明这比@Firo建议更容易:

public class CountryUserTypeConvention : UserTypeConvention<CountryType>
{
}

public class StateUserTypeConvention : UserTypeConvention<StateType>
{
}
Run Code Online (Sandbox Code Playgroud)

这些内容的定义IUserTypes是从原始问题中引用的食谱中删除的,但如果您不想阅读它:

public class CountryType : GenericWellKnownInstanceType<Country, string>
{
    // The StateType is pretty much the same thing, only it uses "StateCode" instead of "CountryCode"
    private static readonly SqlType[] sqlTypes =
        new[] {SqlTypeFactory.GetString(2)};

    public CountryType()
        : base(new Countries(),
               (entity, id) => entity.CountryCode == id,
               entity => entity.CountryCode)
    {
    }

    public override SqlType[] SqlTypes
    {
        get { return sqlTypes; }
    }
}
Run Code Online (Sandbox Code Playgroud)

这源于GenericWellKnownInstanceType:

[Serializable]
public abstract class GenericWellKnownInstanceType<T, TId> :
    IUserType where T : class
{
    private Func<T, TId, bool> findPredicate;
    private Func<T, TId> idGetter;
    private IEnumerable<T> repository;

    protected GenericWellKnownInstanceType(
        IEnumerable<T> repository,
        Func<T, TId, bool> findPredicate,
        Func<T, TId> idGetter)
    {
        this.repository = repository;
        this.findPredicate = findPredicate;
        this.idGetter = idGetter;
    }

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

    public bool IsMutable
    {
        get { return false; }
    }

    public new bool Equals(object x, object y)
    {
        if (ReferenceEquals(x, y))
        {
            return true;
        }
        if (ReferenceEquals(null, x) ||
            ReferenceEquals(null, y))
        {
            return false;
        }
        return x.Equals(y);
    }

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

    public object NullSafeGet(IDataReader rs,
                              string[] names, object owner)
    {
        int index0 = rs.GetOrdinal(names[0]);
        if (rs.IsDBNull(index0))
        {
            return null;
        }
        var value = (TId) rs.GetValue(index0);
        return repository.FirstOrDefault(x =>
                                         findPredicate(x, value));
    }

    public void NullSafeSet(IDbCommand cmd,
                            object value, int index)
    {
        if (value == null)
        {
            ((IDbDataParameter) cmd.Parameters[index])
                .Value = DBNull.Value;
        }
        else
        {
            ((IDbDataParameter) cmd.Parameters[index])
                .Value = idGetter((T) value);
        }
    }

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

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

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

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

    /// <summary>
    /// The SQL types for the columns
    /// mapped by this type.
    /// </summary>
    public abstract SqlType[] SqlTypes { get; }
}
Run Code Online (Sandbox Code Playgroud)

这些类的库都仅仅是一ReadOnlyCollection对的StateCountry对象.再次,从食谱:

public class States : ReadOnlyCollection<State>
{
    // Truncated in the interest of brevity
    public static State Arizona = new State("AZ", "Arizona");
    public static State Florida = new State("FL", "Florida");
    public static State California = new State("CA", "California");
    public static State Colorado = new State("CO", "Colorado");
    public static State Oklahoma = new State("OK", "Oklahoma");
    public static State NewMexico = new State("NM", "New Mexico");
    public static State Nevada = new State("NV", "Nevada");
    public static State Texas = new State("TX", "Texas");
    public static State Utah = new State("UT", "Utah");

    public States() : base(new State[]
                               {
                                   Arizona, Florida, California, Colorado,
                                   Oklahoma, NewMexico, Nevada, Texas, Utah
                               }
        )
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这有助于那里的人.