继承自MembershipUser - Asp.net/C#

时间:2011-05-16 17:18:47

标签: c# readonly inheritance membershipuser

我有一个继承自MembershipUser的客户类。

当我加载Customer对象时,我希望能够轻松地设置基类中的所有值。

我尝试声明Customer的属性,隐藏只读的MembershipUser属性(如ProviderUserKey),然后设置这些属性,但是当我需要设置它们时,基类属性为null。

这是我的班级Customer.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.Security;
using _FusioLibrary.Utilities;

namespace _CutMyCostsLibrary.BusinessObjects
{
   public class Customer : MembershipUser
   {
    #region Properties

    private Boolean active;
    public Boolean Active
    {
        get { return active; }
        set { active = value; }
    }

    private String address1;
    public String Address1
    {
        get { return address1; }
        set { address1 = value; }
    }

    private String address2;
    public String Address2
    {
        get { return address2; }
        set { address2 = value; }
    }

    private String companyName;
    public String CompanyName
    {
        get { return companyName; }
        set { companyName = value; }
    }

    private String county;
    public String County
    {
        get { return county; }
        set { county = value; }
    }

    private DateTime createDate;
    public DateTime CreateDate
    {
        get { return createDate; }
        set { createDate = value; }
    }

    private Int32 customerId;
    public Int32 CustomerId
    {
        get { return customerId; }
        set { customerId = value; }
    }

    private Boolean deleted;
    public Boolean Deleted
    {
        get { return deleted; }
        set { deleted = value; }
    }


    private String firstName;
    public String FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    private String lastName;
    public String LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }

    private String mobile;
    public String Mobile
    {
        get { return mobile; }
        set { mobile = value; }
    }

    private DateTime modifyDate;
    public DateTime ModifyDate
    {
        get { return modifyDate; }
        set { modifyDate = value; }
    }

    private String phone;
    public String Phone
    {
        get { return phone; }
        set { phone = value; }
    }

    private DateTime reminderEmailSentDate;
    public DateTime ReminderEmailSentDate
    {
        get { return reminderEmailSentDate; }
        set { reminderEmailSentDate = value; }
    }

    private String town;
    public String Town
    {
        get { return town; }
        set { town = value; }
    }

    private String userIdAspNet;
    public String UserIdAspNet
    {
        get { return userIdAspNet; }
        set { userIdAspNet = value; }
    }


    #endregion

    #region MembershipUser Properties Hiding 

    read only properties need to be writeable

    private DateTime creationDate;
    public DateTime CreationDate
    {
        get { return creationDate; }
        set { creationDate = value; }
    }

    private bool isLockedOut;
    public bool IsLockedOut
    {
        get { return isLockedOut; }
        set { isLockedOut = value; }
    }

    private bool isOnline;
    public bool IsOnline
    {
        get { return isOnline; }
        set { isOnline = value; }
    }

    private DateTime lastLockoutDate;
    public DateTime LastLockoutDate
    {
        get { return lastLockoutDate; }
        set { lastLockoutDate = value; }
    }

    private DateTime lastPasswordChangedDate;
    public DateTime LastPasswordChangedDate
    {
        get { return lastPasswordChangedDate; }
        set { lastPasswordChangedDate = value; }
    }

    private string passwordQuestion;
    public string PasswordQuestion
    {
        get { return passwordQuestion; }
        set { passwordQuestion = value; }
    }
    private string providerName;
    public string ProviderName
    {
        get { return providerName; }
        set { providerName = value; }
    }

    private object providerUserKey;
    public object ProviderUserKey
    {
        get { return providerUserKey; }
        set { providerUserKey = value; }
    }

    private string userName;
    public string UserName
    {
        get { return userName; }
        set { userName = value; }
    }


    #endregion


    #region Load

