views:

167

answers:

2

I have three entities: Customer, Device and LocationTag.

Customers have a list of LocationTags (nothing more than an ID and a Description). They also have a list of Devices.

Devices are tagged with a subset of the Customer's LocationTags, so Devices have a List of LocationTags, too (but only of the Customer's).

If I delete a LocationTag from the Customer's list, I would like it also to cascade delete from the list of LocationTags in the Device. Currently, I have it working but with manual code in the domain object classes, but violates DRY in my opinion.

Is it possible to accomplish this via Fluent NHibernate mappings?

Simplified Fluent NHib mappings:

Customer

        public CustomerMap()
    {
        WithTable("Customers");

        Id(x => x.ID)
            .WithUnsavedValue(0)
            .GeneratedBy.Identity();

        Map(x => x.Name);

        HasMany<LocationTag>(t => t.LocationTags).IsInverse();

        HasMany<Device>(d => d.Devices).IsInverse();
    }

Device

        public DeviceMap()
    {
        WithTable("Devices");

        Id(x => x.ID)
            .WithUnsavedValue(0)
            .GeneratedBy.Identity();

        Map(x => x.Name);

        HasMany<LocationTag>(x => x.LocationTags).IsInverse();
    }

LocationTag

        public LocationTagMap()
    {
        WithTable("LocationTags");

        Id(x => x.ID)
            .WithUnsavedValue(0)
            .GeneratedBy.Identity();

        Map(x => x.Description);
    }
A: 

I'm not sure if this is even possible with standard NHibernate. You're best asking this on the NHibernate users mailing list, you're more likely to get a helpful answer there.

James Gregory
Right; my question should really be, should something like this even be attempted in NHibernate mappings as opposed to the domain object logic. Thanks for the response, James.
Todd Brooks
A: 

You could have an associative class between the Customer and the LocationTags. Then have the Device use a FK into the associative class instead of to the LocationTag directly.

Customer
- CustomerID (PK)

CustomerLocationTag
- CustomerLocationTagID (PK)
- CustomerID (FK)
- LocationTagID (FK)

LocationTag
- LocationTagID (PK)

Device
- DeviceID (PK)
- CustomerLocationTagID (FK)
Mufasa
How would this be mapped in NHibernate or Fluent NHib?
Todd Brooks