NHibernate保护内部与内部

时间:2011-11-17 19:54:51

标签: nhibernate fluent-nhibernate

我需要先说明这一事实,当谈到nhibernate时,我是一个完全新手,而且我不知道我不知道的事情。

我有一个像这样的用户类:

public class nhMembershipUser : MembershipUser
{
    public new virtual Guid ProviderUserKey { get; set; }
    public new virtual DateTime LastPasswordChangedDate { get; set; }
    public virtual bool IsEnabled { get; set; }
    protected internal virtual byte[] Password { get; set; }
    protected internal virtual string Salt { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string Application { get; set; }
    public new virtual string UserName { get; set; }
    public override bool IsOnline
    {
        get
        {
            var UnitOfWork = new nhUnitOfWork();
            return UnitOfWork.UserIsOnline(this);
        }
    }
    public new virtual bool IsLockedOut { get; set; }
    public virtual IList<nhRole> Roles { get; set; }

}

...和一个像这样流畅的映射类:

public class nhMembershipUserMap : ClassMap<nhMembershipUser>
{
    public nhMembershipUserMap()
    {
        Table("Users");
        Id(x => x.ProviderUserKey).Column("UserID");
        Map(x => x.Comment).Nullable().Length(10000);
        Map(x => x.CreationDate).CustomType<UtcDateTimeType>().Not.Nullable();
        Map(x => x.Email).Not.Nullable().Length(250);
        Map(x => x.IsApproved).Default("1");
        Map(x => x.IsLockedOut).Default("0");
        Map(x => x.LastActivityDate).Nullable();
        Map(x => x.LastLockoutDate).Nullable();
        Map(x => x.LastLoginDate).Nullable();
        Map(x => x.LastPasswordChangedDate).Nullable();
        Map(x => x.PasswordQuestion).Nullable().Length(250);
        Map(x => x.ProviderName).Default("").Not.Nullable().Length(250);
        Map(x => x.UserName).Not.Nullable().Length(250);
        Map(x => x.IsEnabled).Default("1").Not.Nullable();
        Map(Reveal.Member<nhMembershipUser>("Password")).Length(260).Nullable();
        Map(Reveal.Member<nhMembershipUser>("Salt")).Length(250).Nullable();
        Map(x => x.FirstName).Length(35).Not.Nullable();
        Map(x => x.LastName).Length(35).Not.Nullable();
        Map(x => x.Application).Length(250).Not.Nullable();
        HasManyToMany(x => x.Roles).ChildKeyColumn("RoleId").ParentKeyColumn("UserId").Cascade.All().Table("UsersToRoles");

    }
}

这构建,但当然我不能在类之外获取或设置Salt或Password属性,这是假的。我正在尝试保持我的用户类轻松和POCO-ish并在我的会员提供商中构建salting和散列功能;我真的不希望公众有访问权限,但我想要我的提供者类。显而易见的答案是使用一个内部访问修饰符,但是当我这样做时,NH没有受保护的修饰符就会咆哮我,这会破坏目的。

如何让我的程序集中的另一个类访问这些受限制的属性并保存到NHibernate?看起来这应该很容易实现。

1 个答案:

答案 0 :(得分:2)

public class nhMembershipUser : MembershipUser
{
    private byte[] PasswordHash;
    private string Salt;

    public void SetPassword(ICredentialsProvider provider)
    {
        PasswordHash = provider.GetHash(this);
        Salt = provider.GetSalt(this);
    }
}