IProductRepositoryProxy ProductDataServiceProviderInstance = new ServiceProductDataProvider();
builder.RegisterInstance(ProductDataServiceProviderInstance).As<IProductRepositoryProxy>();
Run Code Online (Sandbox Code Playgroud)
VS
builder.RegisterType<ServiceProductDataProvider>().As<IProductRepositoryProxy>().InstancePerRequest();
Run Code Online (Sandbox Code Playgroud)
我在这里看到了来自前雇员的代码,并想知道这个人是否想要注册.SingleInstance()行为.
builder.RegisterType<ServiceProductDataProvider>().As<IProductRepositoryProxy>().SingleInstance();
Run Code Online (Sandbox Code Playgroud)
使用RegisterInstance的ServiceProductDataProvider的手动新增与Register .SingleInstance()不同吗?
我试图使用通用的Lazy类来实例化.net核心依赖注入扩展的昂贵类.我已经注册了IRepo类型,但我不确定Lazy类的注册是什么样的,或者它是否支持.作为一种解决方法,我使用了这种方法http://mark-dot-net.blogspot.com/2009/08/lazy-loading-of-dependencies-in-unity.html
配置:
public void ConfigureService(IServiceCollection services)
{
services.AddTransient<IRepo, Repo>();
//register lazy
}
Run Code Online (Sandbox Code Playgroud)
控制器:
public class ValuesController : Controller
{
private Lazy<IRepo> _repo;
public ValuesController (Lazy<IRepo> repo)
{
_repo = repo;
}
[HttpGet()]
public IActionResult Get()
{
//Do something cheap
if(something)
return Ok(something);
else
return Ok(repo.Value.Get());
}
}
Run Code Online (Sandbox Code Playgroud) c# dependency-injection lazy-initialization .net-core asp.net-core
我想知道注册容器本身是否有任何副作用
IContainer container;
ContainerBuilder builder = new ContainerBuilder();
container = builder.Build();
builder.RegisterInstance(container).As<IContainer>();
Run Code Online (Sandbox Code Playgroud)
并使用它像这样
builder.RegisterType<IManagmentServiceImp>().As<ManagmentServiceImp>()
.WithParameter(new ResolvedParameter(
(pi, ctx) => pi.ParameterType == typeof(IContainer) && pi.Name == "Container",
(pi, ctx) => container
));
Run Code Online (Sandbox Code Playgroud)
或者它是否会起作用.
让我们说我有一个clas
class Foo : FooBase {
public Foo(Settings settings, IDbRepository db)
: base(settings) {
this.db = db;
}
...
}
Run Code Online (Sandbox Code Playgroud)
基本上FooBase通过构造函数接收设置并从配置文件加载一些配置.
然后我有实现IDbRepository的类MySQLRepository
class MySQLRepository : IDbRepository {
...
public MySQLRepository(IConfigurationRepository config) {
conn = new MySQLConnection(config.GetConnectionString());
}
...
}
Run Code Online (Sandbox Code Playgroud)
在Program.cs我有:
Foo foo = container.Resolve<Foo>();
Run Code Online (Sandbox Code Playgroud)
问题是只有在加载了所有其他依赖项之后才调用FooBase的构造函数.但是在调用FooBase构造函数之前不会加载配置.
我的想法是创建一个IDbRepository和任何其他需要配置的接口的惰性实现.
这是一个好主意吗?我如何使用Unity容器实现它?
我有以下服务类:
public class JobService {
private UserService us;
public JobService (UserService us) {
this.us = us;
}
public void addJob(Job job) {
// needs to make a call to user service to update some user info
// similar dependency to the deleteUser method
}
}
public class UserService {
private JobService js;
public UserService(JobService js) {
this.js = js;
}
public void deleteUser(User u) {
using (TransactionScope scope = new TransactionScope()) {
List<IJob> jobs = jobService.findAllByUser(u.Id);
foreach (IJob job in …Run Code Online (Sandbox Code Playgroud) c# dependency-injection circular-dependency inversion-of-control
如果我把Lazy放在我的对象的构造函数中,并且X没有在容器中注册,我得到了依赖项解析异常.
为什么我得到这个例外?我不喜欢它,因为我无法在运行时选择组件.用例示例:
class Controller
{
public Controller(Lazy<A> a, Lazy<B> b) { /* (...) */ }
Lazy<A> a;
Lazy<B> b;
public IActionResult Get(){
if(someConfig)
return Json(a.Value.Execute());
else
return Json(b.Value.Execute());
}
}
Run Code Online (Sandbox Code Playgroud)
为此,我需要注册组件A和B.即使从未使用过B,我的程序也会失败.我希望B是可选的,仍然由autofac管理.
如果我有组件列表,并且只想使用一个组件,这就是更大的问题.例如:
class Controller
{
Controller(IEnumerable<Component> components) { /* (...) */ }
IActionResult Get()
{
return components.First(n => n.Name == configuredComponent).Execute();
}
}
Run Code Online (Sandbox Code Playgroud)
我不再得到异常是没有注册的东西,但是一切都是构建的.使用起来也很尴尬.