[译]Use Dependency Injection In WebForms Application
怎么在已用的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的更多相关文章
- 【译】Dependency Injection with Autofac
先说下为什么翻译这篇文章,既定的方向是架构,然后为了学习架构就去学习一些架构模式.设计思想. 突然有一天发现依赖注入这种技能.为了使得架构可测试.易维护.可扩展,需要架构设计为松耦合类型,简单的说也就 ...
- Ninject学习(一) - Dependency Injection By Hand
大体上是把官网上的翻译下而已. http://www.ninject.90iogjkdcrorg/wiki.html Dependency Injection By Hand So what's Ni ...
- MVC Controller Dependency Injection for Beginners【翻译】
在codeproject看到一篇文章,群里的一个朋友要帮忙我翻译一下顺便贴出来,这篇文章适合新手,也算是对MEF的一个简单用法的介绍. Introduction In a simple stateme ...
- [转载][翻译] IoC 容器和 Dependency Injection 模式
原文地址:Inversion of Control Containers and the Dependency Injection pattern 中文翻译版本是网上的PDF文档,发布在这里仅为方便查 ...
- 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 ...
- 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 ...
- Inversion of Control Containers and the Dependency Injection pattern
https://martinfowler.com/articles/injection.html One of the entertaining things about the enterprise ...
- 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, ...
- Benefits of Using the Spring Framework Dependency Injection 依赖注入 控制反转
小结: 1. Dependency Injection is merely one concrete example of Inversion of Control. 依赖注入是仅仅是控制反转的一个具 ...
随机推荐
- LaTeX教程与下载
LaTeX教程与下载如下: 其实,下载好CTEX 的步骤只有三步.第一步下载好CTEX ,第二步下载编辑器Texstudio ,第三步安装配置TexStudio 详细步骤: 第一步:下载CTEX La ...
- java ee wildfly 批处理 job 工作
配置批处理job,同时启动两个并行任务processData,syncTableTask,执行往后执行第三个任务job:playDurationTask. xml配置如下: <job id=&q ...
- 2019.4.1考试&2019.4.2考试&2019.4.4考试
4.1:T1原题,T2码农板子题,T3板子题 4.2 好像是三个出题人分别出的 以及#define *** 傻逼 T1 思维好题 转成树形DP,$dp[i][j]$表示点i值为j的方案数,记录前缀和转 ...
- A1014. Waiting in Line
Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...
- 【洛谷P1896】互不侵犯
题目大意:给定 N*N 的棋盘,一共放 K 个国王,一共有多少种方法. 题解: i&i<<1 判断是否每个 1 的位置之间都有 0. i&j<<1 判断 i 中 ...
- express框架学习笔记
用express框架也有一段时间了,下面我总结一下我做项目时迷惑的点: app.use()与app.get()的区别 app.use()是用来为应用程序绑定中间件的,当第一个参数是path是,第二个参 ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- python学习笔记:python异常的调用原理
因为错误是class,捕获一个错误就是捕获到该class的一个实例.因此,错误并不是凭空产生的,而是有意创建并抛出的.Python的内置函数会抛出很多类型的错误,我们自己编写的函数也可以抛出错误. h ...
- linux ------ 使用 screen 后 SSH 断开后程序依旧能在后台运行
为什么ssh断开后你运行的进程会退出呢? 因为所有进程都得有个父进程.当你ssh到一个服务器上时,打开的shell就是你所有执行命令的父进程. 当你断开ssh连接时,你的命令的父进程就没了.如果处理不 ...
- 6.Django Admin学习
##创建超级用户 python manage.py createsuperuser 英文界面换成中文: 修改setting.py zh_Hans ##后台显示数据 django requets get ...