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. php面试题12(多态web服务器共享session的方法:将session存到数据库)($val=&$data[$key];)

    php面试题12(多态web服务器共享session的方法:将session存到数据库)($val=&$data[$key];) 一.总结 1.多态web服务器共享session的方法: ...

  2. PHP移动互联网开发笔记(4)——自定义函数及数组

    一.自定义函数 自定义函数就是我们自己定义的函数,在PHP中自定义函数格式如下: function funname(arg1, arg2, arg3......){ //TODO return val ...

  3. [Django] Building the rest API

    Install the rest api framework: pip install djangorestfamework In settings.py: INSTALLED_APPS = [ 'd ...

  4. Warning: file_put_contents(常用单词1.txt): failed to open stream: Invalid argument in

    Warning: file_put_contents(常用单词1.txt): failed to open stream: Invalid argument in 一.总结 1.上述问题是因为Win ...

  5. 机器学习:Softmax Classifier (两个隐含层)

    程序实现 softmax classifier, 含有两个隐含层的情况.activation function 是 ReLU : f(x)=max(0,x) f1=w1x+b1 h1=max(0,f1 ...

  6. .net core 下使用StackExchange的Redis库访问超时解决

    原文:.net core 下使用StackExchange的Redis库访问超时解决 目录 问题:并发稍微多的情况下Redis偶尔返回超时 给出了参考网址? 结论 小备注 引用链接 问题:并发稍微多的 ...

  7. STL源代码学习--vector用法汇总

    一.容器vector 使用vector你必须包含头文件<vector>: #include<vector> 型别vector是一个定义于namespace std内的templ ...

  8. 颜色转换:#hhhfff->UIColor (MHHexColoring)

    MHHexColoring为开发者快速获取想要的十六进制颜色(Hex Color) 查找16进制色码的网站:http://www.color-hex.com // 版权属于原作者 MHHexColor ...

  9. 如何在华为云软件开发云上运行Python

    一. 华为云软件开发云与Python 1. 华为云软件开发云简介 华为云软件开发云(DevCloud)是集华为近30年研发实践,前沿研发理念,先进研发工具为一体的一站式云端DevOps平台,面向开发者 ...

  10. ANDROID L——RecyclerView,CardView进口和使用(Demo)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简单介绍: 这篇文章是ANDROID L--Material Design具体解释(UI控 ...