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

  • Home
  • SEARCH
  • 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 164313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:43:19+00:00 2026-05-11T11:43:19+00:00

Is there any potential problem in setting datacontext as property like this: repository public

  • 0

Is there any potential problem in setting datacontext as property like this:

repository

public Repository() {      public DataContext dc {get;set;}      public GetOrders(int id)        { ...from dc.Orders...} } 

service layer:

public GetNewOrders()    {        ....        Repository rep=new Repository();        using {DataContext dc=new DataContext())         {             rep.dc=dc;            rep.GetOrders(id);         }     } 
  • 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. 2026-05-11T11:43:19+00:00Added an answer on May 11, 2026 at 11:43 am

    In DDD, you’re missing the bigger picture here by referencing the concret classes. You are not interfacing between the Repository and ‘Services layer’ by best practices. If you must have DataContext injected into the Repository, I would recommend refactoring to:

    public interface IRepository {   IList<Orders> GetNewOrders(); } public Repository : IRepository {   private IDataContext _dataContext;   public Repository(IDataContext dataContext)   {     _dataContext = dataContext;   }   public IList<Orders> GetNewOrders()   {     // perform your actions on _dataContext here   } } 

    The better solution would be to let the Repository handle the DataContext on its own – keeping the seperation of concert valid by masking the underlying requirements:

    public interface IRepository {   IList<Orders> GetNewOrders(); } public Repository : IRepository {   private IDataContext _dataContext;   public Repository(String connectionString)   {     _dataContext = new DataContext(connectionString);   }   public IList<Orders> GetNewOrders()   {     // perform your actions on _dataContext here   } }  

    If you must keep control of the DataContext (or another class) yourself (perhaps you want to keep a static reference around, or change settings based on an WebRequest, etc), you you will need to use a ‘Factory’.

    The factory would look something like this:

    public static class DataContextFactory {   public static IDataContext GetInstance()   {     // return either a static instance,      // or threaded instance, a GlobalContext instance     // or whatever your preference is here     //    } } 

    That way, you have full control over how the instance of DataContext is controlled outside and away from your ‘Services’ layer. So, you would use this DataContextFactory like the following:

    public interface IRepository {   IList<Orders> GetNewOrders(); } public Repository : IRepository {   public IList<Orders> GetNewOrders()   {     using (var dataContext = DataContextFactory.GetInstance())     {       // dataContext is now your IDataContext to work with     }   } }  

    ‘How to access the IRepository?’ you may ask?

    Your services layer would do something like:

    public void GetNewOrdersForServices() {   // Not recommended!   //      IRepository repo = new Repository()   //   // The following is recommended instead; because, it removes the   // the Concret reference from your Services layer completely!   //   IRepository repo = ServiceLocator.InstanceOf<IRepository>();   IList myList = repo.GetNewOrders();  } 

    Or, you would inject it into the constructor of your service using your favorite flavor of Inversion of Control container like so:

    public class OrderService {   private IRepository _repo;    public OrderService(IRepository repo)   {     _repo = repo;   }    public void GetNewOrdersForServices()   {     IList myList = _repo.GetNewOrders();    } 

    If you are not fimilar with the service locator concepts, check out Castle Windsor as it Encapsulates just about all your needs.

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

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.