IoC using Autofac

I am just starting to work with IoC frameworks and am playing with Autofac.

In the following code example, where I register 2 completely different classes (in global.asax) that implement the same interface, I wonder how we guarantee that the correct Autofac is used? Currently, one of my controllers that accepts IPhotoBlogRepository as their constructor is either passing PhotoBlogRepositoryOR a TestRepository, depending on which appears first / last in the code below.

builder.RegisterType<PhotoBlogRepository>().As<IPhotoBlogRepository>();
builder.RegisterType<TestRepository>().As<IPhotoBlogRepository>();
+3
source share
2 answers

. , . , , .

, IEnumerable<IPhotoBlogRepository>. Autofac , .

, , Autofac .

: , . SUT ( ) , . "" , .

+7

, , , , , , , PreserveExistingDefaults() , :

builder.RegisterType<PhotoBlogRepository>().As<IPhotoBlogRepository>();
builder.RegisterType<TestRepository>().As<IPhotoBlogRepository>().PreserveExistingDefaults();

- , PreserveExistingDefaults().

+2

Source: https://habr.com/ru/post/1778794/


All Articles