Introduction to dependency injection with unityContainer
2015-02-16 05:22
531 查看
/* By Dylan SUN */
This article is just a simple introduction to unityContainer.
If you have already used unityContainer as a dependency injection or IoC (Inversion of control) library, this article is not for you.
As you know, dependency injection is a design pattern to prevent the code coupling.
For example, you access class CA’s method MA in several places in your application. If one day you need to use class CB instead of class CA. You need to replace all the code referencing class CA with class CB.
But if you use dependency injection, you just need to register the interface and its implementation class once. You can resolve the interface when you need to access method CA.
Here is an sample usage of unityContainer:
Firstly, create an instance of UnityContainer.
Register the interface and its implementation class.
Resolve the interface.
Access to method :
In this way, you won’t need to create any instance of a specific class. The code is more maintainable.
I hope you find this article helpful!
This article is just a simple introduction to unityContainer.
If you have already used unityContainer as a dependency injection or IoC (Inversion of control) library, this article is not for you.
As you know, dependency injection is a design pattern to prevent the code coupling.
For example, you access class CA’s method MA in several places in your application. If one day you need to use class CB instead of class CA. You need to replace all the code referencing class CA with class CB.
But if you use dependency injection, you just need to register the interface and its implementation class once. You can resolve the interface when you need to access method CA.
Here is an sample usage of unityContainer:
Firstly, create an instance of UnityContainer.
//register interface and its implementation IUnityContainer container = new UnityContainer();
Register the interface and its implementation class.
container.RegisterType<IConfigurationReader, ConfigurationReader>();
Resolve the interface.
var configurationReader = container.Resolve<IConfigurationReader>();
Access to method :
var value = configurationReader.GetAppSettings("hello");
In this way, you won’t need to create any instance of a specific class. The code is more maintainable.
I hope you find this article helpful!
相关文章推荐
- Inversion of Control and Dependency Injection with Castle Windsor Container - Part I
- How to use Autofac IoC Container with Unity 3D?
- SCSF - Part 3 Introduction to Dependency Injection
- Inversion of Control and Dependency Injection with Castle Windsor Container
- CS100.1x Introduction to Big Data with Apache Spark
- JavaTech, an Introduction to Scientific and Technical Computing with Java
- Building Applications with Force.com and VisualForce(Dev401)(十六):Data Management: Introduction to Upsert
- Introduction to XML and XML With Java
- Introduction to programming with OpenCV
- Introduction: Connect to SQL Membership with ASP.NET Application
- 更新jenkins插件,报错 Perhaps you need to run your container with "-Djava.awt.headless=true"?
- 【学习笔记】WEEK 1_Introduction to Deep Learning_Supervised Learning with Neural Networks
- Inversion of Control Containers and the Dependency Injection pattern
- Inversion of Control Containers and the Dependency Injection pattern
- Introduction to Programming with C++ Comprehension Version
- Inversion of Control Containers and the Dependency Injection pattern
- Introduction to Sound Programming with ALSA(使用alsa API接口编程)
- Inversion of Control/Dependency Injection with Repository Pattern, Fluent Nhibernate, and LightCore
- 理解PHP依赖注入容器(dependency injection container)系列(三) Symfony服务容器介绍
- An introduction to Generative Adversarial Networks (with code in TensorFlow)