Ehsan Ghanbari

Experience, DotNet, Solutions

Layer Supertype pattern with example in C#

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!

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
using Log4net in asp.net MVC4
How to use PagedList In asp.net MVC
Redis as a cache server
Domain driven design VS model driven architecture
What's the DDD-lite?
Multiple submit buttons in asp.net MVC
comments powered by Disqus