I just came across a blog from Fredrik Normén talking about Dependency Injection and Mock object. I have been using those a lot in my recent projects. Fredrik’s blog gives me a very intertesting idea how to organize and test my application. Currently I seperates the test project from the production project. After I tested my DAL and BLL, then integrate them together into main project. Now I am thinking about to Mock BLL and DAL and inject them into web/Wind project, so that I can test the whole application without touching database.

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? :=)

I use NAnt recently to create build files for my current project. One of the issue I found is how to edit the NAnt build file with intellisense. Actually we can take advantage of the XML schema file come with NAnt to put intellisense feature into Visual Studio.NET. Here is the process:
1. After unzipping NAnt, look for schema folder. Copy nant.xsd into your visual studio .net installation folder, C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Packages\schemas\xml

2. Open nant.xsd and find out the defined namespeace, which is “
xmlns=http://nant.sf.net/schemas/nant.xsd“.

3. Add this name space into NAnt build fild, like that:

4. In visual Studio, right click NAnt build file, select open with “HTML/XML Editor (Default)”.

5. All set. You should be able to use intellisense now.

In my recent projects, I am extensively using Model-View-Presenter (MVP) pattern in the presentation tiers. It easily seperates the responsiblities, and makes my testing easily. Jeremy Miller has two great blogs recently talking about MVP, Unit testing and Regression testing in ASP.NET. You can check it from here and here. He also told about using FitNesse for regression testing, which makes me very interested. I am going to put it on my to-do list.

As far as MVC pattern, I still don’t find a better solution yet. Althought Microsoft claim its ASP.NET codebehind is a controller, I just don’t think so. Castle has a implementation of MVC, but still in its sanbox. The NPetShop2, another implementation of PetShop, from iBatis is heavily using this implemenation. But still, it is in the sanbox. If you have interest, you can access their source code to take a look at. Microsoft has its own UIP (User Interface Process) building block. But I need to customize the source code to make it useful to me, since I already has a BasePage for my ASP.NET application to store common functions. I am keeping looking at…

I am going to keep listing all of the tools and framework I am using or studying in my daily base. I am big fun of open source projects. I have to salute those great developers. They provide not only the tools which help others to speed up their work, but also the opptunties to share and learn the code each others. I have learned a lot and am keeping learning. It is a wonderful journey for me. I will write down some of my usage experience with those wonderful tools.

In my latest project, I am using Castle ActiveRecord as my ORM. I am very happy with the productivity it provides. Castle ActiveRecord is inspired by ActiveRecord in Ruby on Trail. It is built upon nHibernate, a excellent OR mapping tool ported from Java Hibernate. ORM is still a pretty new topic in .NET development. A lot of .NET developers are not comfortable with ORM becasue of dynamic SQL, worrying about its performance. But from my personal experience, I found the performance lag is from reflection most of time, rather than dynamic SQL. From a application development standpoint, performance is one, but not the only factors we need concern. We need to balance performance, maintainability, productivity, etc. From my perspective, I cannot find a good Application Architecture without taking any advantage of OR mapping. Castle ActiveRecord is excellent framework with Null-handler, validation, lazy loading and other great features built. It saves me significant time to build similar things from scratch. I will keep posting what I learn from ActiveRecord, and other O/R mapping, and how to build solid application based on those framework.