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

1 comment:

  1. Đây là trang nhạc của các webmaster vn nhacf.com - được viết với mục đích chỉ để nghe nhạc.

    Trang nhạc trình bày đơn giản nhanh gọn chuyên nghiệp, nghe nhạc nhanh, tìm kiếm bhát dễ dàng.

    Mới chỉ có ít người biết đến. nhưng nhacf được dân lập trình đánh giá rất cao về chất lượng và cách thể hiện.

    Để làm trang web giống như nhacf.com, bạn có thể dùng blogspot để tạo & copy sourcecode của nó ở đây

    Phù hợp với ~ người có sở thích đơn giản nhanh gọn và muốn khám phá..

    ReplyDelete