Denny.NET

I can haz ASP.NET goodness?

About the author

Denny Ferrassoli
Developer at Casting Networks. MCP / .NET
E-mail me Send mail
Add to Technorati Favorites

Recent posts

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Extending SubSonic ActiveRecord

I was looking to refactor my User Management class to use SubSonic to do all the DAL processing and I decided to take advantage of SubSonic's Partial Classes to extend Extend!upon my "Account" ActiveRecord and merge my User Management class into the partial class. Note: I'm not referring to the AccountCollection object.

To be completely honest I'm not sure if this is the best way to handle this type of thing but thought it would be a fun exercise if nothing else.

SO let me share with you the code. Some of you may look at it and laugh, some may think it's really cool. Either way I'm throwing this out there so that whatever I learn as a result will help me (and hopefully you).

First lets take a look at the Account Partial Class that SubSonic generates:

[code:c#]
[Serializable]
public partial class Account : ActiveRecord<Account>
{
 ...
}
[/code]

Now there's nothing really important here other than the fact that it's a "Partial Class." So moving on let's see how we extend the class with one of our very own methods: LoginUser().

First we'll look at the code:

[code:c#]
namespace DAL {
public partial class Account
{
 public void LoginUser()
 {
  this.LastLogin = DateTime.Now;
  this.LoginHash = new Guid().ToString(); 

  HttpCookie c = new HttpCookie( "LoginCookie" );
  c.Expires = DateTime.Now.AddDays( 5.0f );
  c.Values.Add( "lastlogin", this.LastLogin );
  c.Values.Add( "loginhash", this.LoginHash ); 

  this.Save( "app-ext-user" );
  HttpContext.Current.Response.Cookies.Add( c );
 }
}
}
[/code]

Now what's really important is how our class is defined. Notice that our class is also a "Partial Class" and it has the same class name as the SubSonic generated class. It's also important to note that our class is defined within the same namespace as the SubSonic generated code - leaving out the namespace will not give you access to the Account constructors. Note: My previous code was inheriting from DAL.Account which was not necessary as pointed out by Kevin in the Comments section.

Within our class definition we have created the LoginUser method. This is the method we're creating to extend the Account class - in other words the method did not exist in the original SubSonic generated Account class.

Within the LoginUser method we use "this" to access the object's properties. "this" is pretty cool because it refers to the currently instantiated Account object (including our newly added method). However you should note that using "this" is not required... you can access the properties, methods, etc... simply by their name. (Kevin - Comments)

So we set a few properties: LastLogin and LoginHash, create a cookie, then we use the Save method to save our changes and also add our newly created login cookie to the Cookies collection.

We've now extended the original SubSonic ActiveRecord class with our LoginUser() method. Give it a shot:

[code:c#]
Account a = new Account(acctid);
if( a.IsLoaded ) a.LoginUser();
[/code]

This is a great way to extend a single SubSonic ActiveRecord with well defined functionality.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Currently rated 4.0 by 2 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,
Categories: ASP.NET | Server-Side
Posted by Denny on Monday, October 29, 2007 3:15 PM
Permalink | Comments (5) | Post RSSRSS comment feed

Comments

DotNetKicks.com

Tuesday, October 30, 2007 4:28 PM

trackback

Trackback from DotNetKicks.com

Extending a SubSonic ActiveRecord

Kevin Isom nz

Tuesday, October 30, 2007 7:05 PM

Kevin Isom

Why are you using inheritance instead of the partial class? You can just specify public partial class Account {} and you will add your methods and properties to the Account object. Also you don't need to use "this." unless there is a naming conflict.

Denny Ferrassoli

Tuesday, October 30, 2007 9:14 PM

Denny Ferrassoli

Hey Kevin, good point! I wrote the code as an example but from within a content page that was "using DAL;" -- oops! I therefore created a naming conflict and added the DAL.Account inheritance without even realizing it. I'll make a note about this on the post Smile Thanks!

Willie Tilton us

Monday, November 26, 2007 12:40 PM

Willie Tilton

I do the same thing. My generated files are in the Generated folder, and my own classes that do what yours are doing above are in the root of a ProjectName.Data type of project. While it seems more of like a controller class, I'm still doing the same as you. I suppose you could do a partial AccountController class...but that's more typing.

Rye gb

Wednesday, March 05, 2008 1:44 PM

Rye

Thanks for this blog post - it will help a lot with a project Im working on. Have you had any experience in extending the DataTable that SubSonic generates? I have a partial class (MyNamespace.MyClass) to which I would like to add several new non-database columns. I cant find any information on how to do it.

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading