介绍

该篇文章通过一个简单的 ASP.NET MVC 项目进行介绍如何使用 autofac 及 autofac 的 MVC 模块进行依赖注入。注入方式通过构造函数。在编写 aufofac 的依赖注入代码之前先准备一些基础类。

基础类

public class UserInfo
{
public int Id { get; set; } public string Name { get; set; }
}
public interface IRepository<T>
{
void Add( T item ); void Modifty( T item ); List<T> Find( Expression<Func<T, bool>> predicate = null );
}
public interface IProductRepository:IRepository<ProductInfo>
{ }
public class ProductRepository : IProductRepository
{
private static List<ProductInfo> _products = new List<ProductInfo>(); public void Add( ProductInfo item )
{
_products.Add( item );
} public List<ProductInfo> Find( Expression<Func<ProductInfo, bool>> predicate = null )
{
if ( predicate == null ) return _products; return _products.Where( predicate.Compile() ).ToList();
} public void Modifty( ProductInfo item )
{
var product = _products.Find( u => u.Id == item.Id ); if ( product == null ) return; product.Name = item.Name;
}
}
public interface IAppService<T>
{
void Add( T item ); void Modify( T item ); List<T> Find( Expression<Func<T, bool>> predicate = null );
}
public interface IProductAppService:IAppService<ProductInfo>
{ }
public class ProductAppService : IProductAppService
{
public IProductRepository ProductRepository { get; set; } public void Add( ProductInfo item )
{
this.ProductRepository.Add( item );
} public List<ProductInfo> Find( Expression<Func<ProductInfo, bool>> predicate = null )
{
return this.ProductRepository.Find( predicate );
} public void Modify( ProductInfo item )
{
this.ProductRepository.Modifty( item );
}
}
public class ProductController : Controller
{
public IProductAppService ProductService { get; set; } public ActionResult Index()
{
this.ProductService.Add( new ProductInfo { Id = , Name = "Lumia" } ); return View("~/Views/Product/Index.cshtml");
}
}

属性注入代码

以下代码中 PropertiesAutowired() 方法就是实现了属性注入的核心,从单词字面意思就可以看出其用意。

private void _InitIoC()
{
var builder = new ContainerBuilder(); builder.RegisterControllers( typeof( MvcApplication ).Assembly )
.PropertiesAutowired();
builder.RegisterAssemblyTypes( typeof( MvcApplication ).Assembly )
.Where( t => (t.Name.EndsWith( "Repository" ) || t.Name.EndsWith("AppService")) && !t.IsAbstract )
//.InstancePerDependency() //每次都创建一个对象
//.SingleInstance() //每次都是同一个对象
//.InstancePerLifetimeScope() //同一个生命周期生成的对象是同一个
.InstancePerRequest() //单个 Web / HTTP / API 请求的范围内的组件的共享一个实例。仅可用于支持每个请求的依赖关系的整合(如MVC,Web API,Web Forms等)。
.AsImplementedInterfaces()
.PropertiesAutowired(); var container = builder.Build();
var resolver = new AutofacDependencyResolver( container, new StubLifetimeScopeProvider( container ) ); DependencyResolver.SetResolver( resolver );
}

其它

Autofac.dll 版本 3.4.0.0
Autofac.Integration.Mvc.dll 版本 3.3.4.215

