title: Using Generic Classes with Dependency Injection with C#
body: <p>I need to make a commons methods to my services, and use a Repository or other depending of the object type I am passing as argument.</p>
  
  <p>For example, I have these interfaces:</p>
  
  <pre><code>public interface IServiceText
  {
     Task&lt;string&gt; UpdateTextPosition(TextDto object);
     Task&lt;string&gt; UpdateTextSize(TextDto object);
     Task&lt;string&gt; UpdateOtherThingOnText(TextDto object);
  
  }
  
  public interface IServiceImage
  {
     Task&lt;string&gt; UpdateImagePosition(ImageDto object);
     Task&lt;string&gt; UpdateImageSize(ImageDto object);
     Task&lt;string&gt; UpdateOtherThingOnImage(ImageDto object);
  
  }
  </code></pre>
  
  <p>In the order hand, I have the classes that implements the Interfaces (just important code added) and with the constructors:</p>
  
  <pre><code>//Class ServiceImage
  private readonly IRepository&lt;Image&gt; _repo;
  
  public ServiceImage(IRepository&lt;Image&gt; repo) //Dependecy Injection of the IRepository Generic
  {
      _repo = repo;
  }
  
  //Class ServiceText
  private readonly IRepository&lt;Text&gt; _repo;
  
  public ServiceText(IRepository&lt;Text&gt; repo) //Dependecy Injection of the IRepository Generic
  {
      _repo = repo;
  }
  </code></pre>
  
  <p>The UpdateTextPosition and UpdateImagePosition do exactly the same thing, but in differents collections (or using differents IRepository instances, I'm using MongoDB):</p>
  
  <pre><code>public async Task&lt;string&gt; UpdateTextPosition(TextDto object)
  {
       var text = await _repo.FindAsync(object.Id);
       text.PosLeft = object.PosLef;
       text.PosRight = object.PosRight;
       await _repo.Update(text); 
       return text.Id; 
  
  }
  
  public async Task&lt;string&gt; UpdateImagePosition(ImageDto object)
  {
       var image = await _repo.FindAsync(object.Id);
       image.PosLeft = object.PosLef;
       image.PosRight = object.PosRight;
       await _repo.Update(image);  
       return image.Id
  
  }
  </code></pre>
  
  <p><strong>EDIT: (added ImageDto and TextDto)</strong></p>
  
  <pre><code>public class ImageDto
  {
       public ObjectId Id {get; set;}
       public int PosLeft {get; set;}
       public int PosTop {get; set;}
       public ICollection&lt;string&gt; PropertyImage1 {get; set;}
       public string PropertyImage2 {get; set;}
  }
  
  
  public class TextDto
  {
       public ObjectId Id {get; set;}
       public int PosLeft {get; set;}
       public int PosTop {get; set;}
       public double PropertyText1 {get; set;}
       public string PropertyText2 {get; set;}
  }
  
  public class Image : Entity
      {
           public ObjectId Id {get; set;}
           public int PosLeft {get; set;}
           public int PosTop {get; set;}
           public ICollection&lt;string&gt; PropertyImage1 {get; set;}
           public string PropertyImage2 {get; set;}
           public string OtherPropertyImage {get; set;}
      }
  
  
  public class Text : Entity
  {
       public ObjectId Id {get; set;}
       public int PosLeft {get; set;}
       public int PosTop {get; set;}
       public double PropertyText1 {get; set;}
       public string PropertyText2 {get; set;}
       public string OtherPropertyText {get; set;}
  }
  </code></pre>
  
  <p><strong>And the IEntity:</strong></p>
  
  <pre><code>/// &lt;summary&gt;
  /// Generic Entity interface.
  /// &lt;/summary&gt;
  /// &lt;typeparam name="TKey"&gt;The type used for the entity's Id.&lt;/typeparam&gt;
  public interface IEntity&lt;TKey&gt;
  {
      /// &lt;summary&gt;
      /// Gets or sets the Id of the Entity.
      /// &lt;/summary&gt;
      /// &lt;value&gt;Id of the Entity.&lt;/value&gt;
      [BsonId]
      TKey Id { get; set; }
  }
  
  /// &lt;summary&gt;
  /// "Default" Entity interface.
  /// &lt;/summary&gt;
  /// &lt;remarks&gt;Entities are assumed to use strings for Id's.&lt;/remarks&gt;
  public interface IEntity : IEntity&lt;string&gt;
  {
  }
  </code></pre>
  
  <p><strong>And Entity class:</strong></p>
  
  <pre><code>/// &lt;summary&gt;
      /// Abstract Entity for all the BusinessEntities.
      /// &lt;/summary&gt;
      //[DataContract]
      [Serializable]
      [BsonIgnoreExtraElements(Inherited = true)]
      public abstract class Entity : IEntity&lt;string&gt;
      {
          /// &lt;summary&gt;
          /// Gets or sets the id for this object (the primary record for an entity).
          /// &lt;/summary&gt;
          /// &lt;value&gt;The id for this object (the primary record for an entity).&lt;/value&gt;
          [DataMember]
          [BsonRepresentation(BsonType.ObjectId)]
          public virtual string Id { get; set; }
      }
  </code></pre>
  
  <p>I need to move the "common methods" in a new <em>CommonService</em> with an Interface (<em>ICommonService</em>), and take the object type and identify what IMongoRepository use... I think that Generic is my way... but I have read about it, but I don't understand very well the examples and I would like to the best way to implement this in my real solution.</p>
  
  <p>Thank you soooo much!!</p>