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:
http://martinfowler.com/articles/injection.html#UsingAServiceLocator
http://msdn.microsoft.com/en-us/library/ff649658.aspx
http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html
http://msdn.microsoft.com/en-us/magazine/cc337885.aspx
http://msdn.microsoft.com/en-us/magazine/cc163739.aspx

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 http://thangcq.blogspot.com/2010/04/configurating-automapper.html 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 http://nma.codeplex.com. 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:

http://thangcq.blogspot.com/2010/04/mocking-lamda-expression-method.html
http://thangcq.blogspot.com/2010/04/converting-expression-tree.html
http://thangcq.blogspot.com/2010/04/configurating-automapper.html
http://thangcq.blogspot.com/2010/04/customizing-controller-factory-used-ioc.html
http://thangcq.blogspot.com/2010/05/unit-testing-uses-nunit-in-visual.html

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!