IoC containers and service locator pattern

I am trying to get an understanding of Inversion of Control and the dos and donts of this. Of all the articles I read, there is one by Mark Seemann (which is widely linked to in SO) which strongly asks folks not to use the service locator pattern.

Then somewhere along the way, I came across this article by Ken where he helps us build our own IoC.

I noticed that is is nothing but an implementation of service locator pattern.

Questions:

  1. Is my observation correct that this implementation is the service locator pattern?
  2. If the answer to 1. is yes, then Do all IoC containers (like Autofac) use the service locator pattern?
  3. If the answer to 1. is no, then why is this differen?
  4. Is there any other pattern (other than DI) for inversion of control?

Consolidation of answers:

Answer 1. Kelly: The service locator pattern is indeed used for the initial Resolve call. The service locator pattern has to be used at least once.

Answer 2. Eric: Simply calling Ioc.Resolve(ISomeInterface) doesn’t mean that the IoC container is being used as a Service Locator. After all, Resolve() has to be called somewhere, right?

Answer 3. Tungano: No, his code contains a constructor receiving a dependency… No, a container doesn’t ‘use’ any pattern. It’s really up to the programmer using the container to use a pattern. It’s all in how you wire it together. Basically you can both use a container for DI and Service locator.

Answer 4. Gnat: …factory and lookup… [can be used for inversion of control.]

I have learnt, thanks to the above answers that IoC itself uses Service Locator Pattern. This is not inherently harmful. Using the IoC can be done in a number of ways (including the Service Locator Pattern). This is the part we need to take care and use other options of injecting the dependency.

10

Simply calling Ioc.Resolve(ISomeInterface) doesn’t mean that the IoC container is being used as a Service Locator. After all, Resolve() has to be called somewhere, right?

The clue is where and how many times is IoC.Resolve() is being called? If it’s being called all over the place, in the dependent objects themselves, then that’s a sign that the IoC container is being misused as a Service Locator. If Resolve() is being called in one central location (typically called the ‘composition root’), then it’s most likely being used correctly.

The article you mentioned doesn’t talk about when and where to call Resolve(), it just explains what happens after it’s called.

0

1) No, his code contains a constructor receiving a dependency:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public BuildDirectoryStructureService(IFileSystemAdapter fileSystemAdapter)
</code>
<code>public BuildDirectoryStructureService(IFileSystemAdapter fileSystemAdapter) </code>
public BuildDirectoryStructureService(IFileSystemAdapter fileSystemAdapter)

The IoC container is injecting the dependency.

If it used the service locator the BuildDirectoryStructureService would self obtain the dependency. For example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public BuildDirectoryStructureService()
{
this.fileSystemAdapter = Locator.Current.Resolve<IFileSystemAdapter>();
}
</code>
<code>public BuildDirectoryStructureService() { this.fileSystemAdapter = Locator.Current.Resolve<IFileSystemAdapter>(); } </code>
public BuildDirectoryStructureService()
{
   this.fileSystemAdapter = Locator.Current.Resolve<IFileSystemAdapter>();
}

An IoC container can be used as a service locator.

2 & 3)

No, a container doesn’t ‘use’ any pattern. It’s really up to the programmer using the container to use a pattern. It’s all in how you wire it together.

Basically you can both use a container for DI and Service locator.

4) You can do constructor injection and still not really have inversion of control. You can ask for concrete types in a constructor. Dependencies you inject may not have a workable abstraction for multiple implementations and such.

Inversion of control is aligned with the Hollywood principle, don’t call us, we’ll call you. Instead of operating a service you let the service “call back” on you over an interface it defines.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật