Chủ Nhật, 2 tháng 11, 2014

ST - Inversion of control and Dependency injection

Link tham khảo: http://www.codeproject.com/Articles/29271/Design-pattern-Inversion-of-control-and-Dependency

Chào tháng 11 với bài tuts viết rất dễ hiểu về IoC và DI.

Introduction

I have been writing and recording on design patterns for past some days. You can see some of the design pattern videos we have made on http://www.questpond.com/FreeDesign1.htm. You can read my previous articles on design patterns ,UML, MVC and MVP from the below links:
  • DI using unity application blocks http://www.codeproject.com/KB/aspnet/IOCandDI.aspx
  • Part 1 – Design patterns Factory, Abstract factory, builder, prototype, shallow and deep copy, and singleton and command patterns
  • Part 2 – Design patterns Interpreter, Iterator, Mediator, Memento and observer patterns 
  • Part 3 – Design patterns State, Strategy, Visitor, Adapter and fly weight pattern 
  • Part 4 - Design patterns Bridge, Composite, Decorator, Facade, COR, Proxy and template pattern
  • Part 5 Model View Controller MVC
  • Part 6 Model View Presenter
  •  
You can download by architecture interview question book from here.

In this section we will discuss about how IOC and DI can help us build loosely coupled software architecture. I am not sure should we call this a design pattern or more of a approach. If you search around the web you will see lot of controversy on whether IOC is a design pattern or not. From my point of view it is a design pattern as it solves a problem context.

It would be great to see actual architectures implementing IOC using container oriented approaches. I am sure it will change the way we think about interaction between components.

So let’s understand in detail about IOC and DI.

The Problem – Tight Coupling

Before even we get in to abbreviation of IOC and DIP, let’s first understand the problem. Consider the below example we have a customer class which contains an address class object. The biggest issue with the code is tight coupling between classes. In other words the customer class depends on the address object. So for any reason address class changes it will lead to change and compiling of ‘ClsCustomer’ class also. So let’s put down problems with this approach:
  • The biggest problem is that customer class controls the creation of address object.
  • Address class is directly referenced in the customer class which leads to tight coupling between address and customer objects.
  • Customer class is aware of the address class type. So if we add new address types like home address, office address it will lead to changes in the customer class also as customer class is exposed to the actual address implementation.
     

Figure: - Problems of IOC
So if for any reason the address object is not able to create the whole customer class will fail in the constructor initialization itself.

Solution

Now that we know the issue, let’s understand the solution. The solution definitely revolves around shifting the object creation control from the customer class to some one else. The main problem roots from the customer class creating the address object. If we are able to shift this task / control of object creation from the customer class to some other entity we have solved our problem. In other sentence if we are able to invert this control to a third party we have found our solution. So the solution name is IOC (Inversion of control).

Principles of IOC

The basic principle of IOC stands on the base of Hollywood principle (response given to amateurs auditioning in Hollywood):

Do not call us we will call you 

Translating to bollywood (for struggling actors)

Aap Mauke ko mat bulao, mauka aap ke paas ayega – Hindi conversion ?

In other words it like address class saying to the customer class, do not create me I will create myself using some one else.

There are two principles of IOC:
  • Main classes aggregating other classes should not depend on the direct implementation of the aggregated classes. Both the classes should depend on abstraction. So the customer class should not depend directly on the address class. Both address and customer class should depend on an abstraction either using interface or abstract class.
  • Abstraction should not depend on details, details should depend on abstraction.
     

Figure: - IOC framework
Figure ‘IOC framework’ shows how we can achieve this decoupling. The simplest way would be to expose a method which allows us to set the object. Let the address object creation be delegated to the IOC framework. IOC framework can be a class, client or some kind of IOC container. So it will be two step procedure IOC framework creates the address object and passes this reference to the customer class.

Ways of implementing IOC

Ok, now we know the problem, let’s try to understand the broader level solution. Let’s look at how we implement the solution for IOC. IOC is implemented using DI (Dependency injection). We have discussed on a broader level about how to inject the dependency in the previous sections. In this section we will dive deeper in to other ways of implementing DI.
 
