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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:28:59+00:00 2026-06-06T08:28:59+00:00

I am trying to model a simple domain by using DDD. Database layer is

  • 0

I am trying to model a simple domain by using DDD. Database layer is implemented by using Entity Framework and domain objects are POCO. Domain has User entity which has FirstName, LastName and Username properties. Accordingly, domain defines IRepository that handles repository for users.

Now one requirement in the domain logic is that no two users with same username can exist. So trying to add new user when another one with the same username already exist should throw an exception.

IUnitOfWork unitOfWork = new UnitOfWork();
IRepository<User> users = unitOfWork.Users;

User user1 = new User() { Username = "jsmith", FirstName = "John", LastName = "Smith" };
users.Add(user1);   
users.Save();       // ok, new user added to the underlying database

User user2 = new User() { Username = "jsmith", FirstName = "Jim", LastName = "Smith" };
users.Add(user2); // exception here?
users.Save();     // or exception here?

This is an example of the code that should go into the WPF application that adds new user. Here, UnitOfWork encapsulates Entity Framework’s DbContext object.

My question is how and where should I enforce this domain rule? Should exception be thrown when trying to add user into repository or rather when Save() method is called? Should I maybe create Domain Service for adding new users and then handle all domain logic rules there?

Also, what exception should I throw? Should I create some custom domain exception such as DuplicateUserException or something like that?

  • 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-06-06T08:29:01+00:00Added an answer on June 6, 2026 at 8:29 am

    The db is the ultimate enforcer of that rule. Now, is this a multi user app? If so, then the most pragmatic way is to rely on the db , catch the sql exception and then throw a businessrule exception that will be redirected to the UI (i.e will trigger an error message).

    You could verify that a name already exists at the domain level (via a Service is the most efficient way) but this can fail in a concurrent environment. However, it is an elegant and clean solution for a single user app. And by single user app I mean that ALL the app will serve one user at a time. If you have a WPF client and a web service, that’s a multi user app.

    Your best bet is to let the persistence (in this case the db) handle this rule, because it’s the repo’s responsibility to ensure that there are no duplicate names (the repository is not a dumb bucket, it’s the manager of persistence) and it solves the concurrency problem as well.

    Note that I haven’t mentioned EF, because it doesn’t matter the way you communicate with the db. If tomorrow you’ll switch to Azure db, the solution is still the same, only specific parts of implementation will be different.

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

Sidebar

Related Questions

I'm trying to model a asynchronous job processing framework using MailboxProcessor. My requirements are
I am trying to map a new domain model to a fixed-schema legacy database
My application has a very simple model right now and I'm trying to find
I am using Entity Framework with Linq to Entities, trying to select some data
I'm trying to manually build a WCF Data Service using a POCO data model
I'm trying to set up a simple Roles model for my site. Users can
Am trying to construct a simple update query in my model class Model_DbTable_Account extends
I am trying to create a simple app on GAE/J. The data model consists
I am trying to model a network using C++. I have a struct called
I'm trying to make a simple email notification when a user signs up. My

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.