开发者

Saving and Retrieving Entities of different types using LINQtoSQL

开发者 https://www.devze.com 2022-12-19 17:59 出处:网络
Disclaimer:Bit of a C开发者_如何学C# newbie - first Software Dev gig in awhile after being in QA for a couple years.

Disclaimer: Bit of a C开发者_如何学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?

}


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.
   }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号