{STATIC} hippo

...on becoming a great developer...
posts - 38, comments - 12, trackbacks - 0

My Links

Archives

Post Categories

ASP.NET Strongly Typed Profile

I'm not a huge fan of the out-of-the-box functionality of ASP.NET Profiles.  I really dislike the way everything is is just serialized as a blob and there's no out-of-the-box alternative.  That aside, I'm using Profiles for an ASP.NET MVC site right now and wanted a way to strongly type the data (and remove the need for string literals).  Here's what I ended up with:

   1: public class StronglyTypedProfile
   2:     {
   3:         private ProfileBase profile;
   4:  
   5:         public int MerchantId
   6:         {
   7:             get
   8:             {
   9:                 object ob = profile.GetPropertyValue("MerchantId");
  10:                 if (ob == null)
  11:                     return -1;
  12:                 return (int)ob;
  13:             }
  14:             set
  15:             {
  16:                 profile.SetPropertyValue("MerchantId", value);
  17:             }
  18:         }
  19:  
  20:         public string ForumNick
  21:         {
  22:             get { return (string)profile.GetPropertyValue("ForumNick"); }
  23:             set
  24:             {
  25:                 profile.SetPropertyValue("ForumNick", value);
  26:             }
  27:         }
  28:  
  29:         public string FirstName {
  30:             get
  31:             {
  32:                 return (string)profile.GetPropertyValue("FirstName");
  33:             }
  34:             set
  35:             {
  36:                 profile.SetPropertyValue("FirstName", value);
  37:             }
  38:         }
  39:  
  40:         public string LastName
  41:         {
  42:             get
  43:             {
  44:                 return (string)profile.GetPropertyValue("LastName");
  45:             }
  46:             set
  47:             {
  48:                 profile.SetPropertyValue("LastName", value);
  49:             }
  50:         }
  51:  
  52:         public void Save()
  53:         {
  54:             profile.Save();
  55:         }
  56:  
  57:         private StronglyTypedProfile() { }
  58:         public static StronglyTypedProfile Create(string username)
  59:         {
  60:             StronglyTypedProfile p = new StronglyTypedProfile();
  61:             p.profile = ProfileBase.Create(username);
  62:             //profile = ProfileBase.Create(username);
  63:             return p;
  64:         }
  65:     }


Now I can use it like so:

   1: StronglyTypedProfile profile = StronglyTypedProfile.Create(Username);
   2: profile.FirstName = u.FirstName;
   3: profile.LastName = u.LastName;
   4: profile.Save();


Very simple, and it works.

Print | posted on Tuesday, October 28, 2008 1:08 PM |

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 7 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET