Uncategorized

[C#] Inversion of Control vs Dependency Injection vs Service Locator pattern

Inversion of Control

Inversion of Control means that objects do not create other objects on which they rely to do their work. Instead, they get the objects that they need from an outside service (for example, xml file or single app service). 2 implementations of IoC are DI and ServiceLocator.

Wikipedia definition: Inversion of Control, or IoC, is an abstract principle describing an aspect of some software architecture designs in which the flow of control of a system is inverted in comparison to procedural programming.

In procedural programming: a chunk of codes that uses another chunk of codes is in control of the process. It knows exactly what piece of code, what method in what class that it uses.

Example:

One example is when a class X uses another class Y.

By extracting an abstract definition of the behavior I used by X in Y, and letting X uses an instance of that instead of Y, it can continue to use that without knowing the detail about Y.

Y implements I and X uses an instance of I. X can still use Y, but X doesn’t really know about that. And instead of Y, it can be something else like A, B or C that implements I and X can use that as well.

Benefits of using Inversion of Control:

  • X is not dependent on Y anymore, any change in Y will not affect X.
  • X is much more flexible as we can change which implementation of I it uses without changing anything in X
  • We can isolate our code  when creating unit tests

 

Dependency Injection

Dependency Injection means the IoC principle of getting dependent object is done without using concrete objects but abstractions (interfaces). This makes all components chain testable, cause higher level component doesn’t depend on lower level component, only from the interface. Mocks implement these interfaces.

Using Dependency Injection we inject an instance of I into X when we use it.

public class OrderService
{
    private IOrderSaver orderSaver;

    public OrderService(IOrderSaver orderSaver)
    {
        this.orderSaver = orderSaver;
    }

    public void AcceptOrder(Order order)
    {
        //Domain logic such as validation

        orderSaver.SaveOrder(order);
    }
}

 

Service Locator

OrderService doesn’t know about OrderDatabase but it does know about an instance of the concrete class ServiceLocator which in turn knows about OrderDatabase.

When AcceptOrder is invoked it will ask ServiceLocator for an instance of IOrderSaver which will supply one based on how we have configured it.

Using this approach OrderService could be implemented like this:

public class OrderService
{
    public void AcceptOrder(Order order)
    {
        //Domain logic such as validation

        OrderSaverFactory.GetOrderSaver().SaveOrder(order);
    }
}

A simple implementation of ServiceLocator would be to have it supply an instance of a default implementation of IOrderSaver unless we specifically tell it to supply another instance, like this:

public class ServiceLocator
{
    public static IOrderSaver OrderSaver { get; set; }

    public static IOrderSaver GetOrderSaver()
    {
        if (OrderSaver == null)
            OrderSaver = new OrderDatabase();

        return OrderSaver;
    }
}

Reference: http://joelabrahamsson.com/inversion-of-control-an-introduction-with-examples-in-net/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s