Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a couple things you can try.</p> <p>One is to define an interface that has all the relevant fields that the thirty entity classes share. Then, you would be able to have each entity class implement this interface (let's call it <code>IMyEntity</code>) by doing something like</p> <pre><code>public partial class EntityNumber1 : IMyEntity { } </code></pre> <p>for each entity (where <code>EntityNumber1</code> is the name of one of the entity classes). Granted, this is still thirty different definitions, but your CRUD operation class could then operate on <code>IMyEntity</code> instead of having to write a new class each time.</p> <p>A second way to do this is simply to genericize the CRUD operation class, as you suggest:</p> <pre><code>public class ExampleAttributes&lt;T&gt; : IAttributeList { ... </code></pre> <p>which allows you to use T as the type on which to operate. Granted, this might be easier in combination with the first method, since you would still have to check for the presence of the attributes and cast the entity to the appropriate type or interface.</p> <p><strong>Edit:</strong></p> <p>To check for the presence of the appropriate properties on the entity, you might need to use reflection methods. One way to check whether the given type T has a particular property might be to check for</p> <pre><code>typeof(T).GetProperties().OfType&lt;PropertyInfo&gt;().Count&lt;PropertyInfo&gt;(pi =&gt; pi.Name == "MyPropertyName" &amp;&amp; pi.GetGetMethod().ReturnType == typeof(TypeIWant)) &gt; 0 </code></pre> <p>Of course, replace <code>TypeIWant</code> with the type you are expecting the property to be, and replace <code>MyPropertyName</code> with the name of the property for which you are checking.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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