ObjectMapper.net

Q

qhaut

Hello,

First of all - sorry if this forum ist not the most suitable one for my
question...

I am using the current version from ObjectMapper.net and try to solve
the following:

Two Classes:
Company
Employee

an employee can work for 0 ... n Companies
a Company can have 0 .. n Employees

Not so hard - isn't it? How do I realize that m:n (both directions) in
ObjectMapper.Net?

There are two Interfaces to support...
Company.Employees -> List<Employee>
Employee.Companies -> List<Company>

Thanke you for your help!
 
D

Dave Shooter

qhaut said:
Hello,

First of all - sorry if this forum ist not the most suitable one for my
question...

I am using the current version from ObjectMapper.net and try to solve
the following:

Two Classes:
Company
Employee

an employee can work for 0 ... n Companies
a Company can have 0 .. n Employees

Not so hard - isn't it? How do I realize that m:n (both directions) in
ObjectMapper.Net?

There are two Interfaces to support...
Company.Employees -> List<Employee>
Employee.Companies -> List<Company>

Thanke you for your help!

I am not familiar with ObjectMapper.net but usually solve this scenario
with an Intersection relation, namely create a third class (and
corresponding table) which links the two other classes. Assuming both
Employee and Company have unique identifiers/primary keys:

CREATE TABLE EmployeeCompany (
EmployeeID INT FOREIGN Key REFERENCES EMPLOYEE,
CompanyID INT FOREIGN KEY REFERENCES COMPANY,
PRIMARY KEY EmployeeID, CompanyID)

The interfaces would then become:

Company.Employees -> List<EmployeeCompany>
Employee.Companies -> List<EmployeeCompany>

Instead of and m:n relationship directly between company and employees
you have two 1:n relationships, one between Employee and EmployeeCompany
and a second between Company and EmployeeCompany. Obviously, there are
no nulls and the combination of EmployeeID and CompanyID for any given
EmployeeCompany instance must be unique.

As I said, this works for me using a proprietary O/R library, and don't
quote me on the SQL!
 
Q

qhaut

Dave,

Thank you for your answer. Yes, you are right - the main thing is, that
ObjectMapper.NET translates (as I think) an M:N Relation into two 1:N
Relations.

Company
Employee
CompanyEmployee
EmployeeCustomer

That is the behavior out of the box. But I am not sure if that is the
only way...
I want a solution you have shown in your answer above.

Probabely there is another solution out there.

Thanks again, Günther
 
G

gerhard.stephan

Hi,

the ObjectMapper .NET offers the posibility to establish backlinks to
the object which is referencing the child object.
That's the way to prevent two link tables. You don't need it, only if
you want to have distinct references.

Have a look at the attribute [BackLink]. In your example you can add a
property to the employee which links back to the ID of the customer
that references the employee. So you only have 1 link table
(CustomerEmployee)

Cheers
Gerhard

/// <summary>
/// Gets or sets the name of the company.
/// </summary>
/// <value>The name of the company.</value>
[BackLink(typeof(Company_With_Employees), "LegalName",
"Employees")]
public string CompanyName
{
get { return companyName; }
set { companyName = value;}
}

/// <summary>
/// Gets or sets the id of the company.
/// </summary>
/// <value>The name of the company.</value>
[BackLink(typeof(Company_With_Employees), "Id", "Employees")]
public string CompanyId
{
get { return companyId; }
set { companyId = value;}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top