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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:26:59+00:00 2026-06-09T20:26:59+00:00

I am working on rewriting my ASP.NET MVC app using the domain driven design

  • 0

I am working on rewriting my ASP.NET MVC app using the domain driven design priciples. I am trying to validate my User entity. So far I am able to validate basic rules (like the username and password being a non null/whitespace string). However one of the rules, I need to make sure that the username is unique. However I need access to the database inorder to do this, which means I would have to inject my IUserRepository into my User entity like so.

public class User
{
    private readonly IUserRepository _userRepository;
    public User(IUserRepository repo)
    {
        _userRepository = repo;
    }

    public override void Validate()
    {
        //Basic validation code
        if (string.IsNullOrEmpty(Username))
            throw new ValidationException("Username can not be a null or whitespace characters");
        if (string.IsNullOrEmpty(Password))
            throw new ValidationException("Password can not be a null or whitespace characters");

        //Complex validation code
        var user = _userRepository.GetUserByUsername(Username);
        if (user != null && user.id != id)
            throw new ValidationException("Username must be unique")
    }
}

However this seems … well wrong. Making my entity depend on my repository seems like a bad idea (correct me if I am wrong). But having the validation code in the entity makes sense. Where is the best place to put complex validation code?

  • 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-09T20:27:00+00:00Added an answer on June 9, 2026 at 8:27 pm

    However this seems … well wrong. Making my entity depend on my repository seems like a bad idea (correct me if I am wrong).

    In general, dependency on repository is not ‘wrong’, it is sometimes unavoidable. However I think it should be an exception and should be avoided as much as possible. In your scenario you may reconsider having this dependency. If you think about it, ‘uniqueness’ is not a responsibility of the entity itself because entity does not know about other entities. So why having entity enforce this rule?

    But having the validation code in the entity makes sense. Where is the best place to put complex validation code?

    I think that you may be overgeneralizing ‘validation’. I would get rid of the ‘Validate’ method and will make sure that the object does not get into ‘invalid’ state in the first place. I answered similar question few months ago.

    Now back to the uniqueness rule. I think that this is one of the examples where DDD ‘leaks’ a bit, in a sense that the enforcement of this business rule can not be expressed purely in domain code. I would approach it like this:

    // repository
    interface Users {
      // implementation executes SQL COUNT in case of relation DB
      bool IsNameUnique(String name);
    
      // implementation will call IsNameUnique and throw if it fails
      void Add(User user);
    }
    

    The client code would know that before adding a new user, it should explicitly check for uniqueness, otherwise it will crash. This combination enforces business rule in the domain code, but it is usually not enough. As an additional enforcement layer you may want to add UNIQUE constraint in the database or employ explicit locking.

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

Sidebar

Related Questions

i'm working on asp.net web application. please guide me for url rewriting in asp.net:
I am working on an ASP.NET MVC project which allows users to construct arbitrarily
We have been wrestling with the confines of ASP.NET trying to get JSONP working.
I'm using the MVCSitemapProvider for my ASP.NET MVC 3 project, I have a dynamic
i am using URL rewriting in my asp.net application using regx virtual URL is
I'm using asp.net mvc 3 with jquery unobtrusive validation. I recently changed from standard
I am working on ASP.NET website. I need to post a Question Paper based
I have a Domain that I generate using Entity Developer. This creates all of
I am creating a site in asp.net with URL rewriting. My initial url is
We are rewriting an old site for a client, from Classic ASP to ASP.NET

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.