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 6102827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:39:33+00:00 2026-05-23T13:39:33+00:00

Continuing on from these debates: DDD – the rule that Entities can't access Repositories

  • 0

Continuing on from these debates:

DDD – the rule that Entities can't access Repositories directly

Is it ok for entities to access repositories?

There are still some situations where it feels better for the Domain to access the repository. Take this example, which assumes I need a TaskStatus table in the database which contains a description for reporting purposes:

public class TaskStatus
{
    public long Id {get;set;}
    public string Description {get;set;}
}

public class Task
{
    public long Id {get;set;}
    public string Description {get;set;}
    public TaskStatus Status {get;set;}

    public void CompleteTask()
    {
        ITaskStatusReposity repository = ObjectFactory.GetInstace<ITaskStatusReposity>(); //Or whatever DI you do.
        Status = repository.LoadById(Constants.CompletedTaskStatusId);
    }
}

I know I could have CompletedTaskStatus and OpenTaskStatus objects but there are situations where this would be unnecessary and could lead to a class explosion.

And anyway, why are repository interfaces stored in the Domain, if not for this sort of thing?

  • 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-23T13:39:33+00:00Added an answer on May 23, 2026 at 1:39 pm

    Is it ok for Entities to access Repositories?

    No. Please don’t do this.

    Firstly the idea of the Domain Model is to separate out the business logic from the application. Isolate it from your db, repositories and your application. This allows you to keep the business logic separate and to allow it to be tested and changed separate from your application.

    The Domain should be completely unaware of data persistence, and should assume that it happens automagically.

    ddd-the-repository-pattern.aspx

    Secondly there’s other more practical reasons not to inject your repositories into your enties.

    1. Your entites should be unit testable, by injecting your repositories into your entity you’ve created a dependency on your repository.

    2. Using a GetInstance() method breaks the Law of Demeter, you are creating a tight coupling of ITaskStatusRepository to your entity. Meaning that when creating a new Task and writing unit tests it is not obvious opon construction that a Task requires a ITaskStatusRepository. This makes it more difficult to unit test your business logic.

    3. From a DDD standpoint, a repository is not only concerned with interfacing with the DB, it could be retrieving from an in memory store. Or a List.

    4. Your repository does not have to be a 1 to 1 relationship with tables. If you need your task repository to perform inner joins with other tables to perform complex queries and it returns a list of Task items, then you expose a method from your repository that performs that query. (I think this is a common misconception of the repository pattern).

    5. Your entities should not be concerned with performing actions on the db.

    Refer to the image posted here:

    DDD: how the layers should be organized?

    public class TaskStatus
    {
       public long Id { get; set; }
       public string Description { get; set; }
    
       public TaskStatus() {
          Description = "Incomplete";
       }
    }
    
    public class Task
    {
        public long Id {get;set;}
        public string Description {get;set;}
        public TaskStatus Status {get;set;}
    
        public void CompleteTask()
        {        
            Status.Description = "Complete";
        }
    }
    

    Inside the application layer, your repository is responsible for persistence (or not). A repository is a List<> of aggregate roots. And it works at the aggregate root level.

    An example of using your Tasks inside a TaskService. A service acts upon Entities from your Application Layer.

    public class TaskService 
    {
        private readonly ITaskRepository _taskRepository;
    
        public TaskService(ItaskRepository taskRepository){
          _taskRepository = taskRepository;
        }
    
        public List<Task> CompleteAllTasks()
        {
          List<Tasks> getTasks = _taskRepository.GetTasks();
          getTasks.ForEach(CompleteTask);
          return _taskRepository.Save(getTasks);
        }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Continuing from my previous question , is there a comprehensive document that lists all
Continuing my problem from yesterday, the Silverlight datagrid I have from this issue is
I have a solution containing several projects that have migrated from VS 2003, 2005,
Let's say you got a file containing texts (from 1 to N) separated by
I have a object built through a factory containing my parameters read from the
Continuing my investigation of expressing F# ideas in C#, I wanted a pipe forward
Due to continuing crash problems, I'm about to uninstall and reinstall my copy of
I have a dl containing some input boxes that I clone with a bit
I have a column containing the strings 'Operator (1)' and so on until 'Operator
I have a table containing prices for a lot of different things in a

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.