Are there too many entities?

I have an old puzzle, so I thought that sharing it with you might be right. The fact is that some of our entities in the database are quite large (reading has many properties), and rarely business logic uses all the properties of the entity, so every time I need to think which properties should be loaded for the business logic to work correctly. A very hypothetical pattern:

public class Product { public string Title {get;set;} public string Description {get;set;} public string RetailPrice {get;set;} public string SupplierId {get;set;} public Supplier Supplier { get;set;} // many other properties } public class ProductDiscountService { public decimal Get(Product product) { // use only RetailPrice and Supplier code return discount; } } public class ProductDescriptionService { public string GetSearchResultHtml(Product product) { // use only Title and Description return html; } } 

It looks like I can extract the IDiscountProduct and ISearchResultProduct interfaces, mark the product as implementing these interfaces, and then create smaller DTOs that implement each of these interfaces, but at the moment it looks like iterating over (at least I haven't seen anyone either grouped properties using interfaces).

Splitting an object in the database into smaller objects also does not seem reasonable, since all these properties belong to the product, and I'm afraid that I will be forced to use many connections to select something, and if I decide that some property belongs to another object , this move will be quite difficult to implement.

So that each property used in a particular business logic of the method as a parameter of the method also looks like a bad decision.

+7
source share
1 answer

If the properties are not large (read long lines and / or binaries), I will just download them all.

The following are simple properties (e.g. Title)

  • No additional code (get this product only with a title or get only the price, blah blah)
  • The product instance is always complete, so you can pass it without checking if the property is null.
  • If you have to lazily load some other properties, it will cost you more than download them eagerly. If you have 20 properties, this is not even a large object (again, if your (hypothetical) Description property does not have kilobytes).

Now, if you have related objects (ProductSupplier) - it should be lazy-load, imo, if you do not know that this property will be used.

+1
source