What's the Microsoft DI equivalent of Castle.Windsor's DependsOn Dependency.OnValue

464 Views Asked by At

I have a .NET Standard 2.0 library which has DI set up in the following way,

container.Register(Component.For<IMyFactory>()
    .ImplementedBy<MyFactory>()
    .DependsOn(Dependency.OnValue("connectionString",
        container.Resolve<IDataAccess>().ConnectionString))
    .LifestyleSingleton());

container.Register(Component.For<IMyAdapter>()
    .ImplementedBy<MyAdapter>().LifestyleTransient());

What are the equivalent of these statements in Microsoft.Extensions.DependencyInjection?

I want to move away from Castle.Windsor dependencies.

1

There are 1 best solutions below

3
Steven On BEST ANSWER
services.AddSingleton<IMyFactory>(sp =>
    ActivatorUtilities.CreateInstance<MyFactory>(
        sp,
        sp.GetRequiredService<IDataAccess>().ConnectionString));

services.AddTransient<IMyAdapter, MyAdapter>();