Sunday, March 25, 2007

Automate your Build and Packaging Process

Check this out great toolset for automate your build and packaging process

Wix ToolSet
Tips and Tricks
Learn MSBUILD

Thursday, March 22, 2007

Beauty Of .NET (Extender Controls)

Have you ever wanted to extend functionality of .NET controls ? if so first thing you will do is subclasing the control and implement your functionality. Hmm there is a much more elegant way of doing this by using Extender Controls.

Extender controls allows you to extend functionality of exiting controls in a form without subclasing. You have to only drag the extender control to the form and there you go ..... all the controls in the form are equipped with the new functionality.

The basics are very simple, and you have to build the rest according to your needs. End of this article I have presented some interesting things that can be done using this technique. Extender control is a component so you have inherit your class from a component and implement the IExtenderProvider interface.

[ProvideProperty("HelloWorld",typeof(System.ComponentModel.Component))]
public class HelloWorldComponent : Component,IExtenderProvider

IExtenderProvider interface is very simple. You have to only implement the CanExtend method.

public bool CanExtend(object extendee)
{
if (extendee is Control && !(extendee is HelloWorldComponent))
{
return true;
}
else
{
return false;
}
}

The above will extend all the controls in a form (win or web) with properties extended from extender control. You can introduce one to many properties to controls in a form using one extender control. For introduce a property you have implement two methods Set(Component,String) and Get(Component). ProvideProperty attribute gives names of the properties. Pls refer Extender Controls Tutorial for more details on creating and using extender controls.


Interesting things you can do with Extender Controls

1) UI Security Implementations
2) Two Way Data Binding in ASP.NET for simple controls like text boxes and radio buttons

Have fun.........

Friday, March 09, 2007

Robust Coding with CBO's and Hydrator

I thought of giving some of my thoughts on DAL and BL designing. Some buzz words today among designers are DTO's and Business Objects (BO's). DTO Vs BO discussion is somewhat controversy and I'm not going to touch that here :). DTO's are smiler to CBO's (Custom Business Objects) that I'm talking in this article.

First I'll refer to a Framework that use CBO's and hydrator design pattern. DotnetNuke is a open source Content Management and Portal development framework (http://www.dotnetnuke.com/). It has a large community and fairly stable product. We have used this for couple of commercial products and customers are happy so do we. The complete data layer of this solution is built on CBO's and a Hydrator. This greatly reduces the size of the code and it's complexity.

If you are using CBO's in your code just think how are you going to populate those objects using a DaraReader. If you are going to hand code all the filling mechanisms definitely it's going to be a nightmare. Just think the the maintenance effort, if you change something in the data model you have to go and change the filling codes as well. You can greatly reduce this complexity by using a hydrator or a helper class with CBO's to automatically fill your CBO's based on reflection.

Few hints on how to write a flexible helper class (Hydrator)

  • Use .NET 2.0 Generics to define the FillCollection method

public static C FillCollection(Type objType, IDataReader dr)
where T : class, new()
where C : ICollection, new()

By using generics you can generalize the method to use custom return collections as well as to define concrete objects to be filled at compile time. What I love about this is you can specify generic constraint using where clause.

C# supports five different constraints:


  1. Interface constraint - The type argument must implement the specified interface.
  2. Inheritance constraint - The type argument must derive from the specified base class.
  3. Class constraint - The type argument must be a reference type.
  4. Struct constraint - The type argument must be a value type.
  5. New constraint - The type argument must expose a public, parameterless (default) constructor.

In the above
"where T : class, new()" means the object should be a reference type and it should have a parameterless constructor


  • Use Custom Attributes to map object properties to DB columns and to define custom values to null properties. So you don't have to use the same DB column names in your object properties and you can override default null values.
Download Code

Saturday, February 24, 2007

Google Apps

My friend Hasith one day showed me this interesting concept of google docs. Though I was not that interested on it, Today i thought of just looking at it. I came across this online Google Application concept it's not just Google docs, it has different application packages for home users, small business users, enterprise users etc. It's interesting concept ah! Everything is 100% free :) only requirement is you have to have a domain name to get registered into Google apps. Before I was using MS word to write my blog posts today I'm using Google Docs Wow. The great benefit I'm getting is I can write my documents if I can get hold of a computer and Internet connection. I don't have to wait till I come home or to the office. This is a hassle free powerful communication & collaborative platform for all of us enjoy!

Tuesday, January 09, 2007

Connection Strings

Guys who want's to refer connection strings here is the site http://www.connectionstrings.com/

Thursday, January 04, 2007

.NET Documentation Compilers

