DennyDotNet

Awesome ASP.NET C# and other cool coding stuff

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

Categories

None


Disclaimer

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

© Copyright 2010

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:


[Serializable]
public partial class Account : ActiveRecord<Account>
{
 ...
}

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:


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 );
 }
}
}

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:


Account a = new Account(acctid);
if( a.IsLoaded ) a.LoginUser();

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

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Tags: ,
Posted by Denny on Monday, October 29, 2007 10:15 AM
Permalink | Comments (5) | Post RSSRSS comment feed

Removing an Item from a SiteMap Menu at Runtime

Yesterday I was implementing a new SiteMap menu and had to apply a boat load of business rules to the menu. At first I thought I could hide certain menu items by using Roles. However I quickly realized that not only do I have to apply these business rules based on roles but certain configuration settings as well, so roles would not work. Since the SiteMap file is essentially a static XML file I really didn't want to do any XML parsing. I felt it would be far too expensive to edit the XML in memory. So finally this morning I found what I needed. A simple way to remove items from a sitemap.


protected void MasterMenu_MenuItemDataBound( object sender, MenuEventArgs e ) {
     if( e.Item.Text == "Account" ) {
         MasterMenu.Items.Remove( e.Item );
     }
}
 

The only issue I have with this method is that I have to run through my business logic for every item in the menu.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posted by Denny on Thursday, October 18, 2007 3:47 AM
Permalink | Comments (0) | Post RSSRSS comment feed

The ASP.NET MVC Framework...

Finally some great news! Scott Gu officialy announced the ASP.NET MVC Framework on his blog. I'm so anxious to get my hands on the framework! If you're not familiar with MVC I would suggest reading Scott's post, he explains it briefly.

However there are some really cool features... for example you can use both MVC and WebForm frameworks. You can also continue to use Master Pages, ASCX controls, <%=%> snippets and plenty more. The MVC framework will not use the existing post-back model, instead you route all end-user interaction to a Controller (no viewstate or page lifecycle - a great approach).

What about ASP.NET AJAX UpdatePanels? Scott mentions this in his comments:

UpdatePanel does use postback, so you won't use that control directly within a MVC based view.  But there will be a control (and optional helper method) with capabilities very similar to it.  It will invoke an action on a controller and allow you to incrementally update a portion of HTML really easily.  It will enable you to use the ASP.NET AJAX libraries really easily.

Curious to see the "optional helper method."

My biggest frustration has been with the difficulty in mashing JavaScript with ASP.NET WebForm's rendered output. With the MVC Framework it seems like I'll finally have the control I want to use JavaScript frameworks and server-side controls seamlessly.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Tags: , ,
Posted by Denny on Monday, October 15, 2007 3:35 AM
Permalink | Comments (2) | Post RSSRSS comment feed

SubSonic QueryCommand Object

The QueryCommand object in SubSonic makes writing SQL Commands a whole lot easier and less time consuming. When would you use it? Well sometimes the Query object can't perform complex queries, especially ones involving JOINS. So the QueryCommand is an excellent object to use when you need to run a complex query. Below is an example:


// The SQL (notice I am using parameters: @projectid)
string cmd1 = "SELECT ListName FROM tList a INNER JOIN tMain b ON b.tId = a.tId WHERE a.tId = @projectid";

// The QueryCommand object...
// SubSonic knows which connection to use based on the provider name “Master” which is defined in Web.Config
// Or leave it blank if you have a default provider selected (also defined in Web.Config)
QueryCommand q = new QueryCommand( cmd1, "Master" );

// Set the parameter(s)
q.AddParameter( "projectid", intProjectId, DbType.Int32, ParameterDirection.Input );

// Execute the command
string strValue = Convert.ToString( DataService.ExecuteScalar( q ) );

// You use the DataService object to execute your QueryCommand object.
// There are several methods you can use with the DataService object including:
//
// DataService.ExecuteQuery(...);
// DataService.ExecuteTransaction(...);
// DataService.GetDataSet(...);
// DataService.GetReader(...);

So in 4 lines of code (subtracting comments and breaks) you can run a custom SQL command. Thanks SubSonic!

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posted by Denny on Thursday, October 04, 2007 6:43 AM
Permalink | Comments (1) | Post RSSRSS comment feed