简体   繁体   中英

How to inject a specific implementation in asp.net core

I have a Repository which has a dependency of User

I need to put other implementation of User and I can do it like that, but with this approach I do not know how to mock my repository

private readonly IRepository<Entity> _repository;

public SomeClass(MyAppDbContext context)
{
  _repository= new EfRepository<WorkOrder>(context, new User());
}

Is there a way to inject a specific implementation of User here, or how I can test the code I wrote

Just as you inject MyAppDbContext into SomeClass you can also inject an instance of User , eg

private readonly IRepository<Entity> _repository;

public SomeClass(MyAppDbContext context, User user)
{
  _repository= new EfRepository<WorkOrder>(context, user);
}

You can either register User in the IoC like this:

services.AddTransient<User>();

In case you have already registered a service for User and want to use another instance, you can register a factory method for SomeClass that sets up the User instance:

services.AddScoped<SomeClass>(prov => new SomeClass(
  prov.GetRequiredService<MyAppDbContext>(), 
  new User()));

The factory method approach is viable if you only have a few spots that need the special instance, otherwise you can use this approach: Unlike other IoCCs, the .NET Core IoCC does not support named registrations, but you can also use some kind of "marker interface" to register another instance:

public interface ISpecialUser : IUser {}

public class User : IUser
{
  // ...
}

public class AnotherUser : ISpecialUser
{
  // ...
}

// ...
public SomeClass(MyAppDbContext context, ISpecialUser user)
{
  _repository= new EfRepository<WorkOrder>(context, user);
}


// ...
services.AddScoped<IUser, User>();
services.AddScoped<ISpecialUser, AnotherUser>();

In the tests, you can set up an instance of User that suits your needs and use the new constructor parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM