Wednesday, June 16, 2010

Moving my house to weblogs

Dear everyone,
This morning I received the email from Microsoft that they just created for me the blog at I really happy about that and decided to moving my house from Blogger ( to blogs ( If anyone want to read some articles at Blogger, you still read all of it at those. I will not move all articles to weblogs, and in the future I will try to write new articles at blog. I hope anyone will support me in that. And in next time, I will try to finish the NMA project at codeplex.
Sorry for this inconvenience! 

Thursday, June 10, 2010

Cast anonymous types

Today, when coding for one module in my project at company, I had a problem that make me crazy. Used var keyword on .NET 3.5, it's a anonymous type, so when we go out its scope, we don't know its type. I think many people also had this problem like me. So after using many methods for solve this problem, I also found the best solution for it. Thanks Alex for best idea from him! Now there are some my effort for solve it:
+ First thing, I tried to coding and gave this error:
                 Cannot cast 'exampleObj' (which has an actual type of '<>f__AnonymousType0<string,string>') ...
+ Next, I started my working for solving it. Wrote the extension method for cast anonymous method:
+ Object that content the anonymous type:
Binding this object to ListView:
+ And used the extension method for cast anonymous type:
Now we can use it very normally:
+ References:

Tuesday, May 25, 2010

Implementing Service Locator pattern

Today, I will write about Service Locator pattern. Reason, because I must make sure that all thing shall run well up to now on NMA project.

The first thing, we should mention about the theory. So what is the Service Locator? In Java community, they defined it is
"The Service Locator abstracts the API lookup (naming) services, vendor dependencies, lookup complexities, and business object creation, and provides a simple interface to clients. This reduces the client's complexity. In addition, the same client or other clients can reuse the Service Locator"
But this definition also don't make you clear about Service Locator. I only give you one example as: Do you know your manager in company? Yeah, you must know him/her. But now, I suggest you a orther question, do you know about all employees in your company? Maybe you know all. But with me, I don't need to know that. When I want to find some information about the employee that work in my company, I only contact with my Human Resource Manager (HRM). So he will give me full of information about this employee. Now come back to our situation about Service Locator, you can imagine your HRM is a Service Locator. You don't need know full of information about all employees in company. He will keep all information for you, and that is his work in company.

Now if you want to know more clearly about Service Locator, you can reference to the article that had written by Martin Powler. I will give you all links at a bottom of this post.

So why do we use Service Locator? Because Service Locator make you free in your work, you don't need care about detail implementation of dependency classes. You shall assign this task for Service Locator, so you have much time to concentrate other task in your project. Service Locator will help you managed life cycle of all instances in your project (maybe its task do by Structure Map). All class only know about the contract (here is interface and abstract class), so it make your components don't couples each other. Your components shall be easy to re-use, maintain,...

Nearly 3 months ago, I read some articles said that Service Locator is a anti-pattern, hard to Unit Testing the dependency class, but I also find method to avoid it. I think you need research about Opaque dependencies and Tranparent Dependencies.

And now is the time I will show you about my work in NMA project. In MNA, I used Microsoft Service Locator that integrated with Structure Map for taking care all components in MNA. You should download the Structure Map adapter at here into your computer and try to build it. After you built successful, you are able to reference it in your solutions. In my solution, I combined Service Locator with Bootstrapper for building unify system in NMA. This is my code:
If you used to read about my Bootstrapper article, you maybe saw this code. But anyway, it's not make you disturb! I continue implemeting the concreate class as:
In this trunk of code, you only need notice some lines code as below:
I registered my Service Locator and used Structure Map adapter in this function. And that is all thing I want to shared with you. Now we only need new the Bootstrapper, so your Service Locator will register with it, and all components of own application shall be available to using.

+ References:

Thursday, May 20, 2010

Implementing the Bootstrapper for your application

Do you know about Bootstrapper?
If you don't know about it before, I will explain it now. If you used to turn on your computer, you shall see that you only are able to work when your operating system (OS) ready to booting. And underline your computer must do many works for preparing your OS. You can't see those, it only run underline. That is a bootstrapper task. You shall not be care about all task that run under OS, you only need working. Do you agree with me? Yeah! If you agree with me, you understood the jobs that Bootstrapper take it to you.
Now is the time that we come back to software world, How are we able to boot all our tasks in software (include Windows Form & Web)? And why must we run bootstrapper in our application? The second question, one articles can answer for you at here. And the first question I will answer for you after my post end. OK now we shall start to preparing all thing for implementing the Bootstrapper at below:

+ First thing, we must define the interface for the contract in each task:
+ Next, I will implement the abstract class for compositing all functions that I think I can re-use:
+ And I continue to implement sub-class for CommonBootStrapper that I just implemented:
+ Don't confuse about some messy thing in my code. Because I copied it in NMA project, so it have many things (StructureMap, Automapper, Fluent NHibernate,...) in it. But if you follow all post at my blog, I will explain all thing for you. Now continue, we shall implement one class for registering Automapper task when my application started:
Please go to for clear about AutoMapper.
Certainly, you can inplement many tasks for booting, such as setup action filter, add model binder, add custom view engine,...
+ Finally, we shall call it in Global.asax for web or App.xaml in WPF. But in this post I only implement in web:
At here, I used StructureMap for scanning 3 dlls: NMA.Infrastructure.NHibernate, NMA.Application, NMA.Infrastructure.AutoMapper for finding all tasks in my application.
I implemented scanning dll at CoreRegistry.cs, and this is the important function inside this class:
As you see this function will find all class that implemented interface IBootstrapperTask. If you implement it in any other dll, you also show it for Bootstrapper.

* And I answered for you the first question if you read all my post.
In past time, I also suggested this ideas for Deran Schilling and he also implemented very excellent post at here.
OK, I just finished the post about Bootstrapper, if you don't want to implement from scratch, you can use Unity Bootstrapper that mention at here. But it use Prism on WPF.
Finally you can find all my source code on NMA project. Good bye and see you next time!

Wednesday, May 12, 2010

Publishing a open source project on CodePlex

In 2 days later, I published NMA project on CodePlex. Now you can access to my project with URL Now I'm trying to finishing this project. I finished the all layers except for UI layer. And now I 'm designing the GUI for it.
In next post, I intend to writing the series posts about NMA project. Some posts that I wrote in past as:

Hope I will write many posts in future!

Thursday, May 6, 2010

Unit testing uses NUnit in Visual Studio 2010

Have you ever used NUnit in Visual Studio 2010?
If you used it once times, I thought you shall be realized the problem at here. NUnit can't run on .NET 4.0. So how can I run it when I writes Unit Testing? There is have a little trick for running it well. You only added some line of XML in configuration file of NUnit. And this is my work:

Finding the tag and adding the configuration tag is . And the result is

Hope it is useful trick for you!

Wednesday, April 28, 2010

Customizing controller factory used Structure Map

I recently started to working on new version of ASP.NET MVC 2. And the first thing I must to do is customizing the controller factory to using IoC for getting controller type. In the first stable version of ASP.NET MVC (1.0), I had done it, but in this version it had some thing changed. In special, when you inherited from the DefaultControllerFactory class it didn't have the RequestContext instance to the constructor. In the version 2, the constructor is have 2 parameters.

But why do you need customizing the Controller Factory? That because you need injecting some instances to the constructor of Controller. For example, you coded the CategoryFacade class and wanted to injecting into the CategoryController for getting some data from Database. this is a situation that you need customizing the Controller Factory.

Now, we started to implementing it. The first thing you need is inheriting the DefaultControllerFactory class:
In my case, I used Structure Map  for IoC container. And in my Registry class, I need scanning the Web assembly as below:

Now, we must notify for ASP.NET engine for it can understand my custom Controller. How do I make it?. It's really easy, I implemented as below:
Don't confuse about it. At here, I only implement the Bootstrapper for my application. So don't care about it. We only concentrate on the ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory()). As you see I replaced the default Controller Factory with my custom Controller Factory.

Next step, we need registering all my configuration, so when the website started we shall have all instances to using. In this post, I implemented the Bootstrapper for booting the Website. So we only write the simple line of code as below in Global.asax:

As you see, we registered all Areas and continued to booting my website by the Bootstrapper. Just simple thing! Now saving all codes, and press F5 in Visual Studio's IDE. We shall possible injecting the Category Facade instance as here:

Finally, in order to writing this post, I referenced many articles from Internet. And this is my resources: