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);
} } }

  

 

c# Unity依赖注入WebService的更多相关文章

  1. Unity 依赖注入之二

    1. 构造子注入 1.1 构造子注入初级代码 container.RegisterType<IMyWork, MyWork>(new InjectionConstructor(new Bo ...

  2. Unity依赖注入使用详解

    写在前面 构造器注入 Dependency属性注入 InjectionMethod方法注入 非泛型注入 标识键 ContainerControlledLifetimeManager单例 Unity注册 ...

  3. WPF PRISM开发入门二(Unity依赖注入容器使用)

    这篇博客将通过一个控制台程序简单了解下PRISM下Unity依赖注入容器的使用.我已经创建了一个例子,通过一个控制台程序进行加减乘除运算,项目当中将输入输出等都用接口封装后,结构如下: 当前代码可以点 ...

  4. C# Unity依赖注入

    简介: 控制反转:我们向IOC容器发出获取一个对象实例的一个请求,IOC容器便把这个对象实例“注入”到我们的手中,在这个过程中你不是一个控制者而是一个请求者,依赖于容器提供给你的资源,控制权落到了容器 ...

  5. Unity 依赖注入

    关于Ioc的框架有很多,比如astle Windsor.Unity.Spring.NET.StructureMap,我们这边使用微软提供的Unity做示例,你可以使用Nuget添加Unity,也可以引 ...

  6. 使用Microsoft.Practices.Unity 依赖注入

    Unity是微软Patterns & Practices团队所开发的一个轻量级的,并且可扩展的依赖注入(Dependency Injection)容器,它支持常用的三种依赖注入方式:构造器注入 ...

  7. 使用Microsoft.Practices.Unity 依赖注入 转载https://www.cnblogs.com/slardar1978/p/4205394.html

    Unity是微软Patterns & Practices团队所开发的一个轻量级的,并且可扩展的依赖注入(Dependency Injection)容器,它支持常用的三种依赖注入方式:构造器注入 ...

  8. ASP.NET MVC5+EF6+EasyUI 后台管理系统(6)-Unity 依赖注入

    系列目录 前言 为了符合后面更新后的重构系统,文章于2016-11-1日重写 本节重构一下代码,采用IOC控制反转,也就是依赖注入 您可以访问http://unity.codeplex.com/rel ...

  9. Unity 依赖注入知识点

    三种依赖注入方法,构造器注入.属性注入.方法注入 可以配置Config文件,来实现不用修改代码.需要先将接口与实体关联,然后使用时会自动加载对应实体. namespace WeChatConsole ...

随机推荐

  1. [Git] How to rename your remote branch

    Rename your local foo branch with bar: git branch -m foo bar Remember this will add the new branch w ...

  2. [Compose] 10. Capture Side Effects in a Task

    We examine the data structure Task, see some constructors, familiar methods, and finally how it capt ...

  3. ChangeWindowMessageFilterEx 概述(用于取消低权限程序向高权限程序发送消息不成功的限制,分6个等级)

    ChangeWindowMessageFilterEx 函数,为指定窗口修改用户界面特权隔离 (UIPI) 消息过滤器. 函数原型: BOOL WINAPI ChangeWindowMessageFi ...

  4. matlab 下的集成学习工具箱

    matlab 当前支持的弱学习器(weak learners)类型分别为: 'Discriminant' 'knn' 'tree' 可通过 templateTree 定义: 1. fitcensemb ...

  5. Deepin下python安装uwsgi报错: Python.h:没有那个文件或目录

    解决方法是安装python-dev,这是Python的头文件和静态库包 正在读取软件包列表... 完成正在分析软件包的依赖关系树       正在读取状态信息... 完成       下列软件包是自动 ...

  6. android之ContentProvider和Uri具体解释

    一.使用ContentProvider(内容提供者)共享数据 在android中ContentProvider的作用是对外共享数据,就是说能够通过ContentProvider把应用中的数据共享给其它 ...

  7. 动态创建Fragment

    在android3.0之前.每创建一个界面就要新创建一个activity. 在3.0之后引入了Fragment.相当于一个轻量级的activity.不须要在清单文件配置. 先来看下怎样创建和使用Fra ...

  8. NOIP模拟 Date - Tarjan

    题目描述 小Y和小Z好不容易有机会相见啦,可是邪恶的小H却不想让他们相见.现在有一些城市,城市之间有双向路径相连,有路径相连的城市之间可以互相到达.小H可以任意选择一条路径,然后用他的邪恶力量污染这条 ...

  9. 商(quotient)—— 两数之比

    1. IQ:Intelligence quotient IQ=MACA×100 MA:心智年龄(mental age) CA:实足年龄(chronological age)

  10. padding百分百值是相对谁的百分比

    先看代码 <head> <meta charset="UTF-8"> <title>Document</title> <sty ...