First of all look at the definition by @martinfowler "A type that acts as the supertype for all types in its layer" all you need is a Supertype Class for all the Objects. I usually use it in the Repository layer as a generic Repository. I recommend to use it if you have more than one kind of object in your layer. Let's consider an example in the real world, Create a class and name it Profile
public class Profile { public string Name { get; set; } public int Age { get; set; } }
Then create a generic interface called IRepository
public interface IRepository<T> { void Add(T entity); void Delete(T entity); }
Now create the IProfileRepository
public interface IProfileRepository : IRepository<Profile> { }
Then create the Repository Class, this is the Generic Class that we talk about
public class Repository<T> : IRepository<T> { public void Add(T entity) { throw new NotImplementedException(); } public void Delete(T entity) { throw new NotImplementedException(); } }
As you see you have to implement the Add and Delete methods, now if you want to create the ProfileRepository you don’t need to create the Delete and Add again
public class ProfileRepository : Repository<Profile>, IProfileRepository { }
That's it, Thanks for reading!
Category: Software
Tags: Design Pattern