, BookService DatabaseRepository . .
:
var service = _container.Resolve<IBookService>(new ParameterOverride("repository", _container.Resolve<IBookRepository>("Database")));
, - , :
_container = new UnityContainer();
_container.RegisterType<IBook, Book>();
if (useDatabase)
{
_container.RegisterType<IBookRepository, DatabaseRepository>();
}
else
{
_container.RegisterType<IBookRepository, BookRepository>();
}
_container.RegisterType<IBookService, BookService>();
, .
, :
_container.RegisterType<IBookService, BookService>(
new InjectionConstructor(
new ResolvedParameter<IBookRepository>("Database")
)
);
This will allow the container to resolve IBookServiceusing the constructor with a single parameter IBookRepositoryand resolve IBookRepositorywith the name Database.
source
share