views:

54

answers:

1

Caveat emptor, I'm new to Linq To SQL.

I am knocking up a prototype to convert an existing application to use Linq To SQL for its model (it's an MVVM app). Since the app exists, I can not change its data model.

The database includes information on events; these are either advertising events or prize events. As such, the data model includes a table (Event) with two associated tables (AdvertisingEvent and PrizeEvent). In my old C# code, I had a base class (Event) with two subclasses (AdvertisingEvent and PrizeEvent) and used a factory method to create the appropriate flavour.

This can not be done under Linq to SQL, it does not support this inheritance strategy.

What I was thinking of doing is creating an interface (IEvent) to includes the base, shared functionality (for example, a property "Description' which is implemented in each subclass). I thought I'd then add a propery to the superclass, for example SharedStuff, that would either return an AdvertisingEvent or PrizeEvent as a IEvent. From WPF I could then bind to MyEvent.SharedStuff.Description.

Does this make sense? Is there a better way to do this?

BTW: I'd rather not have to move to Linq to Entities.

A: 

You could always use interface inheritance to accomplish this. Instead of working with subclasses, have your IEvent interface, with the IPrizeEvent and IAdvertisingEvent interfaces deriving from that.

Then, work in terms of the interfaces.

You could then have separate implementations that don't derive from each other, but implement the appropriate interfaces.

Also, the nice side effect of working with interface inheritance in LINQ-to-SQL is if you have methods that operate on IQueryable<T> where the constraint on T is IEvent, you can do something like this:

// Get an IQueryable<AdvertisingEvent>
IQueryable<AdvertisingEvent> events = ...;

// A function to work on anything of type IEvent.
static IQueryable<T> FilteredEvents<T>(this IQueryable<T> query, 
    string description)
    where T : class, IEvent

{
    // Return the filtered event.
    return query.Where(e => e.Description == description);
}

And then make the call like this:

events = events.FilteredEvents("my description");
casperOne
Thanks for the idea, I'll give it a go and see how it feels to use.
dave