A "transactional" generic DbHelper for LINQ to SQL
In LINQ to SQL, the data model of a relational database is mapped to an object model expressed in the programming language of the developer. When the application runs, LINQ to SQL translates into SQL the language-integrated queries in the object model and sends them to the database for execution. When the database returns the results, LINQ to SQL translates them back to objects that you can work with in your own programming language. You may want to make a data access layer that separates the data operation from business layer like the following:
DbHelper.Insert<Student>(
new Student()
{
FirstName = “Tanzim”,
LastName = “Saqib”,
Email = “me@TanzimSaqib.com”,
Website = “http://www.TanzimSaqib.com”
}, true); // Use Transaction?
To make use of such transactional generic DbHelper, you might want to write a singeton DbHelper class like the following. You might notice that the class is singleton, it’s static and the DataContext is being used is private, and only initialized using the connection string if not yet.
public static class DbHelper { private const string CONNECTION_CONFIG_NAME = “StudentServerConnectionString”; private static StudentServerDataContext _StudentServerDataContext = null; public static StudentServerDataContext GetDataContext() { if(_StudentServerDataContext == null) _StudentServerDataContext = new StudentServerDataContext (ConfigurationManager.ConnectionStrings [CONNECTION_CONFIG_NAME].ConnectionString); return _StudentServerDataContext; } public static void CleanUp() { _StudentServerDataContext.Dispose(); _StudentServerDataContext = null; } // … code edited to save space
On application wide error or on end you can dispose the context so CleanUp method is useful here. To implement an Insert method see the following. You will find I have used a TransactionScope which ensures that the transaction is taking place without any interruption. If there is really any error the scope.Complete() method never gets invoked. This is how it ensures that the code inside the TransactionScope is taking place as a transaction. It is available from .NET 2.0 framework.
public static void Insert<T>(T t, bool isTransactional) where T : class { if (isTransactional) { using (var scope = new TransactionScope()) { Insert<T>(t); // On any Exception, Complete() method won’t be invoked. // So, the transaction will be automatically rollbacked. scope.Complete(); } } else Insert<T>(t); } public static void Insert<T>(T t) where T : class { using (var db = GetDataContext()) { db.GetTable<T>().InsertOnSubmit(t); try { db.SubmitChanges(); } catch (Exception e) { // TODO: log Exception throw e; } } }
I did not show other methods as part of the CRUD implementation. The rest is left open for you to implement.
[New Article] 7 ways to do Performance Optimization of an ASP.NET 3.5 Web 2.0 portal
Web 2.0 applications are widely developed. These applications often work with third party contents, aggregate them, make various use of them and then make something useful and meaningful to the users. For the past few years, developers were also engaged with such endeavors and a lot of their websites have not addressed performance issues, thus resulting in an unpleasant experience to the users.
Performance is a vast area and great results can never be achieved by a silver bullet. This article explores some of the key performance issues that can occur while developing a Web 2.0 portal using server side multithreading and caching. It also demonstrates model driven application development using Windows Workflow Foundation.
Write your own DOM friendly extension methods for HtmlElement in Volta
I know there are GetById, GetById<> methods in Document object. But, I often miss a method that I feel should be in Volta, which iterates through its child nodes and find an element for me. Let us say, there is a HTML like the following:
<div id="divContainer"> <b>Some text</b> <div id="firstDiv"> <i>Some more text</i> </div> <div id="secondDiv"> Okay, I gotta go now </div> <div anyAttribute="anyValue"> Babye </div> </div>
The most important thing is, I can not get the last div by Document.GetById, because instead of id I chose anyAttribute. So, I wrote my own extension method which can run into not only Div but also any HtmlElement, and can find me the desired HtmlElement inside the prior one with the anyAttribute and anyValue. To make my intention clear, I’d like to show how I’d like to use that extension method:
var divContainer = Document.GetById<Div>("divContainer"); var anyDiv = divContainer.Find<Div>("anyAttribute", "anyValue"); if(anyDiv != null) anyDiv.InnerHtml += "guys!";
So, I’d like to call my extension method Find<> which will take the type I’m looking for (in this case a Div) and that HtmlElement should have an attribute "anyAttribute" that contains "anyValue". Here is how I make up the extension method:
public static class HtmlExtensions { public static T Find<T>(this T parent, string attribute, string value) where T : HtmlElement { var element = parent.FirstChild; while(element != null) if (element.IsProper<T>(attribute, value)) return element as T; else element = element.NextSibling; return null; } public static bool IsProper<T>(this DomNode element, string attribute, string value) where T : HtmlElement { if (element.GetType() == typeof(T) && element.Attributes != null && element.Attributes.GetNamedItem(attribute) != null && element.Attributes.GetNamedItem(attribute).Value == value) return true; return false; } }
This method can iterate only one depth. Multi depth implementation can be done by running a simple DFS which is left to you guys. Note one thing, I have called one extension method "IsProper" inside another extension method, and there is no harm in it. So, this is how you can add your own extension methods to the HtmlElement.
Appearing on Microsoft Volta team blog
Microsoft Volta team blogged about me and one of my articles: http://labs.live.com/volta/blog/Volta+How+To+Flickr+Widget.aspx
[New Article] ASP.NET AJAX Best Practices
While we develop AJAX applications, we often carelessly ignore giving up bad practices, which cause effects which are not so significantly visible when the site is not so large in volume. But, it’s often severe performance issue when it is the case for sites that make heavy use of AJAX technologies such as Pageflakes, NetVibes etc.
There are so many AJAX widgets in one page that little memory leak issues combined may even result the site crash into very nasty “Operation aborted”. There are a lot of WebService calls, lot of iterations among collection so that inefficient coding in a whole lead to make site very heavy, browser eats up a lot of memory, requires very costly CPU cycles, and ultimately causes unsatisfactory user experience. In this article many of such issues are demonstrated in the context of ASP.NET AJAX: http://www.codeproject.com/KB/ajax/AspNetAjaxBestPractices.aspx

