Custom Entity – Many 2 Many Table Issue
When we define a custom business entity as an architecture for a given project, one common issue arises that, how we will consider “Many to Many” relationships? As there is only one entity for each database table, “Many to Many” tables might not be a good choice to make a separate table for that, since this is basically a connector entity.
Case:
Database Simplest Samples – Northwind Employees
Entities: Employees, Territories, EmployeeTerritories, Region
One-to-One: Employees (EmployeeId, ReportsTo)
Description: one employee reports to another employee.
One-to-Many: Region, Territories (TerritoryId, RegionId)
Description: One region has many territories.
Many-to-Many: Employee, Territories, EmployeeTerritories (EmployeeId, TerritoryId)
Description: One employee belongs to many territories and one territory has many employees.
Solutions:
1. Create a single table for the m2m table.
2. Attach the CRUD methods of the m2m table, as static methods, to one of the o2m entity. A common convention can be chosen to attachment, to consider that entity, which is created first. In our case this is author.
3. Attach the m2m primary key, in the “save” instance method (for instance or update) parameter.
4. We can use a collection of objects, in the another tables entity. For example, the employee class may contain a collection of territories or the territory entity may contain a collection of employee object. The save method will be responsible to update the m2m list.
Anonymous comments are disabled
About joycsc
Mohammad Ashraful Alam is a Software Engineer, who is dedicated to Microsoft .NET based development. This Bangladeshi national is involved with project management and development of several US based software projects from his country. Already he has managed and developed 15 software projects, which are being used by several users of different counties, such as USA, Canada, Australia, and Bangladesh. While developing and managing a team, he contains and maintains a set of well defined engineering practices developed by him and other online developer community. Beside software development, he has also written several technical articles and research papers published by IEEE Computer Society and many other worlds recognized publishers. When not engaged with technical stuffs, he likes to pass time with his friends, and family members, listens music or watches TV.