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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:10:26+00:00 2026-06-16T01:10:26+00:00

I have a scenario where a domain entity property needs to be verified to

  • 0

I have a scenario where a domain entity property needs to be verified to be unique before it can be saved to the database. Here is a simple Product class. Let’s assume I want to validate that when creating a new Product that the ProductKey string property is unique:

public class Product : EntityBase
{
    int ID { get; set; }
    string ProductKey { get; set; }
    int CategoryID { get; set; }

    bool IsValid
    {
        get
        {
            if (string.IsNullOrEmpty(ProductKey))
            {
                ValidationErrors.Add("ProductKey Required.");
            }

            if (CategoryID == 0)
            {
                ValidationErrors.Add("CategoryID Required.");
            }

            /* Validation that the product key is unique could go here? i.e. requires a database read. */

            return ValidationErrors.Count() == 0;
        }
    }
}

Since I’m using Domain Driven design the Product entity has no knowledge of persistence or the service layers. I could just add a check to the Service method as follows:

public class ProductService 
{
    private IProductRepository _productRepository = new ProductRepository();

    public int CreateProduct(Product item) 
    {
        if (item.IsValid)
        {
            if (ProductKeyIsUnique(item.ProductKey))
            {
                _productRepository.Add(item);
            }
            else
            {
                throw new DuplicateProductKeyException();
            }

        }
    }

    private bool ProductKeyIsUnique(string productKey)
    {
        return _productRepository.GetByKey(productKey) == null;
    }

}

This is simple enough but ideally I would like such logic to live in the domain model. Perhaps by raising some kind of validation event that can be caught by the service layer?

Is there a best practice or known design pattern for this type of scenario?

  • 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-16T01:10:27+00:00Added an answer on June 16, 2026 at 1:10 am

    Product key uniqueness is not domain object knowledge. So you don’t need domain validation for it. Why Product should care about key uniqueness? In my opinion it is an application layer responsibility. Your solution seems valid and right for me.

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

Sidebar

Related Questions

Following is the scenario. I have something called as Domain which can have multiple
So here's the scenario: The domain I have is www.abc.com and I want to
Hi here is what I what I have: Scenario Outline: Seatching for stuff Given
I have a scenario where combobox can have same string values. for exa combo
I have the following domain class: public class Product { public virtual Guid Id
Ok, so here is the scenario: I have an activeX that uploads files using
I've a question about Entity Framework. In a code first scenario I have a
I have the following scenario, Domain class A which hasMany B's Domain class B
I have a scenario where I have a base class as one entity, then
Here is the scenario: I have a container page that swaps iFrames in and

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.