0

I have an interface IRepository<TEntity>, and a generic repository Repository<TEntity, TContext>. GameRepository and UserRepository are extended from Repository<TEntity, TContext>. Instead of adding the repositories manually, I want to do it in one line. I've tried the one-line solution from this answer, but it doesn't seem to work in my case.

        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers();

            services.AddDbContext<ApplicationDBContext>(options =>
                options.UseMySql(Configuration.GetConnectionString("DefaultConnection"))
            );

            services.AddScoped<GameRepository>();
            services.AddScoped<UserRepository>();
        }
2
  • Do you mean you want to use just one line to register GameRepository and UserRepository? In my opinion, we couldn't do that, since the GameRepository and UserRepository is different class. We couldn't use DI to new two different class in your services. Commented Aug 26, 2020 at 12:58
  • What are the other solutions that can do the similar thing? Commented Aug 27, 2020 at 10:26

1 Answer 1

0

What are the other solutions that can do the similar thing?

As far as I know, there is no way to do similar thing, if you still want to use one line, I suggest you could consider create a service connection extension method.

You could create a ServicesConfiguration class like below:

public static class ServicesConfiguration
{
    public static void AddCustomServices(this IServiceCollection services)
    {
        services.AddScoped<GameRepository>();
        services.AddScoped<UserRepository>();
    }
}

Then you could register this service by using just one line:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddCustomServices();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.