When Microsoft introduces XML documentation I was wondering why Microsoft didn’t introduce a compiler to compile this XML documentation into nice looking API’s. Hmm I don’t have an answer for it but some open source guys came up with a good tool (NDOC) for this need. Guys who have the experience in NDOC know what I’m talking about. If you don’t know about NDOC have a look at http://sourceforge.net/projects/ndoc/

Now the good news is Microsoft have open up there internal MSDN style XML documentation tool to the community. All who are interested in code / API documentation look at SandCastle. http://blogs.msdn.com/sandcastle/ is a good place to hang around. Download it from http://www.microsoft.com/downloads

SandCastle is purely command prompt base set of tools and compilers. If you like to get a technical overview of the tool have a look at this blog

https://blogs.msdn.com/sandcastle/archive/2006/07/28/681209.aspx

Some guys have written a NDOC style application in top of SandCastle tools and can be found at http://www.codeplex.com/SHFB

Enjoy !

Friday, October 27, 2006

Change Your Destiny....

Last 3 days I attended the Microsoft Tech.ED 2006 held at Waters Edge Sri Lanka. It was a good experience but bit pissed off b'cos the expected contents are not delivard in this 3 days of sessions. I'm not expected marketing presentations in a TechED forum. Do you? Some of the speakers new what they are talking about (technically fluent) but that is less than 40% of them and the rest are Marketing guys.

Actually I loved the concept of "People Ready" software which I believe futuristic concept. It shows the maturity of the IT industry. Big thanks go to Microsoft for addressing this and I hope Microsoft will think backward compatibility issues and not to discontinue their tools and platforms as they wish b'cos many is going to suffer from it.

Thursday, June 29, 2006

ADO.NET Data Reader Vs DataSet

ADO.NET Data Reader Vs DataSet

For a long time I was thinking of maintaining my own blog, but unfortunately I couldn’t find time for it since I’m always busy with my office work.

Though I’m enjoying my daily life style very much, I always feel I’m not sharing my thoughts and knowledge with others. So here is my first writing in the area of ADO.NET.

Let’s start

When I first heard the name ADO.NET I thought this is the next version of classic ADO. But I was wrong it’s totally new set of components that are optimized to the internet world.

ADO.NET provides two options to retrieve data from an underlying data source. Those are: DataSet and

DataReader

The above are exposed through different providers in ADO.NET. Data providers in .NET framework are used to connect to a database execute commands and retrieve data. This resides as a thin layer between the code base and the data source which optimizes the connectivity.

Lets start with DataReader

There are number of Data Providers in ADO.NET which has its own implementations of DataReader. Commonly they all have implemented the IDataReader interface. If you are sending out the DataReader from a method you can send it out as IDataReader which wraps the actual implementation which solves the problem of many actual implementations when passing the DataReader around different layers of your design.

DataReader is a one way forward only, read only method to read data from a result set. Once you get data into a DataReader it’s very important to remember that the pointer is just before the first record in the result set. You have to call .Read() which moves the pointer to the next location in the result set and returns “true” if it is a valid row.

Common way of reading data from a DaraReader is as follows:

SqlDataReader reader = command.ExecuteReader();

If(reader.HasRows)
{

while(reader.Read())

{
…..
}
}

DataSets

DataSets are database independent in memory data store which can be used to access one or many tables. It can contain one or many DataTables and these tables can contain rows and columns of data.

Data within DataSets can be updated by the user and can persist to the underplaying data store through DataAdapters. DataAdapters are database specific implementations in DataProiders.

Which to be used?

If you need one way read only access to data that we often need in ASP.NET DataReader makes sense. ASP.NET is a stateless technology and you will not hold any data or database connections between requests. ASP.NET allows holding data using session state but it’s not the ideal model to hold custom DataSets between requests and will not make your solution scalable.

The bad side of DataReader is it keeps the database connection open till you finish entire processing of your data. So keep in mind if you have any long processing requirements DataSets may be ideal.

For windows and Smart client applications DataSets are ideal since they maintain their state whole through its life time. The dark side of using DataSet is it holds the entire data store in memory. This may cause high memory requirements if you are loading thousands and millions of data into these DataSets. Data readers are ideal in such cases.

Some Key Points

If you are passing a DataReader around your logical layers there is an inherent problem of closing the Database connection. This is one of the mistakes developers do in their developments which causes application performance and scalable issues. This issue can be solved by using the CommandBehavior.CloseConnection parameter with the ExecureReader which automatically close the database connection when closing the DataReader.

command.ExecuteReader(CommandBehavior.CloseConnection)

DataSets can be easily converted into XML and vise versa, this can be very useful if you want to serialize data across the wire (.ReadXML, .WriteXML methods can be used).

DataSets are accepted by ASP.NET web services and can return DataSets as well. You must be thinking this is strange, though you think it’s limits usability of you web service, it might be a good reason to use those features if you know all your web service consumers are .NET.