The issue: ProfileGroupBase implementations in a Web Application vs. a Web Site.
The Solution: Special instance overrides using the “new” keyword on certain methods in conjunction with leaving the “group” element definitions in the web.config file.
As an example; I’ve created a UserProfile object deriving from ProfileBase, a UserProfileAddress object deriving from ProfileGroupBase and a UserProfileShippingAddress deriving from UserProfile Address. The class code in show below.
[Serializable]
public class UserProfile : ProfileBase
{
/// <summary>The user's first name.</summary>
public string FirstName { get { return GetPropertyValue("FirstName").ToString(); } set { SetPropertyValue("FirstName", value); } }
/// <summary>The user's last name.</summary>
public string LastName { get { return GetPropertyValue("LastName").ToString(); } set { SetPropertyValue("LastName", value); } }
/// <summary>The user's email address.</summary>
public string EmailAddress { get { return GetPropertyValue("EmailAddress").ToString(); } set { SetPropertyValue("EmailAddress", value); } }
/// <summary>The user's day time phone number.</summary>
public string DayTimePhone { get { return GetPropertyValue("DayTimePhone").ToString(); } set { SetPropertyValue("DayTimePhone", value); } }
/// <summary>Gets or sets the billing address.</summary>
public UserProfileAddress BillingAddress { get { return GetProfileGroupSpecial<UserProfileAddress>("BillingAddress"); } }
/// <summary>Gets or sets the shipping address.</summary>
public UserProfileShippingAddress ShippingAddress { get { return GetProfileGroupSpecial<UserProfileShippingAddress>("ShippingAddress"); } }
/// <summary>Get a special mapped instance of the profile group.</summary>
/// <typeparam name="T">The type of group.</typeparam>
/// <param name="name">The group name.</param>
/// <returns>An instance of the special mapped group.</returns>
private T GetProfileGroupSpecial<T>(string name) where T : UserProfileAddress, new()
{
T retVal = default(T);
try
{
retVal = new T();
retVal.Init(this, name);
}
catch { /* Not much we can do here, logging? */ }
return retVal;
}
/// <summary>Get the profile of the currently logged-on user.</summary>
public static UserProfile GetProfile()
{
return (UserProfile)HttpContext.Current.Profile;
}
/// <summary>The current user's profile.</summary>
public static UserProfile Current { get { return (UserProfile)HttpContext.Current.Profile; } }
/// <summary>Gets the profile of a specific user.</summary>
/// <param name="userName">The user name of the user whose profile you want to retrieve.</param>
public static UserProfile GetProfile(string userName)
{
ProfileBase profile = Create(userName);
return (UserProfile)profile;
}
}
[Serializable]
public class UserProfileAddress : ProfileGroupBase
{
/// <summary>Private reference to name.</summary>
private string _myName = null;
/// <summary>Private reference to parent.</summary>
private ProfileBase _parent = null;
/// <summary>Called by the parent when creating.</summary>
/// <param name="parent">The parent.</param>
/// <param name="myName">The group name.</param>
/// <remarks>Intentionally hides inherited member.</remarks>
new public void Init(ProfileBase parent, string myName)
{
if (_parent == null)
{
_parent = parent;
_myName = myName + ".";
}
}
/// <summary>Address line 1.</summary>
public string Line1 { get { return GetPropertyValue("Line1").ToString(); } set { SetPropertyValue("Line1", value); } }
/// <summary>Address line 2</summary>
public string Line2 { get { return GetPropertyValue("Line2").ToString(); } set { SetPropertyValue("Line2", value); } }
/// <summary>City name</summary>
public string City { get { return GetPropertyValue("City").ToString(); } set { SetPropertyValue("City", value); } }
/// <summary>State ID</summary>
public int State { get { return Convert.ToInt32(GetPropertyValue("State")); } set { SetPropertyValue("State", value); } }
/// <summary>postal code</summary>
public string Zip { get { return GetPropertyValue("Zip").ToString(); } set { SetPropertyValue("Zip", value); } }
/// <summary>Country ID</summary>
public int Country { get { return Convert.ToInt32(GetPropertyValue("Country")); } set { SetPropertyValue("Country", value); } }
/// <summary>Gets the value of a named property.</summary>
/// <param name="propertyName">The property name.</param>
/// <returns>The value of the named property.</returns>
/// <remarks>Intentionally hides inherited member.</remarks>
new public object GetPropertyValue(string propertyName)
{
return this._parent[this._myName + propertyName];
}
/// <summary>Sets the value of a named property.</summary>
/// <param name="propertyName">The name of the property.</param>
/// <param name="propertyValue">The value to set.</param>
/// <remarks>Intentionally hides inherited member.</remarks>
new public void SetPropertyValue(string propertyName, object propertyValue)
{
this._parent[this._myName + propertyName] = propertyValue;
}
/// <summary>Gets or set the value of a named property.</summary>
/// <param name="propertyName">The property name.</param>
/// <returns>The value of the named property.</returns>
/// <remarks>Intentionally hides inherited member.</remarks>
new public object this[string propertyName]
{
get
{
return this._parent[this._myName + propertyName];
}
set
{
this._parent[this._myName + propertyName] = value;
}
}
}
[Serializable]
public class UserProfileShippingAddress : UserProfileAddress
{
/// <summary>The recipients first name.</summary>
public string FirstName { get { return GetPropertyValue("FirstName").ToString(); } set { SetPropertyValue("FirstName", value); } }
/// <summary>The recipients last name.</summary>
public string LastName { get { return GetPropertyValue("LastName").ToString(); } set { SetPropertyValue("LastName", value); } }
}
The web config is relatively straigt forward but ONLY includes definitions for groups. Including anything else will cause duplicate definition exceptions.
DerikJ
Member
30 Points
6 Posts
SOLVED: ProfileGroupBase implementations through ProfileBase in a Web Application vs. a Web Site.
Dec 17, 2009 11:33 PM|LINK
The issue: ProfileGroupBase implementations in a Web Application vs. a Web Site.
The Solution: Special instance overrides using the “new” keyword on certain methods in conjunction with leaving the “group” element definitions in the web.config file.
As an example; I’ve created a UserProfile object deriving from ProfileBase, a UserProfileAddress object deriving from ProfileGroupBase and a UserProfileShippingAddress deriving from UserProfile Address. The class code in show below.
[Serializable] public class UserProfile : ProfileBase { /// <summary>The user's first name.</summary> public string FirstName { get { return GetPropertyValue("FirstName").ToString(); } set { SetPropertyValue("FirstName", value); } } /// <summary>The user's last name.</summary> public string LastName { get { return GetPropertyValue("LastName").ToString(); } set { SetPropertyValue("LastName", value); } } /// <summary>The user's email address.</summary> public string EmailAddress { get { return GetPropertyValue("EmailAddress").ToString(); } set { SetPropertyValue("EmailAddress", value); } } /// <summary>The user's day time phone number.</summary> public string DayTimePhone { get { return GetPropertyValue("DayTimePhone").ToString(); } set { SetPropertyValue("DayTimePhone", value); } } /// <summary>Gets or sets the billing address.</summary> public UserProfileAddress BillingAddress { get { return GetProfileGroupSpecial<UserProfileAddress>("BillingAddress"); } } /// <summary>Gets or sets the shipping address.</summary> public UserProfileShippingAddress ShippingAddress { get { return GetProfileGroupSpecial<UserProfileShippingAddress>("ShippingAddress"); } } /// <summary>Get a special mapped instance of the profile group.</summary> /// <typeparam name="T">The type of group.</typeparam> /// <param name="name">The group name.</param> /// <returns>An instance of the special mapped group.</returns> private T GetProfileGroupSpecial<T>(string name) where T : UserProfileAddress, new() { T retVal = default(T); try { retVal = new T(); retVal.Init(this, name); } catch { /* Not much we can do here, logging? */ } return retVal; } /// <summary>Get the profile of the currently logged-on user.</summary> public static UserProfile GetProfile() { return (UserProfile)HttpContext.Current.Profile; } /// <summary>The current user's profile.</summary> public static UserProfile Current { get { return (UserProfile)HttpContext.Current.Profile; } } /// <summary>Gets the profile of a specific user.</summary> /// <param name="userName">The user name of the user whose profile you want to retrieve.</param> public static UserProfile GetProfile(string userName) { ProfileBase profile = Create(userName); return (UserProfile)profile; } } [Serializable] public class UserProfileAddress : ProfileGroupBase { /// <summary>Private reference to name.</summary> private string _myName = null; /// <summary>Private reference to parent.</summary> private ProfileBase _parent = null; /// <summary>Called by the parent when creating.</summary> /// <param name="parent">The parent.</param> /// <param name="myName">The group name.</param> /// <remarks>Intentionally hides inherited member.</remarks> new public void Init(ProfileBase parent, string myName) { if (_parent == null) { _parent = parent; _myName = myName + "."; } } /// <summary>Address line 1.</summary> public string Line1 { get { return GetPropertyValue("Line1").ToString(); } set { SetPropertyValue("Line1", value); } } /// <summary>Address line 2</summary> public string Line2 { get { return GetPropertyValue("Line2").ToString(); } set { SetPropertyValue("Line2", value); } } /// <summary>City name</summary> public string City { get { return GetPropertyValue("City").ToString(); } set { SetPropertyValue("City", value); } } /// <summary>State ID</summary> public int State { get { return Convert.ToInt32(GetPropertyValue("State")); } set { SetPropertyValue("State", value); } } /// <summary>postal code</summary> public string Zip { get { return GetPropertyValue("Zip").ToString(); } set { SetPropertyValue("Zip", value); } } /// <summary>Country ID</summary> public int Country { get { return Convert.ToInt32(GetPropertyValue("Country")); } set { SetPropertyValue("Country", value); } } /// <summary>Gets the value of a named property.</summary> /// <param name="propertyName">The property name.</param> /// <returns>The value of the named property.</returns> /// <remarks>Intentionally hides inherited member.</remarks> new public object GetPropertyValue(string propertyName) { return this._parent[this._myName + propertyName]; } /// <summary>Sets the value of a named property.</summary> /// <param name="propertyName">The name of the property.</param> /// <param name="propertyValue">The value to set.</param> /// <remarks>Intentionally hides inherited member.</remarks> new public void SetPropertyValue(string propertyName, object propertyValue) { this._parent[this._myName + propertyName] = propertyValue; } /// <summary>Gets or set the value of a named property.</summary> /// <param name="propertyName">The property name.</param> /// <returns>The value of the named property.</returns> /// <remarks>Intentionally hides inherited member.</remarks> new public object this[string propertyName] { get { return this._parent[this._myName + propertyName]; } set { this._parent[this._myName + propertyName] = value; } } } [Serializable] public class UserProfileShippingAddress : UserProfileAddress { /// <summary>The recipients first name.</summary> public string FirstName { get { return GetPropertyValue("FirstName").ToString(); } set { SetPropertyValue("FirstName", value); } } /// <summary>The recipients last name.</summary> public string LastName { get { return GetPropertyValue("LastName").ToString(); } set { SetPropertyValue("LastName", value); } } }The web config is relatively straigt forward but ONLY includes definitions for groups. Including anything else will cause duplicate definition exceptions.
profile properties profile provider profile groups