First of all, thank you for this book! This is really first book for practical application of DDD.
I have some questions:
1) Is it possible to use public constructors for domain object (Project, Contact, Company, etc) initialization?
In the “Domain-Driven Design: Tackling Complexity in the Heart of Software” book Eric Evans wrote:
When a Constructor Is All You Need
I've seen far too much code in which all instances are created by directly calling class constructors, or whatever the primitive
level of instance creation is for the programming language. The introduction of FACTORIES has great advantages, and is generally underused. Yet there are times when the directness of a constructor makes it the best choice. FACTORIES can actually obscure simple
objects that don't use polymorphism.
May be, it would be rather to create “factory method”
public static void CreateEntity(EntityBase entity, object key)
in the base class EntityBase and
for the derived classes, for example:
public static Contact CreateContact(object key, string firstName, string lastName)
Contact contactEntity = new Contact();
contactEntity.JobTitle = string.Empty;
contactEntity.Email = string.Empty;
contactEntity.PhoneNumber = string.Empty;
contactEntity.MobilePhoneNumber = string.Empty;
contactEntity.FaxNumber = string.Empty;
contactEntity.Remarks = string.Empty;
contactEntity.CurrentCompany = null;
contactEntity.addresses = new List<Address>();
2) Is it safe to use the Object type for the Key property of EntityBase? May be it would be rather to use
generics for Key (IEntity<TKey> and EntityBase<TKey>, respectively)? And in this case the method NewKey() can be made as non static and virtual.
3) The pattern “Unit Of Work” is wonderful! But in the real applications it is often necessary to apply the changes to the several entities at the same time within the single transaction. If I get you right the UnitOfWork instance is created in Service class
and it is required to use it in several RepositoryFactory at the same time?