当前位置:网站首页>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>();
边栏推荐
- Pay attention to click and pursue more users to enter the website. What bidding strategy can you choose?
- ADB command (I) record
- Server concurrency - note 1
- C secret script Chapter 3 (detailed explanation of string function) (Section 2)
- Appnium (II) installation and basic use of mitmproxy
- Interview (XI) futu written test questions
- 新技术:高效的自监督视觉预训练,局部遮挡再也不用担心!
- [ROC] aspriseocr C # English, Digital identification (not Chinese)
- C語言中主函數調用另外一個函數,匯編代碼理解
- 工业机械臂(机器人)视觉定位引导系统
猜你喜欢

我愿称之为史上最全的深度学习面经总结(附答案详解)

Two methods of QT using threads

Redis核心配置和高级数据类型

Player actual combat 22 to solve the problems of flower screen and Caton

Des File Encryptor based on MFC framework

What is automatic bidding? What are its advantages?

Tlm/systemc: TLM socket binding problem

Player actual combat 14 display YUV

Llvm pass-- virtual function protection

Player actual combat 25 unpacking module add close
随机推荐
使用make方法创建slice切片的坑
Appnium (II) installation and basic use of mitmproxy
Postgresql14 installation and use tutorial
C secret arts script Chapter 2 (detailed explanation of pointer) (Section 1)
Word insert picture blocked by text
Leetcode 2176. 统计数组中相等且可以被整除的数对
Codeforces Round #798 (Div. 2)(A~D)
Des File Encryptor based on MFC framework
IAT hook hijacking process API call
华为设备配置BGP AS号替换
And, or, not equal, operator
Two methods of implementing asynchronous calling function with QT
To SystemC Beginners: the first program
Is Shell Scripting really a big technology?
QT to realize the simple use of SQLite database
String concatenation and ternary operators
你敢信?開發一個管理系統我只用了兩天
Introduction to functions (inline functions and function overloading)
测试工程师如何转型测开
The original Xiaoyuan personal blog project that has been around for a month is open source (the blog has basic functions, including background management)