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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:03:10+00:00 2026-05-14T03:03:10+00:00

Just created an acc on SO to ask this :) Assuming this simplified example:

  • 0

Just created an acc on SO to ask this 🙂

Assuming this simplified example: building a web application to manage projects…
The application has the following requirements/rules.

1) Users should be able to create projects inserting the project name.
2) Project names cannot be empty.
3) Two projects can’t have the same name.

I’m using a 4-layered architecture (User Interface, Application, Domain, Infrastructure).
On my Application Layer i have the following ProjectService.cs class:

public class ProjectService
{
    private IProjectRepository ProjectRepo { get; set; }

    public ProjectService(IProjectRepository projectRepo)
    {
        ProjectRepo = projectRepo;
    }

    public void CreateNewProject(string name)
    {
        IList<Project> projects = ProjectRepo.GetProjectsByName(name);
        if (projects.Count > 0) throw new Exception("Project name already exists.");

        Project project = new Project(name);
        ProjectRepo.InsertProject(project);
    }
}

On my Domain Layer, i have the Project.cs class and the IProjectRepository.cs interface:

public class Project
{
    public int ProjectID { get; private set; }
    public string Name { get; private set; }

    public Project(string name)
    {
        ValidateName(name);
        Name = name;
    }

    private void ValidateName(string name)
    {
        if (name == null || name.Equals(string.Empty))
        {
            throw new Exception("Project name cannot be empty or null.");
        }
    }
}




public interface IProjectRepository
{
    void InsertProject(Project project);
    IList<Project> GetProjectsByName(string projectName);
}

On my Infrastructure layer, i have the implementation of IProjectRepository which does the actual querying (the code is irrelevant).


I don’t like two things about this design:

1) I’ve read that the repository interfaces should be a part of the domain but the implementations should not. That makes no sense to me since i think the domain shouldn’t call the repository methods (persistence ignorance), that should be a responsability of the services in the application layer. (Something tells me i’m terribly wrong.)

2) The process of creating a new project involves two validations (not null and not duplicate). In my design above, those two validations are scattered in two different places making it harder (imho) to see whats going on.

So, my question is, from a DDD perspective, is this modelled correctly or would you do it in a different way?

  • 1 1 Answer
  • 1 View
  • 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-14T03:03:10+00:00Added an answer on May 14, 2026 at 3:03 am

    I think part of the confusion with (1) is that you’re missing a layer — insert a service layer in your architecture and your problem goes away like magic. You can put the service and the repository implementation in the service layer — i.e., you have a service that uses a concrete implementation of the repository. Other services are free to choose an alternative implementation of the repository if they want. Your application is free to choose whatever service interface that it likes. Having said that, I’m not sure that it really matters in most cases. In nearly all of my applications I’ve got one “domain/datalayer” that basically fixed. I might layer a repository on it or not depending on how complicated the business logic is. Same with the service — it may simply not be necessary if the project isn’t very complicated. If it becomes so later, I can always refactor. Typically I’d put my repository in the same project as my data context (using LINQ) and, if there were a service, it would be in a separate project (because typically it would be exposed as a web service as well).

    With regard to (2) you need to think about the problem from a concurrency perspective. Your check for a duplicate name is best handled by a database constraint if possible. I think this is the easiest way to enforce this logic. You can certainly check if there is a duplicate before attempting an insert, but unless you do it inside a transaction you can’t guarantee that another process won’t come along and insert one between your check and your insert. The database constraint solves this problem. Moving the check into the insert logic (same transaction) also solves the problem, but regardless I think you need to be prepared to handle it as an insert failure as well as (or instead of) a validation error.

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

Sidebar

Related Questions

I just created a new web application that accepts some form based input from
I just created a Database Application for Requesting Parts. It has several forms, one
Just created a .dll file that has some methods. And I have a another
Just created a WSS site with a some custom web-parts. But I get an
I just created a user control. This control also makes use of my static
I just created a web app available offline with the cache manifest etc. It
I just created a WCF service with this MSDN tutorial . from within Visual
I just created this dojo widget and now I want to attached an event
I just created a sample application with devise plugin installed. I have :token_authenticatable in
I just created a web service client using axis and eclipse that does not

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.