微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – Autofac RegisterInstance vs SingleInstance

IProductRepositoryProxy ProductDataServiceProviderInstance = new ServiceProductDataProvider();
builder.RegisterInstance(ProductDataServiceProviderInstance).As<IProductRepositoryProxy>();

VS

builder.RegisterType<ServiceProductDataProvider>().As<IProductRepositoryProxy>().InstancePerRequest();

我从前员工那里看到这个代码,想知道这个人是否想注册一个.SingleInstance()行为.

builder.RegisterType<ServiceProductDataProvider>().As<IProductRepositoryProxy>().SingleInstance();

RegisterInstance的ServiceProductDataProvider的手动新增与Register的不同.SingleInstance()?

解决方法

Is the manual newing-up of the ServiceProductDataProvider with RegisterInstance not the same as the Register .SingleInstance() ??

RegisterInstance允许您在AutoFac中注册一个实例.

RegisterInstance和RegisterType SingleInstance方法的区别在于RegisterInstance方法允许您注册一个非Autofac构建的实例.

但是这两种解决方案都将导致在Autofac中注册单例.

顺便说一句,两个注册在下面的代码示例中是等效的

var instance = GetInstanceFromSomewhere(); 

builder.RegisterInstance<IService>(instance); 
builder.Register(c => instance).As<IService>().SingleInstance();

原文地址:https://www.jb51.cc/csharp/96942.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