[译]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. 依赖注入是仅仅是控制反转的一个具 ...
随机推荐
- centos7/RHEL7下快速搭建DNS域名解析服务器
应用场境:此处搭建的DNS(Domain Name Server)更加偏向于企业内部需要一个域名服务器专门用来解析自己局域网内定义的域名: 比如:app1.company.com, app2.comp ...
- Python 分布式进程
#-*-coding:utf-8-*- '''分布式进程指的是将Process进程分不到多台机器上,充分利用多台机器的性能完成复杂的任务''' #服务器端 #--------------------- ...
- ArcGIS for qml -关于空间参考如何选择设置
作者: 狐狸家的鱼 Github: 八至 版权声明:如需转载请获取授权和联系作者 1.关于空间参考 空间参考可以通过众所周知的ID(WKID) - 整数值来引用. 官网指南中也有对此的专门说明 htt ...
- Java 对远程文件的操作
首先添加jar <dependency> <groupId>jcifs</groupId> <artifactId>jcifs</artifact ...
- request 的介绍使用属性
上下文:相当于一个容器,保存了 Flask 程序运行过程中的一些信息. Flask中有两种上下文,请求上下文和应用上下文 请求上下文(request context) 在 flask 中,可以直接在视 ...
- Apache Ant 项目构建
项目构建:通过构建工具对多个项目进行统一批量的编译和运行,比如,对多个Jmeter脚本批量运行 1.Ant是什么? Ant是 构建工具,Apache Ant是一个将软件编译.测试.部署等步骤联系在一起 ...
- poj 1523"SPF"(无向图求割点)
传送门 题意: 有一张联通网络,求出所有的割点: 对于割点 u ,求将 u 删去后,此图有多少个联通子网络: 对于含有割点的,按升序输出: 题解: DFS求割点入门题,不会的戳这里
- selenium官方网站文档,testng官方网站文档
selenium 阅读至grid部分,当前没有远程需求,搁置:https://www.seleniumhq.org/docs/07_selenium_grid.jsp testng http://te ...
- charles使用:iOS11的手机用charles抓包https
参考:https://www.jianshu.com/p/235bc6c3ca77 因为ios11经常抓不了包,以前一直没管,今天实在是不行,,,,搞了一下.OK了 步骤: 1.下载并安装charle ...
- php 在服务器端开启错误日志记录方法
修改php.ini设置,或者通过方法 ini_set设置以下项即可 1.打开error_reporting设置: 如 error_reporting= E_ALL 2. log_errors=On ...