Autofac使用
创建用例类
创建接口
namespace MyIBLL
{
public interface IUserBLL
{
void AddUser(string name, string pass);
}
}
一个类
namespace MyBLL
{
public class UserBLL:IUserBLL
{
public void AddUser(string name, string pass)
{
Console.WriteLine($"新增用户{name}");
}
}
}
基础
static void Main(string[] args)
{
//1.1 基本用法
//用于从组件注册中构建 IContainer。
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterType<UserBLL>().As<IUserBLL>();
//IContainer:为一组组件创建、连接依赖关系和管理生命周期。
//IContainer的大多数实例都是由 ContainerBuilder创建的。
IContainer container = builder.Build();
//Resolve:从上下文中检索服务。
IUserBLL userBll = container.Resolve<IUserBLL>();
userBll.AddUser("tang san san","123");
Console.Read();
}
AsImplementedInterfaces
这里也可以使用 AsImplementedInterfaces 把所有接口都实例化
static void Main(string[] args)
{
//1.2 基本用法
ContainerBuilder builder = new ContainerBuilder();
//实现所有接口
builder.RegisterType<UserBLL>().AsImplementedInterfaces();
IContainer container = builder.Build();
IUserBLL userBll = container.Resolve<IUserBLL>();
userBll.AddUser("tang san san","123");
Console.Read();
}
Assembly
对程序集注册,如果有很多接口和类,可以如下:
//1.3 基本用法
ContainerBuilder builder = new ContainerBuilder();
//对程序集注册
Assembly asm = Assembly.Load("MyBLL");
builder.RegisterAssemblyTypes(asm).AsImplementedInterfaces();
IContainer container = builder.Build();
IUserBLL userBll = container.Resolve<IUserBLL>();
userBll.AddUser("tang san san", "123");
创建实例方法
InstancePerDependency
对每一个依赖或每一次调用创建一个新的唯一的实例。这也是默认的创建实例的方式。
官方文档解释:Configure the component so that every dependent component or call to Resolve() gets a new, unique instance (default.)
InstancePerLifetimeScope
在一个生命周期域中,每一个依赖或调用创建一个单一的共享的实例,且每一个不同的生命周期域,实例是唯一的,不共享的。
官方文档解释:Configure the component so that every dependent component or call to Resolve() within a single ILifetimeScope gets the same, shared instance. Dependent components in different lifetime scopes will get different instances.
InstancePerMatchingLifetimeScope
在一个做标识的生命周期域中,每一个依赖或调用创建一个单一的共享的实例。打了标识了的生命周期域中的子标识域中可以共享父级域中的实例。若在整个继承层次中没有找到打标识的生命周期域,则会抛出异常:DependencyResolutionException。
官方文档解释:Configure the component so that every dependent component or call to Resolve() within a ILifetimeScope tagged with any of the provided tags value gets the same, shared instance. Dependent components in lifetime scopes that are children of the tagged scope will share the parent's instance. If no appropriately tagged scope can be found in the hierarchy an DependencyResolutionException is thrown.
InstancePerOwned
在一个生命周期域中所拥有的实例创建的生命周期中,每一个依赖组件或调用Resolve()方法创建一个单一的共享的实例,并且子生命周期域共享父生命周期域中的实例。若在继承层级中没有发现合适的拥有子实例的生命周期域,则抛出异常:DependencyResolutionException。
官方文档解释:
Configure the component so that every dependent component or call to Resolve() within a ILifetimeScope created by an owned instance gets the same, shared instance. Dependent components in lifetime scopes that are children of the owned instance scope will share the parent's instance. If no appropriate owned instance scope can be found in the hierarchy an DependencyResolutionException is thrown.
SingleInstance
每一次依赖组件或调用Resolve()方法都会得到一个相同的共享的实例。其实就是单例模式。
官方文档解释:Configure the component so that every dependent component or call to Resolve() gets the same, shared instance.
InstancePerHttpRequest
在一次Http请求上下文中,共享一个组件实例。仅适用于asp.net mvc开发。
参考:
七七 Autofac 依赖注入框架 使用
官方中文文档:https://autofaccn.readthedocs.io/zh/latest/
Autofac使用的更多相关文章
- AutoFac在项目中的应用
技能大全:http://www.cnblogs.com/dunitian/p/4822808.html#skill 完整Demo:https://github.com/dunitian/LoTCode ...
- Autofac - MVC/WebApi中的应用
Autofac前面写了那么多篇, 其实就是为了今天这一篇, Autofac在MVC和WebApi中的应用. 一.目录结构 先看一下我的目录结构吧, 搭了个非常简单的架构, IOC(web), IBLL ...
- Autofac - 生命周期
实例生命周期决定在同一个服务的每个请求的实例是如何共享的. 当请求一个服务的时候,Autofac会返回一个单例 (single instance作用域), 一个新的对象 (per lifetime作用 ...
- Autofac - 属性注入
属性注入不同于通过构造函数方式传入参数. 这里是通过注入的方式, 在类创建完毕之后, 资源释放之前, 给属性赋值. 这里, 我重新弄一些类来演示这一篇吧. public class ClassA { ...
- Autofac 的点滴
泛型类型的注册和使用 public interface IRepository<T> where T:class { } public interface ISchoolDetailRep ...
- ASP.NET Core 整合Autofac和Castle实现自动AOP拦截
前言: 除了ASP.NETCore自带的IOC容器外,我们还可以使用其他成熟的DI框架,如Autofac,StructureMap等(笔者只用过Unity,Ninject和Castle). 1.ASP ...
- Autofac 的属性注入,IOC的坑
Autofac 是一款优秀的IOC的开源工具,完美的适配.Net特性,但是有时候我们想通过属性注入的方式来获取我们注入的对象,对不起,有时候你还真是获取不到,这因为什么呢? 1.你对Autofac 不 ...
- Autofac 组件、服务、自动装配 《第二篇》
一.组件 创建出来的对象需要从组件中来获取,组件的创建有如下4种(延续第一篇的Demo,仅仅变动所贴出的代码)方式: 1.类型创建RegisterType AutoFac能够通过反射检查一个类型,选择 ...
- 使用Adminlite + ASP.NET MVC5(C#) + Entityframework + AutoFac + AutoMapper写了个api接口文档管理系统
一.演示: 接口查看:http://apidoc.docode.top/ 接口后台:http://apiadmin.docode.top/ 登录:administrator,123456 二.使用到的 ...
- autofac 组件的实例范围
实例范围决定如何在请求之间共享服务. 原文地址:http://docs.autofac.org/en/latest/lifetime/instance-scope.html 每个依赖一个实例 使用这个 ...
随机推荐
- react native使用百度echarts显示图表
echarts是百度推出的免费开源的图表组件,功能丰富,涵盖各行业图表.公司项目做h5项目用了不少,最近公司翻新h5页面,用react-native改造,来达到增强用户体验效果的目的.项目中遇到了一些 ...
- hyper发送表单数据
前言 某个美丽的下午,运维把服务器上的nginx升级了,http协议也变成了http2.0,我本地的requests再也连接不到服务器,然后就找到了额hyper 但是hyper的文档写的很简单,而且相 ...
- Prometheus-Consul-Api
官方地址:https://www.consul.io/docs/agent/http.html consul的主要接口是RESTful HTTP API,该API可以用来增删查改nodes.servi ...
- JavaEE三大框架整合
搭建项目: 搭建一个实际的项目,为了避免影响到你之前已经搭建过的项目,可以先新建一个工作空间,指定一个新的空间来做这一次的项目,不会和其他的项目冲突,尤其是在编码这一部分. 指定新工作空间的目录: 一 ...
- 编写高质量的Python代码系列(二)之函数
Python中的函数具备多种特性,这可以简化编程工作.Python函数的某些性质与其他编程语言中的函数相似,但也有性质是Python独有的.本节将介绍如何用函数来表达亿图.提升可复用程度,并减少Bug ...
- 02-oracle中的基础sql
1.SQL SQL(Structured Query Language) 语言是目前主流的关系型数据库上执行数据操作.数据检索以及数据库维护所需要的标准语言,是用户与数据库之间进行交流的接口,许多关系 ...
- 深入剖析Kubernetes学习笔记:容器基础(05-06)
05 :从进程说起 1.容器本身没有价值,有价值的是"容器编排" 2.什么是进程? 一旦"程序"被执行起来,它就从磁盘上的二进制文件,变成 1.计算机内存中的数 ...
- 原型模式-Prototype(Java实现)
原型模式-Prototype 通过复制(克隆.拷贝)一个指定类型的对象来创建更多同类型的对象. 就像去蛋糕店买蛋糕一样. 柜台里的蛋糕都是非卖品. 只是为顾客提供一种参照. 当顾客看上某一个样式的蛋糕 ...
- 记一次线上Java程序导致服务器CPU占用率过高的问题排除过程
博文转至:http://www.jianshu.com/p/3667157d63bb,转本博文的目的就是需要的时候以防忘记 1.故障现象 客服同事反馈平台系统运行缓慢,网页卡顿严重,多次重启系统后问题 ...
- [Deep Learning] 正则化
在总结正则化(Regularization)之前,我们先谈一谈正则化是什么,为什么要正则化. 个人认为正则化这个字眼有点太过抽象和宽泛,其实正则化的本质很简单,就是对某一问题加以先验的限制或约束以达到 ...