English 中文(简体)
使用LINQtoSQL保存和检索不同类型的实体
原标题:Saving and Retrieving Entities of different types using LINQtoSQL

免责声明:C#初学者—在QA工作了几年后的第一个软件开发职位。

我意识到此问题的许多变体已经被问过了(如LINQtoSQL中的继承等),但我希望我能以不同的方式提出问题。

在我的数据库中,我将拥有一个"事件"超类型和多个子类型:例如会议、会面和约会。

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

我正在使用Rob Conery的MVC StoreFront应用作为参考。他基本上从数据库中获取数据并创建类对象,手动映射Event.Id到db.Event.Id等。

我想用我的事件数据模型来实现这个 - 我想检索所有事件,并拥有一个动态的LINQ表达式,基于某些条件(例如TypeId)创建各种事件类型。

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

找到一种方法让每个事件类型都知道如何保存和检索自己会很棒-我担心需要为每种类型编写相同的代码,只是字段不同。 (Zhǎodào yī zhǒng fāngfǎ ràng měi gè shìjiàn lèixíng dōu zhīdào rúhé bǎocún hé jiǎnsuǒ zìjǐ huì hěn hǎo - wǒ dānxīn xūyào wèi měi zhǒng lèixíng biānxiě xiāngtóng de dàimǎ, zhǐshì zìdòng bùtóng de qiánshù.)

我确实看到一个问题被提出,有人回答了类似于:

public bool Save<T>() {}

问题是,我不确定把这个代码放在哪里。我也不确定是否应该使用IEvent接口还是Event部分类。

Edit

我已经取得了很好的进展-从DB到Views,全部都使用IEvent :) (这个问题帮了很大的忙)

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?
  }
  
最佳回答

你可以通过一个帮助类将你的Save<T>()方法放在里面,例如一个SaveEvents类。

当你想要使用LINQ进行保存时,我不太确定你是否可以使用泛型,因为你不知道T是什么,因此无法在查询中更新属性。

我使用继承,然后用母子(Event)作为你的论据。 然后,你可以非常容易地向您的子流层投放这些财产,以进入贵国的LINQQuries。

编辑:

类似这样的东西:

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

}
问题回答

暂无回答




相关问题
Subclass check, is operator or enum check

A couple of friends was discussing the use of inheritance and how to check if a subclass is of a specific type and we decided to post it here on Stack. The debate was about if you should implement a ...

C++ Class Inheritance problem

Hi I have two classes, one called Instruction, one called LDI which inherits from instruction class. class Instruction{ protected: string name; int value; public: Instruction(string ...

Overloading a method in a subclass in C++

Suppose I have some code like this: class Base { public: virtual int Foo(int) = 0; }; class Derived : public Base { public: int Foo(int); virtual double Foo(double) = 0; }; ...

Embedding instead of inheritance in Go

What is your opinion of this design decision? What advantages does it have and what disadvantages? Links: Embedding description

Extending Flex FileReference class to contain another property

I want to extend the FileReference class of Flex to contain a custom property. I want to do this because AS3 doesn t let me pass arguments to functions through event listeners, which makes me feel sad,...

Interface Inheritance in C++

I have the following class structure: class InterfaceA { virtual void methodA =0; } class ClassA : public InterfaceA { void methodA(); } class InterfaceB : public InterfaceA { virtual ...

热门标签