Inheriting From MembershipUser - Asp.net/C#
I have a customer class that inherits from MembershipUser.
When i load a Customer object i want to be able to set all the values in the base class easily.
I try to declare properties of Customer that hide readonly MembershipUser properties such as ProviderUserKey and then set those properties, however the base class properties are null when i need them to be set.
here is my class 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开发者_JAVA百科.LastLoginDate;
oCustomer.LastPasswordChangedDate = oMembershipUser.LastPasswordChangedDate;
oCustomer.PasswordQuestion = oMembershipUser.PasswordQuestion;
oCustomer.ProviderName = oMembershipUser.ProviderName;
oCustomer.ProviderUserKey = oMembershipUser.ProviderUserKey;
oCustomer.UserName = oMembershipUser.UserName;
}
return oCustomer;
}
#endregion
}
}
Now when i try to execute the following code i get an error because the base class ProviderUserKey is 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);
it errors on the last line Membership.UpdateUser(oCustomer) because ProviderUserKey in the base class is null.
Any Ideas how to populate the base class, bearing in mind ProviderUserKey is readonly?
Code samples (my code modified) would be really helpful and appreciated.
You can't. Readonly
means you can't set it, you can only read its value.
The only way to get a properly populated MembershipUser
that you can update is through Membership.GetUser
, which is going to return an instance of the default MembershipUser
class, not an instance of your custom class. To do what you're trying to do, you would need to create a custom MembershipProvider
that returns a Customer
instead of a MemberhipUser
, but that's probably more trouble than it's worth. What I've found works much better is to make Customer
"wrap" MembershipUser
instead of extending it. I've done it both ways (creating a custom MembershipProvider
and creating a wrapper around MembershipUser
), and using a wrapper is definitely the easier approach:
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;
}
}
Then instead of hiding the readonly properties, actually do something with them. Changing Customer.IsLockedOut from true
to false
means something needs to happen, so in your Customer.IsLockedOut
property, instead of just storing a value that doesn't mean anything, call 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();
}
}
}
}
If you don't want that to run when you set the property, then you would need to track whether the value changed, and call UnlockUser()
from your CustomerManager.UpdateCustomer
method.
精彩评论