Profile和MembershipUser复杂视图模型设计

时间:2012-03-07 00:12:10

标签: c# .net asp.net-mvc-3 viewmodel

我有我的mvc 3网络应用程序的一部分,我需要允许用户(在这种情况下是管理员)来管理用户。管理员将能够编辑用户的个人资料信息,以及某些.NET成员资格属性,例如UserName,Email,IsLockedOut。我正在努力的部分是如何最好地将所有这些都包装到自定义模型中。

目前我有一个类似的Profile对象:

 public class Profile
    {
        public int ProfileID { get; set; }
        public Guid UserID { get; set; }  // link to membership user ID
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Phone { get; set; }
    }

我的第一个想法是做这样的事情:

public class ProfileUser
    {
        public ProfileUser(Profile profile)
        {
            Profile = profile;
            User = Membership.GetUser(profile.UserID);
        }

        public Profile Profile { get; set; }
        public MembershipUser User { get; set; }
    }

但由于某些字段在MembershipUser类中是只读的,因此我无法使用UserName或IsLockedOut字段的新值直接将模型传递回控制器以更新数据库。

然后我想到只是为两个类创建一个包装器作为一个类:

public class ProfileUser : Profile
{
    public ProfileUser(Profile profile)
    {
        this.ProfileID = profile.ProfileID;
        this.UserID = profile.UserID;
        this.FirstName = profile.FirstName;
        this.LastName = profile.LastName;
        this.Phone = profile.Phone;
    }

    private string userName;
    public string UserName
    {
        get
        {
            if (userName == null)
            {
                userName = Membership.GetUser(this.UserID).UserName;
            }
            return userName;
        }
        set
        {
            userName = value;
        }
    }

    private string email;
    public string Email
    {
        get
        {
            if (email == null)
            {
                email = Membership.GetUser(this.UserID).Email;
            }
            return email;
        }
        set
        {
            email = value;
        }
    }

    private bool? isLockedOut;
    public bool? IsLockedOut
    {
        get
        {
            if (isLockedOut == null)
            {
                isLockedOut = Membership.GetUser(this.UserID).IsLockedOut;
            }
            return isLockedOut;
        }
        set
        {
            isLockedOut = value;
        }
    }
}

如果我这样做,我可以传递值,因为管理员将它们输回到模型并执行我自己的魔法来更新数据库中的那些只读字段,但我不确定我是否正在实现这一点上课最好的方法。

免责声明:我意识到最好的路线可能是成为自定义会员或个人资料提供商,但由于我手中的原因,我不能这样做。在所有这些示例中,我将从DB中检索配置文件对象,该对象已经从其成员资格记录中链接了UserID。

对此的任何指示都将不胜感激。提前感谢您抽出宝贵时间提供帮助。

0 个答案:

没有答案