介绍

该篇文章通过一个简单的 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. HDU 2277 Change the ball

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2277 Change the ball Time Limit: 2000/1000 MS (Java/O ...

  2. 安装配置sock5代理

    环境准备及安装 yum -y install pam-devel openldap-devel cyrus-sasl-devel yum -y install openssl-devel.x86_64 ...

  3. (MST) HDOJ 1102 Constructing Roads

    怎么说呢 这题就是个模板题 但是 hud你妹夫啊说好的只有一组数据呢??? 嗯??? wa到家都不认识了好吗 #include <cstdio> #include <cstring& ...

  4. [BZOJ 3530][Sdoi 2014]数数

    阿拉~好像最近总是做到 AC 自动机的题目呢喵~ 题目的算法似乎马上就能猜到的样子…… AC 自动机 + 数位 dp 先暴力转移出 f[i][j] :表示从 AC 自动机上第 j 号节点走 i 步且不 ...

  5. 论文笔记之: Deep Metric Learning via Lifted Structured Feature Embedding

    Deep Metric Learning via Lifted Structured Feature Embedding CVPR 2016 摘要:本文提出一种距离度量的方法,充分的发挥 traini ...

  6. protobuf 数据解析的2种方法

    方法1: message person{required int32 age = 1;required int32 userid = 2;optional string name = 3;} mess ...

  7. Spring MVC 4.2 增加 CORS 支持 Cross-Origin

    基于XML的配置: <mvc:cors> <mvc:mapping path="/api/**" allowed-origins="http://dom ...

  8. [redis] redis.clients.jedis.exceptions.JedisDataException: MOVED 13102 127.0.0.1

    这个异常上网查了很久才知道原因: MOVED indicates that you're using Redis Cluster. ShardedJedis is not for Redis Clus ...

  9. [Spring MVC] - 地址路由使用(一)

    常用的一些Spring MVC的路由写法以及参数传递方式. 参考引用: http://docs.spring.io/spring/docs/3.0.x/spring-framework-referen ...

  10. 【python】模块作用域

    作用域 在一个模块中,我们可能会定义很多函数和变量,但有的函数和变量我们希望给别人使用,有的函数和变量我们希望仅仅在模块内部使用.在Python中,是通过_前缀来实现的. 类似_xxx和__xxx这样 ...