I have two tables in my database. Companies and Sites. Each Company has many Sites. The Sites table has has a foreign key of CompanyID.
I have a data layer class for each entity, one for the company, and one for the site.
I need to get the CompanyID by the SiteID and so I can write a method like this:
public Guid GetCompanyGuidBySiteGuid(Guid siteId) =>
_util.SelectGuid("SELECT s.CompanyID FROM pm.Site s WHERE s.SiteID = @SiteId", ("@SiteId", siteId));
Which data layer should I put this method in? The CompanyDataLayer, or the SiteDataLayer?
This is a specific question but also a general one. How does one make these kinds decisions in general when there are overlap between modules?
>Solution :
In this case, since you are trying to retrieve the CompanyID by the SiteID, it would be more appropriate to put the method in the SiteDataLayer class. This is because the method is related to retrieving information from the Site table based on the SiteID.
In general, when making decisions about where to put methods that involve multiple entities or tables, it is important to consider which entity or table the method is primarily related to. If the method is mainly focused on retrieving or manipulating data from one particular entity or table, then it should be placed in the corresponding data layer class.
However, if the method involves multiple entities or tables, it may be more appropriate to create a separate data layer class that is specifically designed to handle the interactions between those entities or tables. This class can then be used to encapsulate the functionality and make it easier to maintain and modify in the future.