Should business objects contain objects or references?
-
22-08-2019 - |
Question
Should a business object contain a reference to other objects (as in the id field references another database record) or should it have an instance of the actual objects.
For example:
public class Company
{
public int Id { get; set; }
public CompanyStatus Status { get; set; }
}
or
public class Company
{
public int Id { get; set; }
public int Status { get; set; }
}
Solution
From my understanding, it should contain references to interfaces, not concrete classes.
public class Company
{
public int Id { get; set; }
public ICompanyStatus Status { get; set; }
}
Assuming that the concrete implementation of CompanyStatus in your example was a class and not an enum.
OTHER TIPS
When creating Business Layer objects in an OO fashion, you should be using objects directly.
In your example, does int Status
refers to the Id of a CompanyStatus object stored somewhere? In that case, it really feels like that's more of a data layer concern. It is usually best to avoid mixing your data layer with your business layer.
If you're talking about C#, then aggregating an object means you're storing a reference to it.
It depends on the data. Some data should be stored as a copy of the original object, some should be a reference.