当前位置:网站首页>Autofac浅学(2)
Autofac浅学(2)
2022-06-12 14:18:00 【有诗亦有远方】
Autofac浅学
一、注册概念
使用Autofac 注册组件,通过创建一个ContainerBuilder并且告知builder 哪些组件公开哪些服务。
//注册
//ConsoleLogger服务
//ILogger 组件
//意思为ILogger组件公开了ConsoleLogger服务
builder.RegisterType<ConsoleLogger>().As<ILogger>();
基本类
Person
public interface Person
{
void Eat();
void Say();
}
Teacher
public class Teacher:Person
{
public void Eat()
{
Console.WriteLine("Tea吃早饭");
}
public void Say()
{
Console.WriteLine("Tea说早安");
}
}
Student
public class Student:Person
{
public void Eat()
{
Console.WriteLine("吃早饭");
}
public void Say()
{
Console.WriteLine("说早安");
}
二、反射组件
#region 通过反射实现
//创建容器
var builder = new ContainerBuilder();
//创建builder,通过反射并在builder中注册类型。
builder.RegisterType(typeof(Student));
//实例化容器(可以讲第一步合并进来)
Container = builder.Build();
//在需要使用接口的地方,通过container来解析得到一个借口的实例。
Student student = Container.Resolve<Student>();
student.Say();
#endregion
三、实例组件
有时候,您可能希望提前生成一个对象的实例,并将它添加到容器供组件注册使用。你可以这样做,使用RegisterInstance 方法:
//创建容器
var builder = new ContainerBuilder();
//创建builder,并在builder中注册类型。
var student = new Student();
builder.RegisterInstance(student).As<Person>().ExternallyOwned();
//实例化容器(可以讲第一步合并进来)
Container = builder.Build();
//在需要使用接口的地方,通过container来解析得到一个借口的实例。
Person person = Container.Resolve<Person>();
person.Eat();
person.Say();
四、默认注册
#region
var builder = new ContainerBuilder();
//Person注册两个服务,因为Teacher是后注册的,所以默认是Teacher服务
builder.RegisterType<Student>().AsSelf().As<Person>();
builder.RegisterType<Teacher>().AsSelf().As<Person>();
Container = builder.Build();
Person person = Container.Resolve<Person>();
person.Eat();
person.Say();
#endregion
//添加.PreserveExistingDefaults()修改默认,变为Student服务
builder.RegisterType<Teacher>().AsSelf().As<Person>().PreserveExistingDefaults();
五、传递注册参数
Autofac提供数个不同的参数匹配策略:
NamedParameter - 通过名称匹配目标参数
TypedParameter - 通过类型匹配目标参数(需要精确匹配类型)
ResolvedParameter - 灵活的参数匹配
Cat
public class Cat:Animal
{
public string Name {
get; set; }
public Cat(string name)
{
Name = name;
}
public void Eat()
{
Console.WriteLine("Cat吃早饭");
}
public void Say()
{
Console.WriteLine("Cat说早安");
}
public void ConsoleName()
{
Console.WriteLine(Name);
}
}
1.NamedParameter
#region 传递注册参数
var builder = new ContainerBuilder();
//AsSelf()在注册组件同时,注册自己的服务
builder.RegisterType<Cat>().AsSelf().As<Animal>().WithParameter("name", "lty");
Container = builder.Build();
var instance = Container.Resolve<Cat>();
instance.Eat();
instance.Say();
instance.ConsoleName();
#endregion
2.TypedParameter
//对比
builder.RegisterType<Cat>().AsSelf().As<Animal>().WithParameter("name", "lty");
//TypedParameter
builder.RegisterType<Cat>().AsSelf().As<Animal>().WithParameter(new TypedParameter(typeof(string), "lty"));
3.ResolvedParameter
//对比
builder.RegisterType<Cat>().AsSelf().As<Animal>().WithParameter("name", "lty");
//ResolvedParameter
builder.RegisterType<Cat>().AsSelf().As<Animal>()
.WithParameter(
new ResolvedParameter(
(pi, ctx) => pi.ParameterType == typeof(string) && pi.Name == "name",
(pi, ctx) => "lty"
)
);
lamda注册
//对比
builder.RegisterType<Cat>().AsSelf().As<Animal>().WithParameter("name", "lty");
//lamda
builder.Register(c => new Cat("lty")).As<Animal>();
边栏推荐
- Remote code injection
- QT link error: undefined reference to VTable for "XXX“
- JS (III) convert ES6 syntax to Es5 syntax
- Appnium (II) installation and basic use of mitmproxy
- Crawler problem set (I) record
- 华为设备配置BGP AS号替换
- And, or, not equal, operator
- C secret script Chapter 1: data storage (in-depth analysis)
- Leetcode 2176. 统计数组中相等且可以被整除的数对
- If you want to build brand awareness, what bidding strategy can you choose?
猜你喜欢

工业机械臂(机器人)视觉定位引导系统

How to use Android studio to create an Alibaba cloud Internet of things app

Machine learning learning notes

Ppt cannot be opened, always prompt how to fix it

If you want to build brand awareness, what bidding strategy can you choose?

Leetcode 2185. Counts the string containing the given prefix

Is Shell Scripting really a big technology?

Soft test (VI) Chrome browser installation selenium IDE

Software package for optimization scientific research field

Redis核心配置和高级数据类型
随机推荐
Design of PLC intelligent slave station based on PROFIBUS DP protocol
Variable parameters
Software package for optimization scientific research field
The original Xiaoyuan personal blog project that has been around for a month is open source (the blog has basic functions, including background management)
Program analysis and Optimization - 6 loop optimization
C secret arts script Chapter 2 (detailed explanation of pointers) (Section 3)
Remote code injection
C secret arts script Chapter 5 (paragraph) (Section 3)
对某热水软件的加密参数逆向
Llvm 13.1 new pass plug-in form [for win]
[MySQL] basic database operation
SystemC:SC_ Thread and SC_ METHOD
Mobileone: the mobile terminal only needs 1ms of high-performance backbone. You deserve it!
C secret script Chapter 1: data storage (in-depth analysis)
The difference between parameter and argument in C language
Player practice 11 audio resampling
Printing colored messages on the console with printf
Dynamic search advertising intelligent search for matching keywords
Appnium (I) basic use of appnium
Three common methods of C language array initialization ({0}, memset, for loop assignment) and their principles