Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Favourite new features in .NET 2.0
Inspired by Jeff's post, i thought I'd talk about some useful things I do with .NET 2.0 on a daily basis.

Let's start with lists of generics. This is a great advance in programming. As well as being a powerful collection object, they can also be easily used as a datasource. All you have to do is set the DataSource property in code-behind, and declaratively set the properties you wish to use for DataTextField and DataValueField. Then call DataBind() and you're done. When I first started with 2.0, I was experimenting with using ObjectDataSource's to store simple lists like Title, Division, etc. But generic lists of Domain objects are much more flexible for this purpose. In fact, you can also make the SelectMethod return a generic list. This is useful when the object used has a property that returns a List<T>, or when you extend the ObjectDataSource and create a SelectMethod which returns a list of domain objects, generally via the Business Layer.

Dictionaries of generics are also a very powerful tool to have at your disposal. I have found that they are well suited to caching of data into more usable, organised structures; or as a way of presenting already cached data. For example, you may have a web cache containing all system user profiles, and one holding the system security groups. When the cache is constructed/refreshed you construct a dictionary hashing a list of the groups/group ids each user is a member of against the user id, or a user object. You'd then just provide a BL method to get at this - say BL.UserSecGrpsGetByUserId(int userId)

As mentioned, the ObjectDataSource (ODS) has been something of which i've been making good use of since ASP.NET 2.0 was released. You can either tie it directly to an existing domain or decorator object; or tie it to an object adapter class, in which you can construct a datatable made up of disparate source material; or you can even inherit from (ODS) and get even more power - just remember to set the TypeName property to the name of your own custom ODS type.

The good thing about ODS's is that when you create one declaratively (in the .aspx file) and then associate it with a databound control using DataSourceID="..." all the ODS delegates are automatically wired up for you. Not only this, they are set up with default event handlers. This means that if you provide a SelectMethod, an UpdateMethod, and the relevant parameters, the Edit/Update/Cancel row editing feature works right out of the box. The code in the default event handlers is actually quite simple. In the case of a GridView, It more or less handles the setting of the currently edited rowindex, and deals with the new and old values for the currently updated row. It is simple to override these handlers, like so:

...
OnRowEditing="gridViewName_RowEditing"
OnRowUpdated="gridViewName_RowUpdated"
...

If you do this, just be careful to add the necessary code which specifies the GridViewRow that's currently under edit, and subsequently which one has just been edited.

Functional programming via the use of anonymous methods/delegates. There are the obvious uses for Find, ForEach, etc.. A slightly more advanced use i've found is when performing intersections (or unions for that matter) on two List<T>'s of domain objects, you need to specify the criteria that the intersect algorithm should consider when determining whether an item exists in both lists. The nice thing you can do here is define a delegate to represent the method used to intersect - all this really does is find if there are corresponding items between the lists:

public delegate T IntersectMethod<T>(List<T> selectedList, T originalItem);

The main intersect method looks like this: 

public static List<T> Intersect<T>(List<T> elementList1, List<T> elementList2, IntersectMethod<T> findMethod) {

    ...
    // checks the return value of findMethod, and adds to returned list if not null and not default(T)

}

You then pass into the intersect method the lists to intersect, plus an anonymous delegate which matches the IntersectMethod signature, like so: 

List<myType> intersectedItems = 
    Intersect(objList1, objList2,
              delegate(List<myType> list1, myType x)
                            {
                                return list1.Find(
                                    delegate(myType y)
                                    {
                                        return x.FieldWeCareAbout == y.FieldWeCareAbout;
                                    });
                            });

And finally, Master Pages have been a godsend. Especially in that they can be combined with interfaces and base classes. Several times I've made use of an abstract base master page which is inherited by multiple master pages, say for user and admin sections. I have found this to be very powerful in terms of reducing the size of the codebase.

For the record, I agree that ASP.NET theming has a bit of a way to go in terms of becoming as good as some other areas, but there some cool features worth mentioning, these being:

  • it is easy to specify the default layout for every control in your site
  • List control skins can have default items in them, which you can then augment in the actual controls by setting AppendDataBoundItems to true. 

Posted 09-26-2006 12:42 AM by Joe Niland
Filed under: ,

[Advertisement]

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)