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

  • Home
  • SEARCH
  • 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 8812505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:37:16+00:00 2026-06-14T03:37:16+00:00

I am trying to learn DDD, and i am making a simple demo project.

  • 0

I am trying to learn DDD, and i am making a simple demo project.

Right now, i have a generic repository

public class Repository<T> where T : class, IAggregateRoot
{
    public void Add(T entity)
    {
        ObjectSet.AddObject(entity);
    }
}

and a Product class

public class Product : IAggregateRoot
{
    private Guid _id = Guid.Empty;
    public Guid Id
    {
        get { return _id; }
        private set { _id = value; }
    }
    public string Name { get; private set; }

    protected Product() { }
    public Product(string name)
    {
        Name = name;
    }
}

The idea is that i want that a Product which has been created to have an empty Guid. It should get a new Guid only when is inserted into databse.

Product product = new Product("Hello World");
product.Id == Guid.Empty; // True

How is right now, when i call the repository to insert a product, it inserts it in database with an empty Guid.

var repository = new Repository<Product>();
repository.Add(product);

Where should i put the Guid generation of the Product? In the repository? If yes, how should i do it, because i have a generic repository.

Thank you

  • 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-14T03:37:18+00:00Added an answer on June 14, 2026 at 3:37 am

    you can change the IAggregateRoot definition to add an ID property, like this

    interface IAggregateRoot
    {
        Guid Id { get; set;}
    }
    

    as every class of your project will have an id and will implement the IAggregateRoot interface, you can initialize the id inside the class constructor, like this:

    public class Product : IAggregateRoot
    {
        public Guid Id { get; set; }
        public string Name { get; private set; }
    
    
        protected Product() { }
        public Product(string name)
        {
            Id = Guid.Empty;
            Name = name;
        }
    }
    

    and inside the repository Add method, you can put the Guid.NewGuid(), and then pass it forward

    public class Repository<T> where T : class, IAggregateRoot
    {
        public void Add(T entity)
        {
            entity.Id = Guid.NewGuid();
            ObjectSet.AddObject(entity);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple script trying to learn about hashes in Perl. #!/usr/bin/perl my
I am trying learn how to work with CEDET. I don't have any project
Trying to learn about php's arrays today. I have a set of arrays like
Im trying to learn a bit about c++ and have run in to some
I'm trying to learn effective DDD practices as I go, but had a fundamental
Trying to learn HTML5 and downloaded a test project https://github.com/amiklosi/Comicr . There is a
Trying to learn Regex in Python to find words that have consecutive vowel-consonant or
Trying to learn C++ and working through a simple exercise on arrays. Basically, I've
Trying to learn Django, I closed the shell and am getting this problem now
trying to learn and practice arrays but I have a problem with this small

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.