I would create a new type "CompanyServiceOffering" and modify the Company
type to use this new class instead of "CompanyService" directly.

 

From: [email protected] [mailto:[email protected]] On Behalf
Of Krokonoster
Sent: Thursday, July 12, 2012 7:54 AM
To: [email protected]
Subject: [nhusers] How to implement things when you need to store data on a
many-to-many relationship

 

Hi,

I got the scenario where one can select services offered by a company (from
a fixed list of services).  Which is a normal many-to-many relationship.

However, I now realized I also need to store data specific to the
company-service relationship, so one can store a "comment" regarding that
specific company's offering of each service.

So my "many-to-many" table should end up having a "CompanyId", "ServiceId"
as well as a "Comments" column.

 

My domain model currently is "Company" (Id, CompanyName and Services [list
of services]) and my "Service" object (Id, ServiceName and Companies [list
of Companies])

 

Brain-dead, so just cannot think where I should be implementing  the
"Comments".

 

For what it might help, here is some code (I'm using mapping by code)
Please note I had to take out some members and rename things a bit
(confidentiallity), so might have made a mistake or two.  

However this works (though critic are welcome, I'm no expert).  It's just I
need to "add" something to make "Comments" work.

 

// Domain models

public class Company

{

    public Company()

    {

        CompanyServices = new List<CompanyService>();

    }

    public virtual int Id { get; set; }

    public virtual string CompanyName { get; set; }

    public virtual IList<CompanyService> CompanyServices { get; protected
set; }

}

public class CompanyService

{

    public CompanyService()

    {

        Companies = new List<Company>();

    }

            public virutal int Id { get; set; }

    public virtual string Name    { get; set; }

    public virtual IList<Company> Companies { get; protected set; }

}

 

// nHibernate Mappings

 

// Defines the CompanyName side of the many-to-many relationship with
CompanyService

mapper.Class<Company>(map =>

                        map.Bag(x => x.CompanyServices,

                                bag =>

                                    {

                                        bag.Key(key =>
key.Column("CompanyFk"));

                                        bag.Table("Companies_Services");

                                        bag.Cascade(Cascade.None);

                                    },

                                collectionRelation =>

                                collectionRelation.ManyToMany(m =>
m.Column("CompanyServiceFk"))));

// Defines the CompanyService side of the many-to-many relationship with
CompanyName

mapper.Class<CompanyService>(map =>

                            map.Bag(x => x.Companies,

                                    bag =>

                                        {

                                            bag.Key(key =>
key.Column("CompanyServiceFk"));

                                            bag.Table("Companies_Services");

                                            bag.Cascade(Cascade.None);

                                        },

                                    collectionRelation =>

                                    collectionRelation.ManyToMany(m =>
m.Column("CompanyFk"))));

-- 
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/nhusers/-/zGon5wwge-YJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to