0

Migrating from 'LightInject' to .netcore DI container.

What are the .netcore DI container equivalents of below LightInject related registrations?

 a. container.RegisterConstructorDependency<IBar>((factory, parameterInfo) => new Bar()); 

 b. container.RegisterInstance<Func<string, string>>
        ((username, password) => new MemCache(userId, password, container.GetInstance<IBusinessLogic>())); 
191180rk
  • 735
  • 2
  • 12
  • 37

1 Answers1

0

I believe A would be something like this:

services.AddTransient<IBar>(container => new Bar());

For B, if you have an instance that already exists that you simply want to register, then you could do something like:

ISomething somethingInstance = new Something();
services.AddSingleton<ISomething>(somethingInstance);

But it seems like you actually want to register a factory, so I would suggest something like this:

services.AddScoped<Func<string, string, MemCache>>(ctx => (username, password) => new MemCache(username, password, ctx.GetRequiredService<IBusinessLogic>()));
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86