Autofac 的属性注入方式的更多相关文章

  1. Autofac 的属性注入,IOC的坑

    Autofac 是一款优秀的IOC的开源工具,完美的适配.Net特性,但是有时候我们想通过属性注入的方式来获取我们注入的对象,对不起,有时候你还真是获取不到,这因为什么呢? 1.你对Autofac 不 ...

  2. 基于autofac的属性注入

    基于autofac的属性注入 什么是属性注入 在了解属性注入之前,要先了解一下DI(Dependency Injection),即依赖注入.在ASP.NET Core里自带了一个IOC容器,而且程序支 ...

  3. Autofac 的构造函数注入方式

    介绍 该篇文章通过一个简单的 ASP.NET MVC 项目进行介绍如何使用 autofac 及 autofac 的 MVC 模块进行依赖注入.注入方式通过构造函数. 在编写 aufofac 的依赖注入 ...

  4. ASP.NET Core中使用Autofac进行属性注入

    一些无关紧要的废话: 作为一名双修程序员(自封的),喜欢那种使用Spring的注解形式进行依赖注入或者Unity的特性形式进行依赖注入,当然,形式大同小异,但结果都是一样的,通过属性进行依赖注入. A ...

  5. TZ_11_Spring-Boot的属性注入方式(jdbc为例)

    1.以上一篇文档为基础 2.创建jdbc外部属性文件 application.properties此名字为默认文件名在使用是不需要使用 @Propertysource("classpath: ...

  6. spring的两种属性注入方式setter注入和构造器注入或者自动注入

    1.这里的属性自动注入,与注解配置bean是两回事.这里的自动注入,指的是bean属性的自动注入. bean属性自动注入,包括byNAme和ByType两码事. 2.所有的applicationCon ...

  7. spring属性注入方式

    一.使用有参构造注入属性 配置文件 constructor-arg标签是需注入属性的名字 User类 生成了User的有参构造函数 测试类 结果 打印出了name属性的值 二.使用set方法注入属性 ...

  8. .net core番外第2篇:Autofac的3种依赖注入方式(构造函数注入、属性注入和方法注入),以及在过滤器里面实现依赖注入

    本篇文章接前一篇,建议可以先看前篇文章,再看本文,会有更好的效果. 前一篇跳转链接:https://www.cnblogs.com/weskynet/p/15046999.html 正文: Autof ...

  9. WebAPI2使用Autofac实现IOC属性注入完美解决方案

    一.前言 只要你是.NETer你一定IOC,IOC里面你也会一定知道Autofac,上次说了在MVC5实现属性注入,今天实现在WebApi2实现属性注入,顺便说一下autofac的程序集的注入方式,都 ...

随机推荐

  1. bootstrap-4

    html文档中,列表结构主要有三种:有序列表.无序列表和定义列表:<ul><li>.<ol><li>.<dl><dt><d ...

  2. js 函数和变量的提升

    js 函数和变量的提升 1. 函数的作用域: js中 ,函数的作用域为函数,而不是大括号. var hei = 123;if(true){ hei = 456;}console.log(hei);// ...

  3. c#检测端口是否被占用

    当我们要创建一个Tcp/Ip Server connection ,我们需要一个范围在1000到65535之间的端口 . 但是本机一个端口只能一个程序监听,所以我们进行本地监听的时候需要检测端口是否被 ...

  4. Hadoop学习12-配置集群环境

    由于之前虚拟机都是用的桥接方式,有时候没有网络可用,想学习的时候,就狠不方便. 于是研究了一下,希望搭建一个多台虚机组成一个局域网的集群,即host-only方式 1.安装VM,网络选择“host-o ...

  5. FC 坦克大战 老巢铁墙

    老巢外围铁墙E2A9:AC 80 EFEF80:A5 10 85 45 A5 45 AC D2 E2 用十六进制编辑器打开坦克大战的游戏文件搜索A5 45 F0 25 A5 0B改为AC 80 EF ...

  6. TextView与Html相结合的具体方法?

    TextView与Html相结合的具体方法? android教程之textview解析带图片的html示例 Android中的TextView,本身就支持部分的Html格式标签.这其中包括常用的字体大 ...

  7. 使用属性动画简单实现view飞入效果

    比较简单的效果,可以用来菜单飞入之类,作为记录吧, package com.test.animation; import android.app.Activity; import android.os ...

  8. Nginx+Nodejs搭建图片服务器

    图片上传请求由Node处理,图片访问请求由Nginx处理. 1.Nginx配置 #user nobody; worker_processes 1; #error_log logs/error.log; ...

  9. 7. Reverse Words in a String

    题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is b ...

  10. Clustering with the ArcGIS API for Flex

    Clustering is an excellent technique for visualizing lotss of point data. We've all seen application ...