Figure: - IOC and DI
Figure ‘IOC and DI’ shows how IOC and DI are organized. So we can say IOC is a principle while DI is a way of implementing IOC. In DI we have four broader ways of implementing the same:
  • Constructor way
  • Exposing setter and getter
  • Interface implementation
  • Service locator
In the further sections we will walkthrough the same in more detail.

Constructor Methodology

In this methodology we pass the object reference in the constructor itself. So when the client creates the object he passes the object in the constructor while the object is created. This methodology is not suited for client who can only use default constructors.
 

Figure: - Constructor based DI
 

Setter and Getter

This is the most commonly used DI methodology. The dependent objects are exposed through set/get methods of classes. The bad point is because the objects are publicly exposed it breaks the encapsulation rule of object oriented programming.
 

Figure: - Getter and Setter
 

Interface based DI 

In this methodology we implement an interface from the IOC framework. IOC framework will use the interface method to inject the object in the main class. You can see in figure ‘Interface based DI’ we have implemented an interface ‘IAddressDI’ which has a ‘setAddress’ method which sets the address object. This interface is then implemented in the customer class. External client / containers can then use the ‘setAddress’ method to inject the address object in the customer object.
 
Figure: - Interface based DI
 

Service Locator

The other way to inject dependency is by using service locator. Your main class which will aggregate the child object will use the service locator to obtain instance of the address object. The service locator class does not create instances of the address object, it provides a methodology to register and find the services which will help in creating objects.
Figure: - Service locator

Implementing the DI

Now that we know the various types of DI to implement IOC. Its time to understand how we can actually implement these DI’s.  

What’s wrong with DI FACTORY?

The first thing which clicks to mind is, can't we achieve all the above things using factory. The main problem is all about one class doing the creational activity of its contained objects which leads to heavy coupling. Introducing factory can solve that to a great extent.

Here are the issues with factory which makes us force to think about some other solutions:
  • Everything is hardcoded: - The biggest issues with factory are it can not be reused across applications. All the options are hardcoded in the factory itself which makes the factory stringent to particular implementation.
  • Interface dependent: - The base on which factories stands are common interfaces. Interfaces decouple the implementation and the object creation procedure. But then all the classes should implement a common interface. This is a limitation by itself again.
  • Factories are custom: - They are very much custom to a particular implementation.
  • Everything is compile time: - All dependent objects for an object in factory have to be known at compile time.

The container way

A container is an abstraction responsible for object management, instantiation and configuration. So you can configure the objects using the container rather than writing client code like factory patterns to implement object management. There are many containers available which can help us manage dependency injection with ease. So rather than writing huge factory codes container identifies the object dependencies and creates and injects them in appropriate objects.
 
Figure: - Container in action
So you can think about container as a mid man who will register address and customer objects as separate entity and later the container creates the customer and address object and injects the address object in the customer. So you can visualize the high level of abstraction provided by containers.

What we will do is cover the customer and address example using one of the container Windsor container, you can get more details about the container here.

Implementation using Windsor

The first thing we do is create the address interface and create the concrete class from this interface. Interface will be an entity to use for injection rather than concrete objects, so that we deal with more abstraction rather than concrete implementation.
 
Figure: - Address interface
In the customer class we have passed the object through the constructor.
Figure: - Customer class
If we are said to write the client code. , it would be something as shown in figure ‘Client code’. In step 1 we create a concrete object and point the implementation to the interface IAddress. In step 2 we pass the interface object to customer class constructor while creating the object. 
Figure: - Client code
 
Ok, now lets see how this will work if we use the Windsor container. Figure ‘Windsor container’ shows how it looks like. So step 1 creates the Windsor container object. Step 2 and 3 register the types and concrete objects in the container. Step 4 requests the container to create the customer object. In this step the container resolves and set the address object in the constructor. Step 5 releases the customer object.
 
