c# Unity依赖注入WebService
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的更多相关文章
- Unity 依赖注入之二
1. 构造子注入 1.1 构造子注入初级代码 container.RegisterType<IMyWork, MyWork>(new InjectionConstructor(new Bo ...
- Unity依赖注入使用详解
写在前面 构造器注入 Dependency属性注入 InjectionMethod方法注入 非泛型注入 标识键 ContainerControlledLifetimeManager单例 Unity注册 ...
- WPF PRISM开发入门二(Unity依赖注入容器使用)
这篇博客将通过一个控制台程序简单了解下PRISM下Unity依赖注入容器的使用.我已经创建了一个例子,通过一个控制台程序进行加减乘除运算,项目当中将输入输出等都用接口封装后,结构如下: 当前代码可以点 ...
- C# Unity依赖注入
简介: 控制反转:我们向IOC容器发出获取一个对象实例的一个请求,IOC容器便把这个对象实例“注入”到我们的手中,在这个过程中你不是一个控制者而是一个请求者,依赖于容器提供给你的资源,控制权落到了容器 ...
- Unity 依赖注入
关于Ioc的框架有很多,比如astle Windsor.Unity.Spring.NET.StructureMap,我们这边使用微软提供的Unity做示例,你可以使用Nuget添加Unity,也可以引 ...
- 使用Microsoft.Practices.Unity 依赖注入
Unity是微软Patterns & Practices团队所开发的一个轻量级的,并且可扩展的依赖注入(Dependency Injection)容器,它支持常用的三种依赖注入方式:构造器注入 ...
- 使用Microsoft.Practices.Unity 依赖注入 转载https://www.cnblogs.com/slardar1978/p/4205394.html
Unity是微软Patterns & Practices团队所开发的一个轻量级的,并且可扩展的依赖注入(Dependency Injection)容器,它支持常用的三种依赖注入方式:构造器注入 ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(6)-Unity 依赖注入
系列目录 前言 为了符合后面更新后的重构系统,文章于2016-11-1日重写 本节重构一下代码,采用IOC控制反转,也就是依赖注入 您可以访问http://unity.codeplex.com/rel ...
- Unity 依赖注入知识点
三种依赖注入方法,构造器注入.属性注入.方法注入 可以配置Config文件,来实现不用修改代码.需要先将接口与实体关联,然后使用时会自动加载对应实体. namespace WeChatConsole ...
随机推荐
- [javase学习笔记]-6.6 基本数据类型參数与引用数据类型參数的传递过程
这一节基本数据类型參数和引用数据类型參数的传递过程. 数据类型參数和引用參数我们在前面章节中都已涉及到了,那么我们来看看以下的两段代码: //基本数据类型參数传递 class Demo { publi ...
- [Postgres] Filter Data in a Postgres Table with Query Statements
We have all this data, but how do we answer questions about it? In this lesson we’ll learn how to fi ...
- UIPasteboard粘贴板:UITableView复制粘贴功能(二)
这篇咱写写一写在UITableView上实用剪贴板的两种方法: 一:在自定义cell里面使用 其实,在cell的使用跟在label里面的用法基本一样 1.放方法: - (BOOL)canBecomeF ...
- Leetcode--easy系列1
近期開始刷Leetcode题目.花了一个星期先完毕了easy难度级别的题目,easy级别的题目思路比較简单,但不一定就直接AC了,主要是问题要考虑全然.特别是对特殊情况的处理. #6 ZigZag C ...
- SQLite的查询优化
SQLite是个典型的嵌入式DBMS,它有很多优点,它是轻量级的,在编译之后很小,其中一个原因就是在查询优化方面比较简单,它只是运用索引机制来进行优化的,经过对SQLite的查询优化的分析以及对源代码 ...
- ServletContextListener和ContextLoaderListener的区别
ServletContext 被 Servlet 程序用来与 Web 容器通信.例如写日志,转发请求.每一个 Web 应用程序含有一个Context,被Web应用内的各个程序共享.因为Context可 ...
- CSS拾遗(一)
重新看<精通CSS(第二版)>做一些记录,方便今后巩固. 1.外边距叠加 只有普通文档流中块框的垂直外边距才会发生外边距叠加.行内框.浮动框.或绝对定位框之间的外边距不会叠加. 2.相对定 ...
- iOS6 与iOS7以及7以上状态栏的颜色设置
iOS7默认状态栏文字颜色为黑色 修改为白色的方法:(chenyong注意 我的Status bar style 使用的仍是默认值Gray style(default)) 1在Info.plist中设 ...
- PAT 1011-1020 题解
早期部分代码用 Java 实现.由于 PAT 虽然支持各种语言,但只有 C/C++标程来限定时间,许多题目用 Java 读入数据就已经超时,后来转投 C/C++.浏览全部代码:请戳 本文谨代表个人思路 ...
- [android]完美的解决方案ListView加载网络图片反弹问题
为什么 先说为什么有照片反弹. 使用convertView对ListView的每一个item优化,item的复用能够有效减少内存的占用.使ListView滑动更为流畅. 但会带来一个问题,当最顶部的i ...