2

I need to make a comparison between two techniques : Use of generic type and extend type. I don't mean a general comparison, I mean in this specific case when I need to add some features to a class named ClassA

  1. Using a generic type

    use a generic type ( Where T: ClassA ) and implement generic methods

  2. Using Extension Methods

    Use the ClassA by adding its extension methods

     public static class Helper
    {
    
     public static void MethodOne(this ClassA obj, )
    
     {
     //
      }
    
     }
    

I need to know :

  • What are the advantages of each technique in comparison with the other?
  • Why the first technique is always used in Repository Pattern? For example in this implementation why we don't add extension methods to a global class Entity ?
Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191
  • 2
    The example isn't a very good one because extension methods can also be generic. What exactly is the question here, why use Generics over extension methods? You are comparing apples and oranges... – James May 08 '15 at 09:35
  • 1
    Extesion methods are syntactic sugar for static method calls that take an instance of the extended class as their first argument. Hence, they cannot do anything that static methods cannot do (e.g. access private state). Your comparison is very much apples to oranges -- and in fact, if the problem statement was "add a feature to Class A", neither an unrelated type with the kind of generic type constraints you specified nor an extension method would be my weapon of choice. If you could specify what you're actually trying to do, it would be far easier to evaluate the options. – Rytmis May 08 '15 at 09:38
  • @James I mean to extend a class ( and derived class) method AND make it general in the same time ,which technique I have to choose and in which cases? – Lamloumi Afif May 08 '15 at 09:40
  • @LamloumiAfif it ultimately comes down to what the extended method is going to do, if it needs access to private members then an extension method definitely won't work. The way to think about an extension method is really just a static helper for an instance, they are primarily used as a way of extending types you don't have control over (i.e. core framework/3rd party classes). If the method is core functionality, part of the model and/or needs access to private members then it almost certainly wants to be part of the class. – James May 08 '15 at 09:44

1 Answers1

5

Those are two entirely different things.

You use generics to provide generic functionality. For repositories, this is often used with a "base entity" class or interface containing properties that all entities implement, like ID:

public interface IEntity
{
    int ID { get; set; }
}

public class Client : IEntity
{
    public int ID { get; set; }
    public string Name { get; set; }        
}

public class Repository<T> 
    where T : IEntity
{
    private readonly IQueryable<T> _collection;
    public Repository(IQueryable<T> collection)
    {
        _collection = collection;
    }

    public T FindByID(int id)
    {
        return _collection.First(e => e.ID == id);
    }
}

You could do that as well with an extension method:

public static T FindByID(this IQueryable<T> collection, int id)
    where T : IEntity
{
    return collection.First(e => e.ID == id);
}

Without generics, you'd have to implement the repository or the extension method for every type.

Why not use an extension method in this case: you generally only use those when you can't extend the base type. With the repository class you can group operations in one logical class.

See also When do you use extension methods, ext. methods vs. inheritance?, What is cool about generics, why use them?.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272