    public static Customer Load(IDataReader iDataReader) 
    {
        Customer oCustomer = new Customer();


        //populate the customer properties
        oCustomer.Active = DataReaderUtility.GetBool(iDataReader["Active"]);
        oCustomer.Address1 = DataReaderUtility.GetString(iDataReader["Address1"]);
        oCustomer.Address2 = DataReaderUtility.GetString(iDataReader["Address2"]);
        oCustomer.CompanyName = DataReaderUtility.GetString(iDataReader["CompanyName"]);
        oCustomer.County = DataReaderUtility.GetString(iDataReader["County"]);
        oCustomer.CreateDate = DataReaderUtility.GetDateTime(iDataReader["CreateDate"]);
        oCustomer.CustomerId = DataReaderUtility.GetInt(iDataReader["CustomerId"]);
        oCustomer.Deleted = DataReaderUtility.GetBool(iDataReader["Deleted"]);
        oCustomer.FirstName = DataReaderUtility.GetString(iDataReader["FirstName"]);
        oCustomer.LastName = DataReaderUtility.GetString(iDataReader["LastName"]);
        oCustomer.Mobile = DataReaderUtility.GetString(iDataReader["Mobile"]);
        oCustomer.ModifyDate = DataReaderUtility.GetDateTime(iDataReader["ModifyDate"]);
        oCustomer.Phone = DataReaderUtility.GetString(iDataReader["Phone"]);
        oCustomer.ReminderEmailSentDate = DataReaderUtility.GetDateTime(iDataReader["ReminderEmailSentDate"]);
        oCustomer.Town = DataReaderUtility.GetString(iDataReader["Town"]);
        oCustomer.UserIdAspNet = DataReaderUtility.GetString(iDataReader["UserIdAspNet"]);



        //populate the MembershipUser properties
        if(!string.IsNullOrEmpty(oCustomer.UserIdAspNet))
        {
           MembershipUser oMembershipUser  = Membership.GetUser(Guid.Parse(oCustomer.UserIdAspNet));


           oCustomer.Comment = oMembershipUser.Comment;
           oCustomer.CreationDate = oMembershipUser.CreationDate;
           oCustomer.Email = oMembershipUser.Email;
           oCustomer.IsApproved = oMembershipUser.IsApproved;
           oCustomer.IsLockedOut = oMembershipUser.IsLockedOut;
           oCustomer.IsOnline = oMembershipUser.IsOnline;
           oCustomer.LastActivityDate = oMembershipUser.LastActivityDate;
           oCustomer.LastLockoutDate = oMembershipUser.LastLockoutDate;
           oCustomer.LastLoginDate = oMembershipUser.LastLoginDate;
           oCustomer.LastPasswordChangedDate = oMembershipUser.LastPasswordChangedDate;
           oCustomer.PasswordQuestion = oMembershipUser.PasswordQuestion;
           oCustomer.ProviderName = oMembershipUser.ProviderName;
           oCustomer.ProviderUserKey = oMembershipUser.ProviderUserKey;
           oCustomer.UserName = oMembershipUser.UserName;

        }


        return oCustomer;

    }

    #endregion
 }
}

现在,当我尝试执行以下代码时,我收到错误,因为基类ProviderUserKey为null。

                    Customer oCustomer = CustomerManager.GetCustomer(UrlUtility.GetStringValue("UserId"));



                    //first we need to update the username                                    
                    CustomerManager.UpdateCustomerUserName(oCustomer.UserIdAspNet, this.txtEmail.Text);

                    //Insert the modified data into the object

                    oCustomer.Active = this.cbActive.Checked;
                    oCustomer.Address1 = this.txtAddress1.Text;
                    oCustomer.Address2 = this.txtAddress2.Text;
                    oCustomer.CompanyName = this.txtCompanyName.Text;
                    oCustomer.County = this.txtCounty.Text;
                    oCustomer.FirstName = this.txtFirstName.Text;
                    oCustomer.LastName = this.txtLastName.Text;
                    oCustomer.Mobile = this.txtMobile.Text;
                    oCustomer.ModifyDate = DateTime.Now;
                    oCustomer.Phone = this.txtPhone.Text;
                    oCustomer.Town = this.txtTown.Text;
                    oCustomer.Email = this.txtEmail.Text;

                    //Update The Customer table
                    CustomerManager.UpdateCustomer(oCustomer);

                    //update the membership tables
                    Membership.UpdateUser(oCustomer);

最后一行Membership.UpdateUser(oCustomer)上的错误,因为基类中的ProviderUserKey为null。

任何想法如何填充基类,请记住ProviderUserKey是readonly?

代码示例(我的代码已修改)将非常有用和赞赏。

1 个答案:

答案 0 :(得分:1)

你做不到。 Readonly表示您无法设置它,您只能读取其值。

获取正确填充的MembershipUser可以更新的唯一方法是Membership.GetUser,它将返回默认MembershipUser类的实例,而不是您的实例自定义类。要执行您要执行的操作,您需要创建一个返回MembershipProvider而不是Customer的自定义MemberhipUser,但这可能比它的价值更麻烦。我发现更好的方法是让Customer“换行”MembershipUser而不是扩展它。我已经完成了两种方式(创建自定义MembershipProvider并在MembershipUser周围创建包装器),并且使用包装器绝对是更简单的方法:

public class Customer : MembershipUser
{
    #region Properties

    private MembershipUser _user;

    ....

    #region Load

    public static Customer Load(IDataReader iDataReader) 
    {
        Customer oCustomer = new Customer();
        ...

        if(!string.IsNullOrEmpty(oCustomer.UserIdAspNet))
        {
            oCustomer._user = Membership.GetUser(Guid.Parse(oCustomer.UserIdAspNet));
        }

        return oCustomer;
    }
}

然后,不是隐藏只读属性,而是实际使用它们。将Customer.IsLockedOut从true更改为false意味着需要执行某些操作,因此在Customer.IsLockedOut属性中,不要只存储一个不起任何意义的值,而是调用{{1} }:

MembershipUser.UnlockUser()

如果您不希望在设置属性时运行该属性,则需要跟踪值是否已更改,并从public bool IsLockedOut { get { return _user.IsLockedOut; } set { if(value != _user.IsLockedOut) { if(value) { // There's no way to programatically lock out a user, so either throw an InvalidOperationException or ignore it } else { _user.UnlockUser(); } } } } 方法调用UnlockUser()