Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7048427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:53:27+00:00 2026-05-28T02:53:27+00:00

I am working on a service, which performs database operations through a repository. In

  • 0

I am working on a service, which performs database operations through a repository. In the service I instantiate the repository which requires a database context in the constructor. I wanted to know if the context should be passed into the Service, or if the code below is fine. Or would it be better to pass a Repository object to the service for it to use? What should the UI code look like when using the Service class?

public class Service
    {
        private IRepository<WWW> _repository;

        public Service()
        {
            _repository = new Repository<WWW>(new DBContext());
        }

        public WWW GetWWW(int wwwID)
        {
            return _repository.Get(x => x.WWWID == wwwID).FirstOrDefault();
        }

        public void AddWWW(WWW www)
        {
            _repository.Add(www);
        }

        public void DeleteWWWByID(int wwwID)
        {
            _repository.Delete(x => x.WWWID == wwwID);
        }

        public void SaveChanges()
        {
            _repository.SaveChanges();
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:53:28+00:00Added an answer on May 28, 2026 at 2:53 am

    Indeed, it would be better to pass a repository to the Service via the Service’s constructor, like so:

    public class Service
    {
        private readonly IRepository<WWW> _repository;
    
        public Service(IRepository<WWW> repository)
        {
            _repository = repository;
        }
    
        /* the rest is unchanged */
    }
    

    Tipically, the class representing the UI would take a dependency on Service, so the code might look like this:

    public class UIClass : BaseClassDictatedByCurrentUIFramework
    {
        private readonly Service _service;
    
        public UIClass(Service service)
        {
            _service = service;
        }
    
        /* UI code that will eventually call methods on the service */
    }
    

    What you have to do next is configure an Inversion of Control container that will know how to resolve instances of IRepository (and feeding them appropriate DataContext instance, if needed).

    For example, if our UI code would be an MVC3 controller, we’d tell the container to resolve an instance of this controller. This is what happens:

    • The container notices the dependency on Service (in the constructor) and tries to resolve it.

    • Since Service is a concrete class, the container will attempt to resolve it and then
      notice the dependency on IRepository<WWW>.

    • The resolution of IRepository, since this is an interface, requires that the container has been previously set up to “know” what to return when it’s asked for an instance of it. Normally, this is just a mapping between the interface and a concrete implementation of it. In our case the concrete implementation is Repository<WWW> and the container is also responsible for “knowing” how to instantiate the needed DataContext instance for it (this also has to be previously configured)

    • Having a repository instance, the container is then able to properly instantiate first the Service, then the controller class.

    Note that the automatic resolution of concrete classes is a feature not all IoC containers have; some require explicit configuration in order to do so.

    Apart from this, I think the Service class does not add much value in your case. It only contains delegations to methods implemented by the repository. In this case it could be better to have the UI take a dependency on the IRepository<WWW> directly and simply delete the Service class.
    However, if this Service class was just an example and in your real project it implements actual business rules, then it should be kept.

    Update: How to resolve dependencies in ASP.Net Webforms

    The example I presented above is the ideal Dependency Injection scenario. It would work for example in ASP.NET MVC, where BaseClassDictatedByCurrentUIFramework would be Controller – in that case the framework allows us to control the components that instantiate Controllers, so we can inject our own dependencies in the constructor.

    However, ASP.Net WebForms is not a very DI-friendly framework. It requires that every Page is required to have a default constructor, which makes all the constructor injection idea not suitable.

    In this case one possible solution would be the following compromise:

    • instantiate the IoC container at application startup (in Globals.asax) and make it available via some global state (for example, the Application context)
    • in a Page class declare dependencies as private readonly fields, but the constructor will not have corresponding parameters (it will have no parameters at all)
    • in the constructor body:
      • get a reference to the IoC container (available from global state)
      • use the container to resolve all dependencies the class has

    Please note that this approach requires discipline – it is easy to use the container somewhere else that the constructor to resolve other components. This approach is called Service Locator, it’s considered an antipattern (http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx) and should therefore be avoided.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on our back-end encoder service which does an awful lot of video
I'm working on a self-hosted WCF service for which encrypted communications is an option.
I am working on an application which starts as a service but only if
I am working on an app which calls a rest web service. Sometimes the
I am working on one real estate website which is Using RETS service to
I am currently working on a REST service. This service has an entity which
I'm working on an application which uses a bootscrapper object to perform some operations
I'm working on a background service which needs to ask several users' iTunes settings
I'm working on a simple web service which exports data from some data store
I'm working with one WCF service which will be consuming records I queue up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.