[译]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. 依赖注入是仅仅是控制反转的一个具 ...
 
随机推荐
- POJ 1845 Sumdiv(逆元)
			
题目链接:Sumdiv 题意:给定两个自然数A,B,定义S为A^B所有的自然因子的和,求出S mod 9901的值. 题解:了解下以下知识点 1.整数的唯一分解定理 任意正整数都有且只有唯一的方式 ...
 - os模块总结
			
学了忘,忘了学,忘了就来看一下...唯一进步的就是这次学的比上次更快了- - 最常用的几个: os.getcwd() # os.path.abspath(os.path.dirname(__fil ...
 - spring data jpa 分页查询
			
https://www.cnblogs.com/hdwang/p/7843405.html spring data jpa 分页查询 法一(本地sql查询,注意表名啥的都用数据库中的名称,适用于特 ...
 - 如何写一个通用的README规范
			
背景 我们平常在进行项目开发时,一般都会把代码上传至代码托管平台上方便管理和维护.目前我厂使用的托管平台是SVN,国内外还有一些比较知名的代码托管平台,比如github.Gitlab.BitBucke ...
 - 如何使用Senparc.Weixin SDK 底层的Redis缓存并设置过期时间
			
最近在微信第三方平台项目开发中,有一个需求,所有绑定的公众号的回复规则按照主公众号的关键词配置来处理,我的处理思路是获取主公众号配置的关键词回复规则,缓存10分钟,由于需要使用Redis缓存来存储一些 ...
 - zxing二维码的生成与解码(C#)
			
ZXing是一个开源Java类库用于解析多种格式的1D/2D条形码.目标是能够对QR编码.Data Matrix.UPC的1D条形码进行解码. 其提供了多种平台下的客户端包括:J2ME.J2SE和An ...
 - Java Scanner篇
			
Scanner 翻译解释为扫描器 Java中用于控制台的的数据输入 包:import java.util.Scanner 使用方法:Scanner reader=new Scanner(System. ...
 - redis设置最大内存
 - C++ template一些体悟(1)
			
#include <iostream> using namespace std; template<typename T> class testClass { public: ...
 - 对C# .Net4.5异步机制测试(二)——加深印象
			
public static void Main() { Console.WriteLine(Thread.CurrentThread.ManagedThreadId); In(); Console.W ...