Inversion of Control, Dependency Injection - useful fashion
Introduction
Nowdays IoC and DI are very popular concepts in world of programming. These things are able to reuse the number of dependences between classes.
Inversion of Control - it is a certain abstract principle that allows you to create the weak relationship between classes. This way each class in their work does not rely on specific implementation of other classes.
Dependency Injection - is concrete realisation of IoC principle.
For automate the ability to write code in accordance with this approach use some type of libraries (IoC-container). Let's try to show this information in examples on Ninject.
Implementation
Project available on Github
Without using IoC-container we wrote something like this:
public class MongoDbContext : IMongoContext { private readonly MongoClient _client; private readonly IMongoDatabase _database; public MongoDbContext() { _client = new MongoClient(); _database = _client.GetDatabase(DATABASE_NAME); } }However, we have a strong bond between the classes , and so any changes in the implementation will require changes in MongoDbContext. So we need to with the help of DI and IoC container to break this relationship and make less dependence.
We need to realize IMongoClient interface and make less dependence between classes. DI have 3 technology for made this task done:
- Constructor Injection
- Method Injection
- Setter Injection
Example uses Constructor injection for making less dependence between classes.
Constructor injection was demonstrated in next code:
For the success of this approach we need to implement factory. To do this, create a class. And register our interface in the factory. Such IoC- container helps reduce the amount of routine, allowing the allocation of the interface and its concrete realization,so you always use it. In our example, we'll do it with the help of factory controllers , as shown in the following code:
public class MongoDbContext : IMongoContext { public const string DATABASE_NAME = "JaelShop"; public const string PRODUCTS_COLLECTION_NAME = "products"; private readonly IMongoClient _client; private readonly IMongoDatabase _database; public MongoDbContext(IMongoClient client) { _client = client; _database = _client.GetDatabase(DATABASE_NAME); } }
For the success of this approach we need to implement factory. To do this, create a class. And register our interface in the factory. Such IoC- container helps reduce the amount of routine, allowing the allocation of the interface and its concrete realization,so you always use it. In our example, we'll do it with the help of factory controllers , as shown in the following code:
public class NinjectControllerFactory: DefaultControllerFactory { private IKernel ninjectKernel; private const string CONNECTION_STRING_NAME = "JaelShop"; public NinjectControllerFactory() { ninjectKernel = new StandardKernel(); AddBindings(); } protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) { return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType); } private void AddBindings() { ninjectKernel.Bind().To (); ninjectKernel.Bind ().To (); ninjectKernel.Bind ().To () .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString); } }
Дописати коментар
0 Коментарі