title: Entity Framework Inheritance and Logic
body: <p>I've been creating a prototype for a modern MUD engine. A MUD is a simple form of simulation and provide a good method in which to test a concept I'm working on. This has led me to a couple of places in my code where things, are a bit unclear, and the design is coming into question (probably due to its being flawed). I'm using model first (I may need to change this) and I've designed a top down architecture of game objects. I may be doing this completely wrong. </p>
  
  <p>What I've done is create a MUDObject entity. This entity is effectively a base for all of my other logical constructs, such as characters, their items, race, etc. I've also created a set of three meta classes which are used for logical purposes as well Attributes, Events, and Flags. They are fairly straightforward, and are all inherited from MUDObject.</p>
  
  <p>The MUDObject class is designed to provide default data behavior for all of the objects, this includes deletion of dead objects. The automatically clearing of floors. etc. This is also designed to facilitate this logic virtually if needed. For example, checking a room to see if an effect has ended and deleting the the effect (remove the flag).</p>
  
  <pre><code>public partial class MUDObject
  {
      public virtual void Update()
      {
          if (this.LifeTime.Value.CompareTo(DateTime.Now) &gt; 0)
          {
              using (var context = new ReduxDataContext())
              {
                  context.MUDObjects.DeleteObject(this);
              }
          }            
      }
  
      public virtual void Pause()
      {
  
      }
  
      public virtual void Resume()
      {
  
      }
  
      public virtual void Stop()
      {
  
      }        
  }
  </code></pre>
  
  <p>I've also got a class World, it is derived from MUDObject and contains the areas and room (which in turn contain the games objects) and handles the timer for the operation to run the updates. (probably going to be moved, put here as if it works would limit it to only the objects in-world at the time.)</p>
  
  <pre><code>public partial class World
  {
      private Timer ticker;
  
  
      public void Start()
      {
          this.ticker = new Timer(3000.0);
          this.ticker.Elapsed += ticker_Elapsed;
          this.ticker.Start();
      }
  
      private void ticker_Elapsed(object sender, ElapsedEventArgs e)
      {
          this.Update();
      }
  
      public override void Update()
      {
          this.CurrentTime += 3;
          // update contents
          base.Update();
      }
  
      public override void Pause()
      {
          this.ticker.Enabled = false;
          // update contents
          base.Pause();
      }
  
      public override void Resume()
      {
          this.ticker.Enabled = true;
          // update contents
          this.Resume();
      }
  
      public override void Stop()
      {            
          this.ticker.Stop();
          // update contents
          base.Stop();
      }
  }
  </code></pre>
  
  <p>I'm curious of two things. </p>
  
  <ol>
  <li><p>Is there a way to recode the context so that it has separate
  ObjectSets for each type derived from MUDObject?</p>
  
  <ul>
  <li>i.e. context.MUDObjects.Flags or context.Flags</li>
  <li>If not how can I query a child type specifically?</li>
  </ul></li>
  <li><p>Does the Update/Pause/Resume/Stop architecture I'm using work
  properly when placed into the EF entities directly? given than it's for
  data purposes only?</p>
  
  <ul>
  <li>Will locking be an issue?</li>
  <li>Does the partial class automatically commit changes when they are made?</li>
  <li>Would I be better off using a flat repository and doing this in the game engine directly?</li>
  </ul></li>
  </ol>