1.IOC 与 DI 简介
IOC 全称是 Inversion Of Control(控制反转), 不是一种技术, 只是一种思想, 一个重要的面相对象编程的法则, 它能知道我们如何设计出松耦合, 更优良的程序. 传统应用程序都是由我们在类内部主动创建依赖对象, 从而导致类与类之
间高耦合, 难于测试; 有了 IoC 容器后, 把创建和查找依赖对象的控制权交给了容器, 由容器进行注入组合对象, 所以对象与对象之间是松散耦合, 这样也方便测试, 利于功能复用, 更重要的是使得程序的整个体系结构变得非常灵活. 其实 IoC
对编程带来的最大改变不是从代码上, 而是从思想上, 发生了 "主从换位" 的变化. 应用程序原本是老大, 要获取什么资源都是主动出击, 但是在 IoC/DI 思想中, 应用程序就变成被动的了, 被动的等待 IoC 容器来创建并注入它所需要的资源
了.
IoC 很好的体现了面向对象设计法则之 -- 法则:"别找我们, 我们找你"; 即由 IoC 容器帮对象找相应的依赖对象并注入, 而不是由对象主动去找.
DI 全称是 Dependency Injection(依赖注入), 是组件之间依赖关系有容器在运行期决定, 即由容器动态的将某个依赖关系注入到组件之中, 依赖注入的目的并非软件系统带来更多的功能,
而是为了提升组件重用的频率, 并为系统搭建一个灵活可扩展的平台. 通过依赖注入机制, 我们只需要通过简单的配置, 而无需代码指定目标需要的资源, 完成自身的业务逻辑, 而不需要关心具体的资源来自何处
DI 的关键是:"谁依赖谁, 为什么需要依赖, 谁注入水, 注入了什么"
2.Unity IOC 简介
Unity 是一个 IOC 容器, 用来实现依赖注入 (DI) 减少耦合, 出自于微软.
组件地址: http://unity.codeplex.com/
3. 使用 Unity 依赖注入编写 web ServiceDemo
a. 使用 NuGet 程序包管理安装引用 Unity
b. 在 Global.asax 中配置容器
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.SessionState;
- using Microsoft.Practices.Unity;
- using WebServiceDemo.Impl;
- using WebServiceDemo.Interface;
- namespace WebServiceDemo
- {
- public class Global : System.Web.HttpApplication,IContainerAccessor
- {
- private static IUnityContainer _container;
- public static IUnityContainer Container {
- get { return _container; }
- set { _container = value; }
- }
- IUnityContainer IContainerAccessor.Container
- {
- get
- {
- return Container;
- }
- }
- protected void Application_Start(object sender, EventArgs e)
- {
- log4net.Config.XmlConfigurator.Configure();
- CreateContainer();
- }
- protected void Session_Start(object sender, EventArgs e)
- {
- }
- protected void Application_BeginRequest(object sender, EventArgs e)
- {
- }
- protected void Application_AuthenticateRequest(object sender, EventArgs e)
- {
- }
- protected void Application_Error(object sender, EventArgs e)
- {
- }
- protected void Session_End(object sender, EventArgs e)
- {
- }
- protected void Application_End(object sender, EventArgs e)
- {
- }
- protected virtual void CreateContainer()
- {
- IUnityContainer container = new UnityContainer();
- container.RegisterType<IWebServiceDemo, WebServiceDemo>();
- Container = container;
- }
- }
- }
c. 创建服务接口基类
- using Microsoft.Practices.Unity;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using WebServiceDemo.Interface;
- namespace WebServiceDemo.Services
- {
- public abstract class BaseService<T> :System.Web.Services.WebService where T:class
- {
- public BaseService()
- {
- InjectDependencies();
- }
- protected virtual void InjectDependencies()
- {
- HttpContext context = HttpContext.Current;
- if (context == null)
- return;
- IContainerAccessor accessor = context.ApplicationInstance as IContainerAccessor;
- if (accessor == null)
- return;
- IUnityContainer container = accessor.Container;
- if (container==null)
- {
- throw new InvalidOperationException("Container on Global Application Class is Null. Cannot perform BuildUp.");
- }
- container.BuildUp(this as T);
- }
- }
- }
d. 创建实现服务的接口和实现类
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using WebServiceDemo.Dto;
- namespace WebServiceDemo.Interface
- {
- public interface IWebServiceDemo
- {
- RespResult<ResultOutput> AddResult(int a,int b);
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using WebServiceDemo.customize;
- using WebServiceDemo.Dto;
- using WebServiceDemo.Interface;
- namespace WebServiceDemo.Impl
- {
- public class WebServiceDemoImpl : IWebServiceDemo
- {
- private static readonly ILog log = LogManager.GetLogger(typeof(BeingSweptServiceImpl));
- public RespResult<ResultOutput> AddResult(int a,int b)
- {
- return a+b;
- }
- }
- }
e. 创建配置 Webservice
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- using WebServiceDemo.customize;
- using WebServiceDemo.Interface;
- using Microsoft.Practices.Unity;
- using WebServiceDemo.Dto;
- namespace WebServiceDemo.Services
- {
- /// <summary>
- /// Summary description for MainSweepService
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [System.ComponentModel.ToolboxItem(false)]
- // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
- // [System.Web.Script.Services.ScriptService]
- public class ServiceDemoService : BaseService<ServiceDemoService>
- {
- [Dependency]
- public IWebServiceDemo _webServiceDemo
- {
- get; set;
- }
- public ServiceDemoService () : base()
- {
- }
- [WebMethod(Description ="加法")]
- public RespResult<ResultOutput> AddResult(int a,int b)
- {
- return this._webServiceDemo.AddResult(a,b);
- }
- }
- }
来源: http://www.bubuko.com/infodetail-2772025.html