Using AutoFac
第一次整理了下关于autofac的一些具体的用法
1. 安装 autofac: install-package autofac -version 4.8.1
2. 创建两个类库项目,iservice (用于编写接口),serviceimpl(用于创建实现类)
- iservice 下
public interface ianimalbark
{
void bark();
}
public interface ianimalsleep
{
void sleep();
}
public interface iuser
{
void addnew(string name, string pwd);
}
public interface ischool
{
void afterschool();
}
- serviceimpl下
public class dog : ianimalbark,ianimalsleep
{
public void bark()
{
console.writeline("汪汪汪汪汪");
}
public void sleep()
{
console.writeline("zz,睡着了");
}
}
public class cat : ianimalbark
{
public void bark()
{
console.writeline("喵喵喵");
}
}
public class user : iuser
{
public void addnew(string name, string pwd)
{
console.writeline("添加新的用户:" + name);
}
}
public class school : ischool
{
public ianimalbark dog { get; set; }
public void afterschool()
{
dog.bark();
console.writeline("放学了");
}
}
3. 原理性使用方法,如果再有一个接口和一个实现类,那就再注册一次
containerbuilder builder = new containerbuilder();
//注册实现类dog,当我们 请求ianimalbark接口 的时候返回的是类dog的对象,原理性的代码
builder.registertype<dog>().as<ianimalbark>();
//上面一句也可改成下面一句,这样 请求dog实现的任何接口 的时候都会返回dog对象,原理性的代码
//builder.registertype<dog>().asimplementedinterfaces();
icontainer container = builder.build();
//请求ianimalbark接口
ianimalbark dog = container.resolve<ianimalbark>();
dog.bark();
console.readkey();
4. 如果有很多接口,很多实现类,每次都要注册一次会很麻烦,可以如下进行一次注册
containerbuilder builder = new containerbuilder();
assembly asm = assembly.load("service");//实现类所在的程序集名称
builder.registerassemblytypes(asm).asimplementedinterfaces();//常用
icontainer container = builder.build();
ianimalbark dog = container.resolve<ianimalbark>();
iuser user = container.resolve<iuser>();
dog.bark();
user.addnew("baidu","123");
console.readkey();
5. 如果有多个实现类,container.resolve<ianimalbark>();只会返回其中一个对象,如果想返回多个类的对象,应改成container.resolve<ienumerable<ianimalbark>>();
containerbuilder builder = new containerbuilder();
assembly asm = assembly.load("service");//实现类所在的程序集名称
builder.registerassemblytypes(asm).asimplementedinterfaces();//常用
icontainer container = builder.build();
ienumerable<ianimalbark> animals = container.resolve<ienumerable<ianimalbark>>();
foreach(var animal in animals)
{
animal.bark();
}
console.readkey();
6. 如果一个实现类中定义了其他类型的属性(接口类型的属性),在注册时又加上builder.registerassemblytypes(asm).asimplementedinterfaces().propertiesautowired();这样会给属性进行“注入”
containerbuilder builder = new containerbuilder();
assembly asm = assembly.load("service");//实现类所在的程序集名称
builder.registerassemblytypes(asm).asimplementedinterfaces().propertiesautowired();//常用
icontainer container = builder.build();
ischool school = container.resolve<ischool>();
school.afterschool();
console.readkey();
7. 可以通过在builder.registerassemblytypes(asm)后面以 instance***()配置来实现auto对象的生命周期
- instanceperdependency()每次resolve都返回新的对象
containerbuilder builder = new containerbuilder();
assembly asm = assembly.load("service");//实现类所在的程序集名称
builder.registerassemblytypes(asm).asimplementedinterfaces().propertiesautowired().instanceperdependency();//常用
icontainer container = builder.build();
ischool school = container.resolve<ischool>();
ischool school2 = container.resolve<ischool>();
console.writeline(school.equals(school2));
console.readkey();
- singleinstance()每次resolve都返回同一个对象,推荐
containerbuilder builder = new containerbuilder();
assembly asm = assembly.load("service");//实现类所在的程序集名称
builder.registerassemblytypes(asm).asimplementedinterfaces().propertiesautowired().singleinstance();//常用
icontainer container = builder.build();
ischool school = container.resolve<ischool>();
ischool school2 = container.resolve<ischool>();
console.writeline(object.referenceequals(school,school2));
console.readkey();
上一篇: Linux的find命令
推荐阅读
-
ERROR 1045 (28000): Access denied for user root@localhost (using password: NO)
-
MYSQL ERROR 1045 (28000): Access denied for user (using password: YES)问题的解决
-
JQUERY JSON的反序列化类 using问题的解决方法
-
连接本地数据库出现:1045 - Access denied for user 'root'@localhost'(using pass
-
ERROR 1045: Access denied for user: root@localhost (Using password: NO)
-
不能忽略c#中的using和as操作符的用处
-
asp.net core3.0 mvc 用 autofac
-
How to debug ASP.NET MVC using source code
-
谈C# using的用法与好处
-
MySQL:Unsafe statement written to the binary log using statement format since BI