怎么在已用的WebForm应用中使用DI

假设有一个电影网站,有个页面会列出最近热门的电影。这个项目中使用了仓储模式来获取数据。

public partial class Default : System.Web.UI.Page
{
private IPopularMovie movieMgr = new MovieManager(new XmlMovieRepository()); public async Task<SelectResult> movieList_GetData()
{
var movies = await movieMgr.GetPopularMoviesAsync();
return new SelectResult(movies.Count(), movies);
}
}

按照下面的4个步骤,可以在default.aspx.cs中使用DI。

1. 将项目指定为.NET Framework 4.7.2.

同时还需要修改web.config中的httpRuntime section的targetFramework。

2. 安装AspNet.WebFormsDependencyInjection.Unity NuGet package

3. 在Global.asax中注册类型

using System;
using Microsoft.AspNet.WebFormsDependencyInjection.Unity;
using PopularMovies.Bizlogic;
using PopularMovies.Repository;
using Unity; namespace PopularMovies
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
var container = this.AddUnity(); container.RegisterType<IPopularMovie, MovieManager>();
container.RegisterType<IMovieRepository, XmlMovieRepository>();
}
}
}

4. 修改 Default.aspx.cs

public partial class Default : System.Web.UI.Page
{
private IPopularMovie movieMgr;
public Default(IPopularMovie movieManager)
{
movieMgr = movieManager;
} public async Task<SelectResult> movieList_GetData()
{
var movies = await movieMgr.GetPopularMoviesAsync();
return new SelectResult(movies.Count(), movies);
}
}

可以在哪些地方使用DI?

  • Pages and controls

    • WebForms page
    • User control
    • Custom control
  • IHttpHandler and IHttpHandlerFactory
  • IHttpModule
  • Providers
    • BuildProvider
    • ResourceProviderFactory
    • Health monitoring provider
    • Any ProviderBase based provider created by System.Web.Configuration.ProvidersHelper.InstantiateProvider. e.g. custom sessionstate provider

[译]Use Dependency Injection In WebForms Application的更多相关文章

  1. 【译】Dependency Injection with Autofac

    先说下为什么翻译这篇文章,既定的方向是架构,然后为了学习架构就去学习一些架构模式.设计思想. 突然有一天发现依赖注入这种技能.为了使得架构可测试.易维护.可扩展,需要架构设计为松耦合类型,简单的说也就 ...

  2. Ninject学习(一) - Dependency Injection By Hand

    大体上是把官网上的翻译下而已. http://www.ninject.90iogjkdcrorg/wiki.html Dependency Injection By Hand So what's Ni ...

  3. MVC Controller Dependency Injection for Beginners【翻译】

    在codeproject看到一篇文章,群里的一个朋友要帮忙我翻译一下顺便贴出来,这篇文章适合新手,也算是对MEF的一个简单用法的介绍. Introduction In a simple stateme ...

  4. [转载][翻译] IoC 容器和 Dependency Injection 模式

    原文地址:Inversion of Control Containers and the Dependency Injection pattern 中文翻译版本是网上的PDF文档,发布在这里仅为方便查 ...

  5. Inversion of Control Containers and the Dependency Injection pattern(转)

    In the Java community there's been a rush of lightweight containers that help to assemble components ...

  6. What is dependency injection and when/why should or shouldn't it be used?

    参考:https://stackoverflow.com/questions/130794/what-is-dependency-injection 原文:https://www.jamesshore ...

  7. Inversion of Control Containers and the Dependency Injection pattern

    https://martinfowler.com/articles/injection.html One of the entertaining things about the enterprise ...

  8. Dependency Injection in ASP.NET Web API 2 Using Unity

    What is Dependency Injection? A dependency is any object that another object requires. For example, ...

  9. Benefits of Using the Spring Framework Dependency Injection 依赖注入 控制反转

    小结: 1. Dependency Injection is merely one concrete example of Inversion of Control. 依赖注入是仅仅是控制反转的一个具 ...

随机推荐

  1. 在centos7下用http搭建配置svn服务

    应用场景:SVN是Subversion的简称,是一个开放源代码的版本控制系统. 安装环境:centos7  //已关闭 Selinux和 Firewall 配置步骤: 1. 安装HTTP和SVN相关软 ...

  2. Servlet -- 重定向

    重定向的两种方式: 1: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws S ...

  3. User Agent 用户代理

    User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本.CPU 类型.浏览器及版本.浏览器渲染引擎.浏览器语言.浏览器插件等. User A ...

  4. OpenLayers学习笔记(一)—在线加载谷歌影像地图&离线加载本地瓦片地图

    实现根据在线离线判断加载地图, 在线加载谷歌影响地图, 离线加载本地瓦片地图 作者: 狐狸家的鱼 Github: 八至 html代码 <div id="map" tabind ...

  5. 百度地图API,展示地图和添加控件

    1.申请百度账号和AK 点我申请 2.准备页面 根据HTML标准,每一份HTML文档都应该声明正确的文档类型,我们建议您使用最新的符合HTML5规范的文档声明: <!DOCTYPE html&g ...

  6. 如何在疲劳的JS世界中持续学习

    作者简介 cnfi 蚂蚁金服·数据体验技术团队 本文翻译自<Stay updated in JS fatigue universe>,并对内容有所补充和修改. 部分内容参考<HOW ...

  7. C++11并发——多线程条件变量std::condition_variable(四)

    https://www.jianshu.com/p/a31d4fb5594f https://blog.csdn.net/y396397735/article/details/81272752 htt ...

  8. BUG描述规范管理

    BUG:软件系统中存在的可能导致系统出错.失效.死机等问题的错误或缺陷. 描述一个缺陷,需要以下核心要素 标题:用简洁的话描述该缺陷,主要让开发知道这是一个什么样的缺陷 参数设置:Bug的类型(功能/ ...

  9. 2050 Programming Competition (CCPC)

    Pro&Sol 链接: https://pan.baidu.com/s/17Tt3EPKEQivP2-3OHkYD2A 提取码: wbnu 复制这段内容后打开百度网盘手机App,操作更方便哦 ...

  10. TODO java-awt中文乱码--疑惑

    参考:http://blog.sina.com.cn/s/blog_025270e90101b1db.html 1.IDE工具是eclipse,检查了编码是UTF-8,控制台中是中文,用awt就会乱码 ...