views:

128

answers:

1

According to this article: http://subsonicproject.com/docs/3.0%5FMigrations

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?

+1  A: 

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 :)

Rob Conery
As you can see in the code I provided in my original post, I did add an IList (which I've now converted to an IQueryable because I want lazy loading) to the Disaster class. However, I'm not sure what you mean by 'just be sure to retrieve your Addresses'. How would I know which addresses to retrieve if this association is not stored in the database?
Daniel T.
If it's not related in the database... well you kind of lost me there. If it's in another system then call it when you need it I spose.
Rob Conery