CASE :
+ Đối với các dự án vừa và lớn , với rất nhiều class và interface có sự liên hệ cực kì phức tạp giữa các đối tượng trong hệ thống. Như vậy khi ta khởi tạo các object và các service sẽ rất rườm rà và chi tiết.
FACT:
+ Việc bạn code sẽ đòi hỏi chuẩn mực nhất định, một số vấn đề sẽ gặp khi khởi tạo 2 object phụ thuộc với nhau như sau:
IA a = new A(); IB b = new B(a);Từ ví dụ trên ta thấy, mỗi khi cần khởi tạo object B ta luôn cần khởi tạo object A trước. Với số lượng object phụ thuộc ngày càng tăng, việc này sẽ dẫn đến khó khăn nhất định.
Vậy có cách nào để đơn giản hóa việc này?
Solution: với IoC, ví dụ trên có thể đơn giản hơn như sau:
IB b = ServiceLocator.Get();ServiceLocator sẽ tự biết cách khởi tạo object A và invoke hàm khởi tạo của B.
ServiceLocator làm điều đó bằng cách nào? Tất nhiên chúng ta sẽ phải cấu hình để ServiceLocator làm đc điều đó, thông qua viết code hoặc file cấu hình (tùy vào khả năng hỗ trợ của tool)
Sức mạnh của IoC nằm ở khả năng reusable và flexible, cùng 1 dòng khởi tạo như trên, code thực thi trong Runtime có thể khác nhau nếu ta thay đổi cấu hính, điều này khiến ứng dụng trở nên uyển chuyển và dễ test.
Vậy tóm lại, IoC là gì?
In software engineering, inversion of control (IoC) is a programming technique, expressed here in terms of object-oriented programming, in which object coupling is bound at run time by an assembler object and is typically not known at compile time using static analysis.Lợi ích của IoC như sau (từ wikipedia):
- There is a decoupling of the execution of a certain task from implementation.
- Every module can focus on what it is designed for.
- Modules make no assumptions about what other systems do but rely on their contracts.
- Replacing modules has no side effect on other modules.
public static void Initialize() { Container container = new Container(x => { x.Forhiểu nôm na là như sau :() .Singleton() .Use(() => NHibernateInitializer.Initialize().BuildSessionFactory()); x.For ().Use (); x.For(typeof(IRepository<>)).Use(typeof(Repository<>)); x.For(typeof(IRepositoryWithTypedId<,>)).Use(typeof(RepositoryWithTypedId<,>)); }); DependencyResolver.SetResolver(new StructureMapDependencyResolver(container)); }
- Tôi cần đồ uống (interface X) - Xin hỏi anh cần đồ uống loại nào? cafe (A:X), trà (B:X), ...? - Buổi sáng tôi muốn uống cafe, còn buổi trưa thì uống tràNhư vậy, mỗi khi bạn gọi đồ uống ở dịch vụ nơi đó, bạn ko cần nhắc lại yêu cầu của mình nữa
Các tools IoC phổ biến
Có rất nhiều (nguồn từ Hanselman):
Here’s what I’ve got so far. What am I missing?
- Castle Windsorbased on the Castle MicroKernel.
- Licensed under Apache 2
- Currently 1.0 RC3
- Well documented and used by many.
- StructureMaphas been around since June 2004
- Licensed under Apache 2
- Actively developed and approaching v2.5.
- Written by Jeremy D. Miller
- Spring.NET
- Licensed under Apache 2
- Currently 1.1, working on 2.0
- Written by Mark Pollack
- Autofacintends to be IoC with a C# 3.0 flavor, but also supports 2.0.
- Licensed under MIT
- Written by Nicholas Blumhardt and Rinat Abdullin
- Unity
- Licensed as MS-PL
- Recently released
- Written by Chris Tavares.
- Build on the “kernel” of ObjectBuilder:
- ObjectBuilder was formerly MSFT’s only public foray into DI/IoC
- Custom PnP License, more restrictive than MS-PL
- Part of the MS PnP group
- Written by Brad Wilson, Peter Provost and Scott Densmore
- Puzzle.NFactory
- Licensed under the Lesser GPL
- Part of the larger Puzzle Framework
- Written by Roger Alsing and Mats Helander
- Ninjectformerly “Titan”
- Licensed under Apache 2
- Was quiet, but has recently sprung back to life.
- Written by Nate Kohari
- S2Container.NET on version 1.3.7.
- Port of Java’s Seasar2
- PicoContainer.NET
- Port of Java’s PicoContainer
- Written by Aslak Hellesøy
- LinFu
- Licensed under Lesser GPL
- Written by Philip Laureano
- Hacked DIY (Do It Yourself)
- There are many examples of quicky IOC Containers. Interesting for learning but little else.
- Oren Eini’s IOC in 15 lines and a follow up in Ken Egozi’s IOC in 15 lines
- There are many examples of quicky IOC Containers. Interesting for learning but little else.
Không có nhận xét nào :
Đăng nhận xét