Figure: - Windsor container 
Ok, guys understood, the above code is more complicated than the client code. In actual implementation using the container we never use client code, rather we use config files. You can see from figure ‘Creating using config files’ we have better flexibility to add more objects. The XmlInterpreter object helps to read the config file to register the objects in the container. Using the container.resolve method we have finally created the customer object. So the container plays the mediator role of understanding the customer object and then injecting the address object in the customer object through the constructor. In config file we need to define all the components in the components section.
 
Figure: - Creating using config files

References

Hanselman has given a list of containers useful link to visit:

Thứ Năm, 9 tháng 10, 2014

[English for Dummies] Câu hỏi đuôi



Link tham khảo:
·        http://forum.englishtime.us/default.aspx?g=posts&t=5951 (bài hướng dẫn của thầy cucku)

I - CÂU HỎI ĐUÔI
(Link: http://forum.englishtime.us/default.aspx?g=posts&t=5951)
Phần cơ bản
1. Công thức
 S + V + O , [] + ĐẠI TỪ ?
Trong đó:
ĐẠI TỪ: Lấy chủ từ câu đầu (S) đổi thành đại từ
Đàn ông                     ---> he
Đàn bà                       ---> she
Vật (số ít )                 ---> it
There                         --->
there
This                            ---> it
That                           ---> it
These                         ---> they
Those                         ---> they
Số nhiều                     ---> they
They, he she ---> giữ nguyên
[] : nhìn ở câu đầu nếu có động từ đặc biệt thì chuyển thành [] nếu không có thì mượn trợ động từ do, does, did
Lưu ý 1: Những động từ đặc biệt có thể chuyển ra vị trí [] mà không cần phải mượn trợ động từ
- is, am, are, was, were
- will, would
- can, could
- may, might
- should
- had ( better) (viết tắt ‘d better)
- would (rather) (viết tắt ‘d rather)
- have, has, had ( + p.p) - lưu ý nếu phía sau không có p.p ( cột 3) thì không được xem là động từ đặc biệt.
Lưu ý 2: Nếu câu đầu có NOT, hoặc các yếu tố phủ định như : never, rarely, no, hardly ...., thì [] không có NOT, nếu câu đầu không có NOT thì []NOT
2. Dạng đặc biệt
- I am --------> Aren't I ? ( nhưng nếu là : I am not ... thì lại dùng : am I ? )
- Let's ....--------> Shall we ?
- Nobody, no one, everyone, everybody,Someone, somebody ...-----------------> [] they ?
- One ------------->    [] you/one
- Câu mệnh lệnh ( không có chủ từ ) -------> Will you ?

Ví dụ:
- Lan can go, can't she ? ( động từ đặt biệt đem can ra sau)
- Tom likes it, doesn't he ? (không có động từ đặt biệt nên mượn trợ động từ does )
- The dogs won't run, will they ? ( câu đầu có not nên câu sau không có not )
- Go out with me, will you ? ( câu mệnh lệnh )
- Don't take it, will you ? ( mệnh lệnh, dù có not hay không cũng dùng will you )
- I am a student, aren't I ?
3. Phần nâng cao
3.1. Câu đầu là I  WISH
- Dùng MAY
- Ví dụ: I wish to study English, may I ?
3.2. Chủ từ là ONE
- Dùng you hoặc one
- Ví dụ: One can be one’s master, can’t you/one?
3.3. Câu đầu có MUST
Must có nhiều cách dùng cho nên tùy theo cách dùng mà sẽ có câu hỏi đuôi khác nhau
- Must chỉ sự cần thiết: => dùng needn’t
Ví dụ: They must study hard, needn’t they?
- Must chỉ sự cấm đoán: => dùng must
Ví dụ: You mustn’t come late, must you ?
-  Must chỉ sự dự đoán ở hiện tại: => dựa vào động từ theo sau must
Ví dụ: He must be a very intelligent student, isn’t he?  ( anh ta ắt hẳn là 1 học sinh rất thông minh, phải không ?)
Must chỉ sự dự đoán ở quá khứ ( trong công thức must +have+ p.p) : => dùng [ ] là have/has
Ví dụ: You must have stolen my bike, haven’t you? ( bạn chắc hẵn là đã lấy cắp xe của tôi, phải không?)
3.4. Let đầu câu
Let đầu câu có nhiều dạng cần phân biệt:
- Let trong câu rủ (let’s ) => dùng  shall we ?
Ví dụ: Let’s go out, shall we? 
- Let trong câu xin phép (let us /let me ): dùng  will you ?
Ví dụ: Let us use the telephone, will you?
Let me have some drink, will you?
- Let trong câu đề nghị giúp người khác (let me): dùng may I ?
Ví dụ: Let me help you do it, may I ? 
3.5. Câu cảm thán
Lấy danh từ trong câu đổi thành đại từ, [ ] dùng is, am, are
Ví dụ:
- What a beautiful dress, isn’t it?
- What a stupid boy, isn’t he?
- How intelligent you are, aren’t you?
3.6. Câu đầu có I + các động từ sau: think, believe, suppose, figure, assume, fancy, imagine, reckon, expect, see, feel + mệnh đề phụ
Lấy mệnh đề phụ làm câu hỏi đuôi.
Ví dụ:
- I think he will come here, won’t he?
- I don’t believe Mary can do it, can she?  ( lưu ý MĐ chính có not thì vẫn tính như ở MĐ phụ)
Cùng mẫu này nhưng nếu chủ từ không phải là I thì lại dùng mệnh đề đầu làm câu hỏi đuôi.
Ví dụ: She thinks he will come, doesn’t she?
3.7. Câu đầu có It seems that + mệnh đề 
Lấy mệnh đề làm câu hỏi đuôi.
Ví dụ: It seems that you are right, aren’t you?
3.8. Chủ từ là mệnh đề danh từ
Dùng it
Ví dụ: What you have said is wrong, isn’t it?
Why he killed himself seems a secret, doesn’t it?
4. Những lỗi thường gặp
4.1. Nhầm 's thành is
Cả ishas đều có cách viết rút gọn là 's , tuy nhiên theo thói quen cứ thấy 's là cho rằng đó là is rút gọn nên khi đem ra phía sau chỗ [] người làm "phục hồi" chúng thành chữ is.
Cách khắc phục lỗi này
Nếu nhìn thấy phía sau có p.p ( động từ cột 3 hoặc thêm ed) thì nên xem lại nếu không phải câu bị động thì 's đó chính là has.
4.2. Không nhận ra 's
'd là viết tắc của would hoặc had, do đó nếu người làm không cẫn thận sẽ lúng túng và chọn [] sai
Cách khắc phục lỗi này
- Nếu nhìn phía sau có better/ p.p  thì 'd đó là viết tắt của had
- Nếu phía sau có rather / like / love thì 'd là viết tắt của would
II - CÁCH TRẢ LỜI CÂU HỎI ĐUÔI
(Link: http://www.tienganh.com.vn/archive/index.php/t-37610.html)
Đối với câu hỏi dạng Yes, No thì trả lời sao thì ý nghĩa y chang vậy, dù là dạng khẳng định hay phủ định.
Trích ví dụ sau:
Are you a student? ~ Aren't you a student? đều có chung câu trả lời:
Yes, I am hoặc No, I am not.

Tim doesn't look very well today, does he? ~ Tom looks well today, doesn't he?
- No,he looks very tired. (Không, trông bạn ấy không được khỏe/mệt mỏi)
- Hoặc Yes, he looks well.(Có, trông khỏe mạnh mà)

Tặng bạn 3 câu hỏi của thầy tớ hồi trước :)
1. You seem not to understand me, do you? (Em có vẻ không hiểu nhỉ?)
Yes, I do.
(Có, em có hiểu mà)
or
No, I don't.
(Không, em không hiểu)
(trả lời không hiểu bị phạt đọc lại bài :D)


2. Don't you want to have a break? (~ Do you want to have a break?) Các em có muốn ra chơi không?
Yes, I do.
( Ai trả lời No, I don't nhịn ra chơi :D)


3. Don't you hate me? (Em ghét thầy hả??????)
No, I don't (Không, em có ghét đâu)
(Trả lời Yes, I do thì toi rồi ::D)