Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well I finally had a chance to write a prototype as suggested. I think that something like this would work but I haven't tested it. Now all of your crud methods can be defined against the <code>IObjectSet&lt;&gt;</code> member.</p> <pre><code>public class Crud&lt;EntityType&gt; where EntityType : class { private readonly ObjectContext Context; private readonly IObjectSet&lt;EntityType&gt; Entities; public Crud(ObjectContext context) { Context = context; Type BaseType = GetBaseEntityType(); if (BaseType == typeof(EntityType)) { Entities = Context.CreateObjectSet&lt;EntityType&gt;(); } else { Entities = (IObjectSet&lt;EntityType&gt;)Activator.CreateInstance(typeof(ObjectSetProxy&lt;,&gt;).MakeGenericType(typeof(EntityType), BaseType), Context); } } private static Type GetBaseEntityType() { //naive implementation that assumes the first class in the hierarchy derived from object is the "base" type used by EF Type t = typeof(EntityType); while (t.BaseType != typeof(Object)) { t = t.BaseType; } return t; } } internal class ObjectSetProxy&lt;EntityType, BaseEntityType&gt; : IObjectSet&lt;EntityType&gt; where EntityType : BaseEntityType where BaseEntityType : class { private readonly IObjectSet&lt;BaseEntityType&gt; Entities; public ObjectSetProxy(ObjectContext context) { Entities = context.CreateObjectSet&lt;BaseEntityType&gt;(); } public void AddObject(EntityType entity) { Entities.AddObject(entity); } //TODO: implement remaining proxy methods public IEnumerator&lt;EntityType&gt; GetEnumerator() { return Entities.OfType&lt;EntityType&gt;().GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } public Type ElementType { get { return typeof(EntityType); } } public Expression Expression { get { return Entities.OfType&lt;EntityType&gt;().Expression; } } public IQueryProvider Provider { get { return Entities.Provider; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload