Monthly Archives: April 2006

I have implemented IOC Castle Container in my current project, which is great experience. As I mentioned before, I have used Castle Active Record in my recently project. AR, with NHibernate under its cover, is definitely improving my productivity. I helps me concentrate on core business logic without worrying about data access details. But there is one thing always bugs me is that I have to declare transaction in data access layers, since I want to maintance a clean business layer without considering any real implementation of data access layers. Any data access logic should be transparent, which means I can replace any other data access implementation without problems. for example, I can switch my nHibernate implementation with Microsoft Enterprise Library or IBatis. In my old design, I heavily utilized data access layers to manage data transactions, so that it becomes bigger and bigger with some business intelligence input which should not be there.

Furtunately, Castle Container is a very open IOC implementation. It allows different components easily to “plug and play” with same interface implemented. With their sample and the help of user forum, I make the container in place in couple hours. Then I implemente Castle transaction manager, remove the transaction from my data access layer, move the real business logic back to business tier. Now my code is much better with clear responsiblity in each layer. My next step is to modify my Active Record as a “Facility” for the container, then i can create a “Enterprise Library” as another “Facility”. Some developers in my group always argue about performance impace with “stored procedure” and without it. I just don’t believe “stored procedure” is a critical issue. So I like to benchmark it side-by-side. I am a real ORM guy. You will see how nice the application architecture is when implementing ORM properly. But unfortunately, most of the Microsoft developers still don’t notice that. “Stored procedure” is a immutable rule. Pretty sad.

Anyway, there have two real time applications implemented with Castle Container I can recommend to you to look at.
1. Cuyahoga, a CRM application using nHibernate with nice Domian/Repository pattern implemented.
2. NPetShop2, a sample application come with IBatics. It not only demostrates the usage of IBastics but also integrates with a true .NET MVC framework which is one of the Castle project as well.

One of the great technical books which inspired me a lot is Rod Johnson’s “Expert One-On-One J2EE Design and Development”. In this book, Rod laid out couple very important application design principles. Two of them are:

  • OO design is more important than any implementation technology.
  • Testability is essential.

Those principles are not only for J2EE development, but also can be applied for most any application development regardless platforms. I found too many .Net developers ignore those valuable guidelines. You probably can see “spinetti” code almost everywhere. Data access logic is tangled in ASP codebehind, or business logic layers are mixed with data access codes. I think probably one of the reasons is coding habits inherited from VB, which very easy leads to procedure code. And Microsoft’s strong encourage to use stored procedure for “improving performance” is probably another one. Writing software is really a maintenance job. In my poin to of view, maintainability is most important factor in development process. Don’t getl me wrong, I still think performance, scability and any other abilities are very important. But maintainability is more critical. It will help improve developers’ productivity by reusing the code base, easy to change funtions to meet the business requirements. It at best improves ROI. I will save OO and architecture design in later, and only talk about some of testing framework which can help .NET developers improve testabilities in their code.

Before you go even further, I have to recommend you to read a excellent article by Justin Gehtland, “10 ways to make your code more testable“. To write readable code is a skill. It cannot easily be achieved by any testing tools, even the new version of Visual Studio .NET. It has learning curve, it requires developers to apply OO principles and patterns in the application design and development. I will discuss TDD and how OO can help to do TDD in another post.

Here are some of the unit testing framework I found and using currently, and they approve great benefits for my development. From my personal usage experice, I categories to three types to corresponding to three tiers development enviroment.

- Data Access Layer

Basically DAL is responsible for Create/Retrieve/Update/Delete (CRUD). There have some arguments that if the unit test should touch database. My option is clear, we should. One of the practice is to restore the database every time before unit testing. It is easy to be done by NAnt. There have couple materials showing how to do that.

1. CodeProject Continuous Database Integration

2. 15 Seconds Automated Backup wiht NAnt

3. Thycotic Software .NET TDD Starter

NUnit testing is the classic and very common one. I used it in my TDD early age, until I find MbUnit coming with TestDriven.NET. MbUnit actually extends the basic function of NUnit and add a lot nice features, for example RowTest which can take testing data row by row as the parameters, or RollBack feature which can do database roll back with COM+ 1.5. Rollback is a great feature to clean the testing data after unit testing. Another thing I like to mention is make sure to download TestDriven.NET. It is a such nice testing framework integreated with NUnit, MbUnit, and visusal studio .net 2005 testing framework. Besides that, it has integrated with NCover and NCoverExplorer. I cannot explain how nice and how much helpful those tools are. It shows very clear my unit testing coverage by highlighting the source code. It also can generate html reports to show the result.

In the next part, I will discuss business component testing and UI testing. Stay tone :=)

MVC is a very common pattern used in OO design to break the responsibilities into different components, Model, View and Controller. It is often used in the UI layer. We can easily find in its various implementations Java or Smalltalk. But this practice is not very common for .NET developers. Maybe it is not what Microsoft recommends. Microsoft has its own concept call page controller. Basically it treats the ASP.NET code behind as the controller itself. Code in codebehind can call service layer to get Model, then display onto the aspx page. This module is working fine under Microsoft’s IDE tool, Visual studio .NET. But the problem is page controller is bound with its view. It is not easy to separate. It is impossible for me to reuse the logic in the controller into Win form. For instance, the controller has some navigation logic which can be applied onto both win form and web form.

Currently my own approach is to apply MVP pattern in very web control in the page (I prefer to use web controls on the page) to delegate the business logic into service layer, and rely on a application level navigation controller to manage the page flow. The navigation controller is some type of sate machine implementation. You can find a excellent article by Michele Leroux Bustamante in MSDN. Andy Hunt has an article regarding this topic as well. For those developers who like to implement a MVC framework, there are some of the options:

1. Microsoft UIP. UIP came with Microsoft Application Building Block 2. But it seemed not as popular as its counterparts, like data building block or log building block. Still probably because of most of the developers don’t realize how important it is to separate the responsibilities and promote testability for the whole application. Again this core implementation is inherited from Microsoft page controller concept. You have to inherit its base page (default controller). Some resources which I recommend to read for UIP

CodeProject – User Interface Process Application Block (UIPAB)

CodeProject – UIPAB Part 2: MVC, UIPAB Essentials and Demo

UIP with PetShopDNG 2.0 (French)
2. MonoRail in Castle Project. MonoRail is inspired by Ruby on Rails. It implements a truly MVC pattern. The Rails engine sit behind as a Front controller to direct the request to the right page. Combined with Active Record, which is excellent implementation upon NHibernate, MonoRail can provide a very fast and maintainable development environment. Although Web Form is one of its supported view, it is more likely use Nvelocity. So its design is not as easy as in Visual Studio, also it lacks developer community support. Maybe it will change as Ruby on Rail gains such popularity recently.

3. Castle MVC Framework. It is very interesting project hosted in Castle. But currently it still in sandbox for reviewing. It is inspired by Microsoft UIP and MVC implementation from MonoRail. Actually the lastest version of Nshop2, sample application of iBatis .NET, is using this MVC framework. Similar with other MVC framework, it uses XML to define the mapping between the views and commands. The other advantage form Castle MVC is that it can be treated as facility to be plugin into Castle IOC container.

4. Maverick.NET. It was dead for a while. But some developers just pick it up recently. It releases its 1.12 version this January. Maverick .Net uses XML to configure sitemap. Its controller supports Pages and Controls. It does not have complete documents. But it does not too hard to figure out how to use it.

Does anybody can add more? :=)