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
you can change the IAggregateRoot definition to add an ID property, like this
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:
and inside the repository Add method, you can put the Guid.NewGuid(), and then pass it forward