Initial conditions:
There is a book with a given set of properties. The book is the essence.
public class Book { public Book(BookID id, string name, string author, double price) { ID = id; SetName(name); SetAuthor(author); SetPrice(price); } public BookID ID { get; } public string Name { get; private set; } public string Author { get; private set; } public double Price { get; private set; } public void ChangeName(string name) { SetName(name); } public void ChangeAuthor(string author) { SetAuthor(author); } public void ChangePrice(double price) { SetPrice(price); } private void SetName(string name) { if(name == null || name.Length == 0) { throw new ArgumentException("Π£ΠΊΠ°ΠΆΠΈΡΠ΅ Π½Π°Π·Π²Π°Π½ΠΈΠ΅ ΠΊΠ½ΠΈΠ³ΠΈ"); } } private void SetAuthor(string author) { if(author == null || author.Length == 0) { throw new ArgumentException("Π£ΠΊΠ°ΠΆΠΈΡΠ΅ Π°Π²ΡΠΎΡΠ° ΠΊΠ½ΠΈΠ³ΠΈ"); } } private void SetPrice(double price) { if(price < 0) { throw new ArgumentException("Π‘ΡΠΎΠΈΠΌΠΎΡΡΡ ΠΊΠ½ΠΈΠ³ΠΈ Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡΡΡ >=0"); } } } public class BookID { public int ID {get;} public BookID(int bookID) { ID = bookID; } } In the present case, the book has only 4 parameters:
- Entity ID
- Title
- Author
- Price
In this case, it is convenient to use a constructor to initialize the Book entity.
Situation
Let the book about 20 parameters. The situation is quite abstract. In the case of a book it may not be. However, this can occur when determining the model of the device and other similar cases.
In the case of a large number of parameters, I would create a separate class BookProperties for convenience, in which the properties of the book would be presented, but without a guarantee of their correctness. Then I would place an instance of the BookProperties class in the constructor of the Book entity in which validation would be performed.
Below is a variant code for a similar approach. For convenience, the same number of parameters is used as before.
public class Book { public Book(BookID id, BookProperties bookProperties) { ID = id; SetName(bookProperties.Name); SetAuthor(bookProperties.Author); SetPrice(bookProperties.Price); } public BookID ID { get; } public string Name { get; private set; } public string Author { get; private set; } public double Price { get; private set; } public void ChangeName(string name) { SetName(name); } public void ChangeAuthor(string author) { SetAuthor(author); } public void ChangePrice(double price) { SetPrice(price); } private void SetName(string name) { if(name == null || name.Length == 0) { throw new ArgumentException("Π£ΠΊΠ°ΠΆΠΈΡΠ΅ Π½Π°Π·Π²Π°Π½ΠΈΠ΅ ΠΊΠ½ΠΈΠ³ΠΈ"); } } private void SetAuthor(string author) { if(author == null || author.Length == 0) { throw new ArgumentException("Π£ΠΊΠ°ΠΆΠΈΡΠ΅ Π°Π²ΡΠΎΡΠ° ΠΊΠ½ΠΈΠ³ΠΈ"); } } private void SetPrice(double price) { if(price < 0) { throw new ArgumentException("Π‘ΡΠΎΠΈΠΌΠΎΡΡΡ ΠΊΠ½ΠΈΠ³ΠΈ Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡΡΡ >=0"); } } } public class BookID { public int ID {get;} public BookID(int bookID) { ID = bookID; } } public class BookProperties { public string Name { get; set; } public string Author { get; set; } public double Price { get; set; } } The BookProperties class is intended solely for the convenience of creating a Book entity.
Question:
How much does this approach contradict DDD and how much is this approach correct in general?