0

I use repository method to get all data from DB.

Here is the code of it:

      public class ExperienceRepository
    {
        private readonly ToSeeDatabaseContext _context;
        public ExperienceRepository(ToSeeDatabaseContext context) 
        {
            _context = context;
        }

        public List<Experience> GetAllExperiences()
        {
            return _context.Experience.ToList();
        }
}

I need to call GetAllExperience from controller.

So at first I need to declare repo as private property

I do it like this

 private ExperienceRepository _exprepo = new ExperienceRepository();

But it says, it need

Severity Code Description Project File Line Suppression State Error CS7036 There is no argument given that corresponds to the required formal parameter 'context' of 'ExperienceRepository.ExperienceRepository(ToSeeDatabaseContext)' TooSeeWeb C:\Users\EugeneSukhomlyn\source\Workspaces\TooSee\Too See Web\Too See Web\Too See Web\Controllers\ExperienceController.cs 14 Active

How I can solve it?

7
  • 1
    Pass it an instance of ToSeeDatabaseContext... Commented Feb 22, 2018 at 13:13
  • 1
    Are you using dependency injection? Commented Feb 22, 2018 at 13:14
  • Yes, It's DI@Sefe Commented Feb 22, 2018 at 13:16
  • DI and new don't work together. Commented Feb 22, 2018 at 13:17
  • 1
    That's basic C# syntax, try reading a tutorial or two. That'd be new ExperienceRepository(new ToSeeDatabaseContext()). Or add ExperienceRepository to your controller's constructor as a parameter and let your DI container inject that. Commented Feb 22, 2018 at 13:26

2 Answers 2

2

Since you are using dependency injection, the preferred way would be to inject the DB context to the repository.

You probably have already code similar to this in the ConfigureServices method of your Startup.cs file (or in the place where you configure your service collection if you are not using ASP.NET Core) to set up the context for dependency injection (if you don't you should add it):

services.AddDbContext<ToSeeDatabaseContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("ToSeeDatabaseContext")));

Since your experience repository already accepts ToSeeDatabaseContext, it is already ready for dependency injection. Now you have to inform the DI framework about ExperienceRepository, so that it can inject it to its consumers. So in ConfigureServices you add:

services.AddTransient<ExperienceRepository, ExperienceRepository>();

Now can use dependency injection whenever you want to need the repository. In your consumer (eg. an ASP.NET page) you can use constructor injection to get a repository:

class MyExperienceConsumer {
    private ExperienceRepository _exprepo;
    public MyExperienceConsumer(ExperienceRepository exprepo) {
        _exprepo = exprepo;
    }
}

If your consumer is an ASP.NET page controller, this is all you need to do, since the MVC framework will create the controller for you and use DI to give you the repository. If you need to instantiate the consumer yourself you need to do so with the help a service provider from the DI framework, so that it can do its magic (assuming you have a service collection). When you use ActivatorUtilities, the DI framework will inject the repository into the constructor:

IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
MyExperienceConsumer consumer =
    ActivatorUtilities.CreateInstance<MyExperienceConsumer>(serviceProvider);

In any case, you can use the DI framework to do the heavy lifting for you.

Sign up to request clarification or add additional context in comments.

Comments

0

Your ExperienceRepository class have one constructor that requires a ToSeeDatabaseContext as parameter.

You are trying to create a instance ExperienceRepository with no parameters. The compiler can't find a constructor which doesn't take any parameters, producing the compiler error.

2 Comments

So as I understood, I can add it into Controller constructor like this private ExperienceRepository _repo; public ExperienceController(ExperienceRepository repo) { _repo = repo; }?
Yes i think theres no problem.

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.