views:

57

answers:

1

Disclaimer: Bit of a C# newbie - first Software Dev gig in awhile after being in QA for a couple years.

I realize flavors of this question have been asked before (inheritance in LINQtoSQL and the like), but I'm hoping I ask the question differently.

In my database, I will have a super-type of "Event" and multiple sub-types: Conference, Meeting and Appointment, for example.

Event
  Id (PK)
  TypeId (FK EventTypes.Id)
  Title

Conference
  Id (PK, FK Event.Id)
  Cost
  Topic

Meeting
  Id (PK, FK Event.Id)
  Location
  Organizer

Appointment
  Id (PK, FK Event.Id)
  Time
  Address

I am using Rob Conery's MVC StoreFront application as a reference. He essentially gets data from the database and creates class objects, manually mapping Event.Id to db.Event.Id, etc.

I'd like to do this with my Events data model - I'd like to retrieve all Events, and have a LINQ expression dynamic enough to create various event types based on some criteria (TypeId, for example).

var result = from e in db.Events
             select new IEvent 
             {
                // Little help? ;)
              };

It would be great to find a way to make it so each Event Type knows how to save itself and retrieve itself - I fear having to write the same code for each type, only varying the fields. Make sense?

I did see a question posed and someone answered with something like:

public bool Save<T>() {}

The problem is, I'm not sure where to put this code. I'm also not sure if I should use an IEvent interface or an Event partial class.

I will now end this monster question with an advanced Thank You to those that can offer help/suggestions.

-- EDIT: Good progress - going from DB to Views all with IEvent :) (This Question Helped A Lot)

public class SqlEventRepository : IEventRepository

  public IQueryable<IEvent> getEvents() {
     // Get Events without doing a query for each type..possible?
     var eventType1 = {query};
     var eventType2 = {query};

     return {concat of all queries};
  }

  public bool SaveEvent(IEvent) {
    // Can I avoid calling a save function for each type of event?

}

+1  A: 

You could have a helper class to put your Save<T>() method in. Something like SaveEvents class.

When you want to save using LINQ I'm not so sure that you can use generics as you don't know what T is and therefore cannot update properties in your queries.

I'd use inheritance and then where you'd pass a sub-class, use the parent class (Event) as your argument. Then you can quite easily cast to your subclasses to access those properties in your LINQ Queries.

EDIT:

Something like this:

public class Event : IEvent (Interface implement common properties to all Event type classes)
{
    // your code
}

public class MeetingEvent : IEvent
{

   public string MeetingEvent
   {
      get;
      set;
   }

    // add IEvent implementation...

}

public class EventManager
{

   public void MyMethod (IEvent event)
   {

       MeetingEvent Mevent = (MeetingEvent)event;

       Mevent.DoSomework();

       // now you can cast all eventclasses to and from IEvent passed as a parameter.
   }

}
Tony
Tony, great info. If you can by any chance provide links or sample code, that would be quite awesome :)
Dan
See my edit. Hope it helps!
Tony
It does - I think part of me wants to find "THE" solution from start to finish, but what fun is programming if you don't bang your head against the wall a few times? ;).Here's a question though - IEvent has it's own members: Title {get;set} - why do I need to implement Title MeetingEvent : IEvent if I'd only be making it a get/set like in IEvent - seems redundant.Thanks Tony!
Dan
If you're working with interfaces, you have to implement it in your class, because an interface is not an implementation, just a signature. But if you have automatic properties, then it does seem redundant yes. However, if you don't want to have to implement things, you could use an abstract class instead of an interface.
Tony
Bit more progress - check my first Edit for more questions :). Thanks again for the input. It has gotten me going exactly in the direction I want to go!
Dan