Introduction to dependency injection with unityContainer

/* 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 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!

时间: 2024-10-27 07:12:19

Introduction to dependency injection with unityContainer的相关文章

Dependency Injection in ASP.NET Web API 2

What is Dependency Injection? A dependency is any object that another object requires. For example, it's common to define a repository that handles data access. Let's illustrate with an example. First, we'll define a domain model: public class Produc

DEPENDENCY INJECTION EXAMPLE USING SPRING

Dependency Injection The Spring Framework is literally built around the concept of Dependency Injection. In this post, we’ll take a look at a simple example of Dependency Injection using the Spring Framework. If you want a deeper dive on Dependency I

Dependency Injection in ASP.NET Web API 2 (在web api2 中使用依赖注入)

原文:http://www.asp.net/web-api/overview/advanced/dependency-injection 1 什么是依赖注入(Dependency Injection) 依赖,简单来说就是一个对象需要的任何其他对象.具体解释请Google或百度.在我们使用Web api2  开发应用程序时,通常都会遇到Controller  访问库 Repository 问题. 例如:我们定义一个Domain 对象 public class Product { public in

[LINK]List of .NET Dependency Injection Containers (IOC)

http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx I'm trying to expand my mind around dependency injection in .NET (beyond the two frameworks I've personally used) and an starting to put together a list of .NET Dependency I

IoC(Inversion of Control)控制反转和 DI(Dependency Injection)依赖注入

首先想说说IoC(Inversion of Control,控制倒转).这是spring的核心,贯穿始终.所谓IoC,对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的关系.这是什么意思呢,举个简单的例子,我们是如何找女朋友的?常见的情况是,我们到处去看哪里有长得漂亮身材又好的mm,然后打听她们的兴趣爱好.qq号.电话号.ip号.iq号---,想办法认识她们,投其所好送其所要,然后嘿嘿--这个过程是复杂深奥的,我们必须自己设计和面对每个环节.传统的程序开发也是如此,在

Dependency Injection 筆記 (4)

續上集未完的相關設計模式... Composite 模式延續先前的電器比喻.現在,如果希望 UPS 不只接電腦,還要接電風扇.除濕機,可是 UPS 卻只有兩個電源輸出孔,怎麼辦? 我們可以買一條電源延長線,接在 UPS 上面.如此一來,電風扇.除濕機.和電腦便都可以同時插上延長線的插座了.這裡的電源延長線,即類似Composite Pattern(組合模式),因為電源延長線本身又可以再連接其他不同廠牌的延長線(這又是因為插座皆採用相同介面),如此不斷連接下去. 呃….延長線的比喻有個小問題:它在

Dependency Injection 筆記 (1)

<.NET 相依性注入>連載 (1) 本文從一個基本的問題開始,點出軟體需求變動的常態,以說明為什麼我們需要學習「相依性注入」(dependency injection:簡稱 DI)來改善設計的品質.接著以一個簡單的入門範例來比較沒有使用 DI 和改寫成 DI 版本之後的差異,並討論使用 DI 的時機.目的是讓讀者先對相關的基礎概念有個概括的理解,包括可維護性(maintainability).寬鬆耦合(loose coupling).控制反轉(inversion of control).動態

(You Can Hack It, Architecture and Design) =&gt; { Dependency Injection; }

(You Can Hack It, Architecture and Design) => { Dependency Injection; }

Scala 深入浅出实战经典 第57讲:Scala中Dependency Injection实战详解

王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 腾讯微云:http://url.cn/TnGbdC 360云盘:http://yunpan.cn/cQ4c2UALDjSKy 访问密码 45e2土豆:http://www.tudou.com/programs/view/5LnLNDBKvi8/优酷:http://v.youku.com/v_show/id_