SOLVED: ProfileGroupBase implementations through ProfileBase in a Web Application vs. a Web Site.http://forums.asp.net/t/1505400.aspx/1?SOLVED+ProfileGroupBase+implementations+through+ProfileBase+in+a+Web+Application+vs+a+Web+Site+Thu, 17 Dec 2009 23:33:32 -050015054003571784http://forums.asp.net/p/1505400/3571784.aspx/1?SOLVED+ProfileGroupBase+implementations+through+ProfileBase+in+a+Web+Application+vs+a+Web+Site+SOLVED: ProfileGroupBase implementations through ProfileBase in a Web Application vs. a Web Site. <p>The issue: ProfileGroupBase implementations in a Web Application vs. a Web Site.</p> <p>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.</p> <p>As an example; Ive 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.</p> <pre class="prettyprint">[Serializable] public class UserProfile : ProfileBase { /// &lt;summary&gt;The user's first name.&lt;/summary&gt; public string FirstName { get { return GetPropertyValue(&quot;FirstName&quot;).ToString(); } set { SetPropertyValue(&quot;FirstName&quot;, value); } } /// &lt;summary&gt;The user's last name.&lt;/summary&gt; public string LastName { get { return GetPropertyValue(&quot;LastName&quot;).ToString(); } set { SetPropertyValue(&quot;LastName&quot;, value); } } /// &lt;summary&gt;The user's email address.&lt;/summary&gt; public string EmailAddress { get { return GetPropertyValue(&quot;EmailAddress&quot;).ToString(); } set { SetPropertyValue(&quot;EmailAddress&quot;, value); } } /// &lt;summary&gt;The user's day time phone number.&lt;/summary&gt; public string DayTimePhone { get { return GetPropertyValue(&quot;DayTimePhone&quot;).ToString(); } set { SetPropertyValue(&quot;DayTimePhone&quot;, value); } } /// &lt;summary&gt;Gets or sets the billing address.&lt;/summary&gt; public UserProfileAddress BillingAddress { get { return GetProfileGroupSpecial&lt;UserProfileAddress&gt;(&quot;BillingAddress&quot;); } } /// &lt;summary&gt;Gets or sets the shipping address.&lt;/summary&gt; public UserProfileShippingAddress ShippingAddress { get { return GetProfileGroupSpecial&lt;UserProfileShippingAddress&gt;(&quot;ShippingAddress&quot;); } } /// &lt;summary&gt;Get a special mapped instance of the profile group.&lt;/summary&gt; /// &lt;typeparam name=&quot;T&quot;&gt;The type of group.&lt;/typeparam&gt; /// &lt;param name=&quot;name&quot;&gt;The group name.&lt;/param&gt; /// &lt;returns&gt;An instance of the special mapped group.&lt;/returns&gt; private T GetProfileGroupSpecial&lt;T&gt;(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; } /// &lt;summary&gt;Get the profile of the currently logged-on user.&lt;/summary&gt; public static UserProfile GetProfile() { return (UserProfile)HttpContext.Current.Profile; } /// &lt;summary&gt;The current user's profile.&lt;/summary&gt; public static UserProfile Current { get { return (UserProfile)HttpContext.Current.Profile; } } /// &lt;summary&gt;Gets the profile of a specific user.&lt;/summary&gt; /// &lt;param name=&quot;userName&quot;&gt;The user name of the user whose profile you want to retrieve.&lt;/param&gt; public static UserProfile GetProfile(string userName) { ProfileBase profile = Create(userName); return (UserProfile)profile; } } [Serializable] public class UserProfileAddress : ProfileGroupBase { /// &lt;summary&gt;Private reference to name.&lt;/summary&gt; private string _myName = null; /// &lt;summary&gt;Private reference to parent.&lt;/summary&gt; private ProfileBase _parent = null; /// &lt;summary&gt;Called by the parent when creating.&lt;/summary&gt; /// &lt;param name=&quot;parent&quot;&gt;The parent.&lt;/param&gt; /// &lt;param name=&quot;myName&quot;&gt;The group name.&lt;/param&gt; /// &lt;remarks&gt;Intentionally hides inherited member.&lt;/remarks&gt; new public void Init(ProfileBase parent, string myName) { if (_parent == null) { _parent = parent; _myName = myName &#43; &quot;.&quot;; } } /// &lt;summary&gt;Address line 1.&lt;/summary&gt; public string Line1 { get { return GetPropertyValue(&quot;Line1&quot;).ToString(); } set { SetPropertyValue(&quot;Line1&quot;, value); } } /// &lt;summary&gt;Address line 2&lt;/summary&gt; public string Line2 { get { return GetPropertyValue(&quot;Line2&quot;).ToString(); } set { SetPropertyValue(&quot;Line2&quot;, value); } } /// &lt;summary&gt;City name&lt;/summary&gt; public string City { get { return GetPropertyValue(&quot;City&quot;).ToString(); } set { SetPropertyValue(&quot;City&quot;, value); } } /// &lt;summary&gt;State ID&lt;/summary&gt; public int State { get { return Convert.ToInt32(GetPropertyValue(&quot;State&quot;)); } set { SetPropertyValue(&quot;State&quot;, value); } } /// &lt;summary&gt;postal code&lt;/summary&gt; public string Zip { get { return GetPropertyValue(&quot;Zip&quot;).ToString(); } set { SetPropertyValue(&quot;Zip&quot;, value); } } /// &lt;summary&gt;Country ID&lt;/summary&gt; public int Country { get { return Convert.ToInt32(GetPropertyValue(&quot;Country&quot;)); } set { SetPropertyValue(&quot;Country&quot;, value); } } /// &lt;summary&gt;Gets the value of a named property.&lt;/summary&gt; /// &lt;param name=&quot;propertyName&quot;&gt;The property name.&lt;/param&gt; /// &lt;returns&gt;The value of the named property.&lt;/returns&gt; /// &lt;remarks&gt;Intentionally hides inherited member.&lt;/remarks&gt; new public object GetPropertyValue(string propertyName) { return this._parent[this._myName &#43; propertyName]; } /// &lt;summary&gt;Sets the value of a named property.&lt;/summary&gt; /// &lt;param name=&quot;propertyName&quot;&gt;The name of the property.&lt;/param&gt; /// &lt;param name=&quot;propertyValue&quot;&gt;The value to set.&lt;/param&gt; /// &lt;remarks&gt;Intentionally hides inherited member.&lt;/remarks&gt; new public void SetPropertyValue(string propertyName, object propertyValue) { this._parent[this._myName &#43; propertyName] = propertyValue; } /// &lt;summary&gt;Gets or set the value of a named property.&lt;/summary&gt; /// &lt;param name=&quot;propertyName&quot;&gt;The property name.&lt;/param&gt; /// &lt;returns&gt;The value of the named property.&lt;/returns&gt; /// &lt;remarks&gt;Intentionally hides inherited member.&lt;/remarks&gt; new public object this[string propertyName] { get { return this._parent[this._myName &#43; propertyName]; } set { this._parent[this._myName &#43; propertyName] = value; } } } [Serializable] public class UserProfileShippingAddress : UserProfileAddress { /// &lt;summary&gt;The recipients first name.&lt;/summary&gt; public string FirstName { get { return GetPropertyValue(&quot;FirstName&quot;).ToString(); } set { SetPropertyValue(&quot;FirstName&quot;, value); } } /// &lt;summary&gt;The recipients last name.&lt;/summary&gt; public string LastName { get { return GetPropertyValue(&quot;LastName&quot;).ToString(); } set { SetPropertyValue(&quot;LastName&quot;, value); } } }</pre> <P>&nbsp;</P> <P>The web config is relatively straigt forward but ONLY includes definitions for groups. Including anything else will cause duplicate definition exceptions.</P><pre class="prettyprint">&lt;profile defaultProvider="MyProfileProvider" automaticSaveEnabled="false" inherits=" UserProfile"&gt; &lt;providers&gt; &lt;clear/&gt; &lt;add name="MyProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="MyConnectionString" applicationName="MyApp"/&gt; &lt;/providers&gt; &lt;properties&gt; &lt;group name="BillingAddress"&gt; &lt;add name="Line1" type="System.String" allowAnonymous="true"/&gt; &lt;add name="Line2" type="System.String" allowAnonymous="true"/&gt; &lt;add name="City" type="System.String" allowAnonymous="true"/&gt; &lt;add name="State" type="System.Int32" allowAnonymous="true"/&gt; &lt;add name="Zip" type="System.String" allowAnonymous="true"/&gt; &lt;add name="Country" type="System.Int32" allowAnonymous="true"/&gt; &lt;/group&gt; &lt;group name="ShippingAddress"&gt; &lt;add name="FirstName" type="System.String" allowAnonymous="true"/&gt; &lt;add name="LastName" type="System.String" allowAnonymous="true"/&gt; &lt;add name="Line1" type="System.String" allowAnonymous="true"/&gt; &lt;add name="Line2" type="System.String" allowAnonymous="true"/&gt; &lt;add name="City" type="System.String" allowAnonymous="true"/&gt; &lt;add name="State" type="System.Int32" allowAnonymous="true"/&gt; &lt;add name="Zip" type="System.String" allowAnonymous="true"/&gt; &lt;add name="Country" type="System.Int32" allowAnonymous="true"/&gt; &lt;/group&gt; &lt;/properties&gt; &lt;/profile&gt; </pre> <p><br> &nbsp;</p> 2009-12-17T23:33:32-05:00