R – How to handle SubSonic ‘relationships’ with migration

migrationsimplerepositorysubsonic

According to this article:
http://subsonicproject.com/docs/3.0_Migrations

Bottom line: if you're a developer that is concerned about database design,
migrations might not be for you.

Ok, that's fine, I can treat the database as simply a persistent repository of data that doesn't contain any business logic. In other words, a glorified text file.

What I don't know how to do is relate two objects together. Take for example these two classes:

public class Disaster
{
    public int DisasterId { get; set; }
    public string Name { get; set; }
    public DateTime? Date { get; set; }
    public IList<Address> Addresses { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string WholeAddressHereForSakeOfBrevity { get; set; }
}

Disaster contains an IList of multiple Addresses that were hit by the disaster. When I use SimpleRepository to add these to the database with SimpleRepositoryOptions.RunMigrations, it generates the tables with all the columns, but no foreign key columns as expected.

How would I relate these two together so that when I call Disaster.Addresses, I get a list of all the affected Addresses? Is this possible or do I have to use ActiveRecord instead and create the database tables first? Or do I have to add in a column for the disaster's ID into Address? If so, how does this method work for many-to-many relationships?

Best Answer

It's possible - you just do it by hand is all. Add a property to Disaster called "Addresses" and make it an IList<> (or you can make it IQueryable if you want it to Lazy Load). When you retrieve your Disaster, just be sure to retrieve your Addresses.

It's sort of "manual" - but that's the idea. I'm working on enhancements to this that i'm hoping to push in a later release.

And before you ask why I didn't do it in the first place :) it's because I don't know if I should use a Many to Many or 1-many based on the parent/child relationship. In your example, I'd guess that it's probably 1 to many but given what I know about Addresses and disasters (especially in Florida) it should probably be many to many.

Bottom Line - how would SubSonic know this? We could introspect both objects for "bi-directionality", which means if Address has many disasters than it's many to many (which is obvious) - but then that's not happy coding if you like DDD.

I'm leaning towards that rule with some type of override that would force the issue. Your thoughts on this are welcome :)