怎么在已用的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. C++类相关

    本文打算通过一些小例子来说明几个关键的知识点. 一:成员函数相关 #include <iostream> using namespace std; class D { public: vo ...

  2. [SCOI2014]方伯伯的OJ(线段树)

    方伯伯正在做他的Oj.现在他在处理Oj上的用户排名问题.Oj上注册了n个用户,编号为1-n“,一开始他们按照编号排名. 方伯伯会按照心情对这些用户做以下四种操作,修改用户的排名和编号: 1.操作格式为 ...

  3. DNA Consensus String

    题目(中英对照): DNA (Deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It co ...

  4. redis执行lua文件

    1.编写lua文件 还lua表示循环插入1-100的键为1-100值为1-100的键值对到redis中 for i=0,100 do redis.call("set",i,i) e ...

  5. 网络分层和Http协议原理

    网络分层: 应用层 传输层 网络层 数据链路层 物理层 物理层: 比特流在节点之间的传输,是计算机连接起来的物理手段. 数据链路层: 控制网络层和物理层之间的通信,功能是在不可靠的物理线路上进行数据可 ...

  6. MySQL命令,一篇文章替你全部搞定

    MySQL命令,一篇文章替你全部搞定 MySQL的基本操作可以包括两个方面:MySQL常用语句如高频率使用的增删改查(CRUD)语句和MySQL高级功能,如存储过程,触发器,事务处理等.而这两个方面又 ...

  7. 把 android 手机变成 web server (golang)

    配置 golang 开发环境 略 安装并初始化 gomobile go get golang.org/x/mobile/cmd/gomobile gomobile init 创建 beego 项目, ...

  8. ASP.NET MVC什么时候使用异步Action

    在没有使用异步Action之前,在Action内,比如有如下的写法: public ActionResult Index() CustomerHelper cHelper = new Customer ...

  9. Vue中data返回对象和返回值的区别

    速记:粗浅的理解是,事件的结果是影响单个组件还是多个组件.因为大部分组件是要共享的,但他们的data是私有的,所以每个组件都要return一个新的data对象 返回对象的时候 <!DOCTYPE ...

  10. vue,elementUI,less,axios,qs的安装及打包

    目前一直使用vue去搭建项目,我个人习惯用Visual Studio Code编辑工具,所以下面的所有操作都是在这个编辑器中 在安装的时候,为了避免安装过程中出错,你最好安装了淘宝镜像(官网:http ...