Я пытаюсь создать базовое приложение ASP. NET, используя общий шаблон репозитория c, используя Entity Framework вместе со слоем сервиса. Однако всякий раз, когда я запускаю свой проект, я получаю сообщение об ошибке:
Не удается разрешить службу для типа 'GameSource.Data.Repositories. GameRepository ' при попытке активировать ' GameSource.Services. GameService '.
GamesController.cs :
public class GamesController : Controller
{
private IGameService gameService;
public GamesController(IGameService gameService)
{
this.gameService = gameService;
}
}
GameService.cs:
public class GameService : IGameService
{
private GameRepository gameRepo;
public GameService(GameRepository gameRepo)
{
this.gameRepo = gameRepo;
}
}
GameRepository.cs:
public class GameRepository : BaseRepository<Game>, IGameRepository
{
private GameSource_DBContext context;
private DbSet<Game> gameEntity;
public GameRepository(GameSource_DBContext context) : base(context)
{
this.context = context;
gameEntity = context.Set<Game>();
}
}
BaseRepository.cs :
public class BaseRepository<T> : IBaseRepository<T> where T : class
{
private GameSource_DBContext context;
private DbSet<T> entity;
public BaseRepository(GameSource_DBContext context)
{
this.context = context;
entity = context.Set<T>();
}
}
GameSource_DBContext.cs:
public class GameSource_DBContext : DbContext
{
public GameSource_DBContext(DbContextOptions<GameSource_DBContext> options) : base(options)
{
}
public DbSet<Game> Game { get; set; }
public DbSet<Genre> Genre { get; set; }
public DbSet<Developer> Developer { get; set; }
public DbSet<Publisher> Publisher { get; set; }
public DbSet<Platform> Platform { get; set; }
}
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews().AddRazorRuntimeCompilation();
services.AddDbContext<GameSource_DBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("GameSource_DB")));
services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
services.AddScoped<IGameRepository, GameRepository>();
services.AddScoped<IGameService, GameService>(); //Can't resolve service?
}
Как вы можете видеть при запуске, я пытаюсь внедрить мой сервис, а также свой репозиторий, но он не может разрешить сервис для GameRepository при попытке активировать GameService. Я что-то пропустил? Является ли это правильным подходом для использования с Entity Framework, например, уровни repo + service?
Спасибо за ваше терпение.