Search code examples
design-patternsrepository-patternfacade

Facade with repository pattern. Handle simple requests


I'm restructuring a very old project. I'm separating the logic from front-ends using a Facade static class. I also implemented the repository pattern with a unit of work class. The idea was that the front-end would only do simple requests on the facade. The facade then handles everything (through sub systems) and those subclasses will be using the unitofwork together. The front-end would never touch the Persistence project in the solution.

I'm wondering how I should tackle simple requests. Like getting a specific row from a table. Right now I have to make a function for this for every single table (250+). Next to that, if I want to get ALL the rows from the table I'd have to do the same thing again for all tables. This isn't feasible and I can't get my head around it. Adding the persistence project as dependency to the front-end projects breaks the whole layering idea.

How do I tackle this?


Solution

  • So my knowledge was lacking. I didn't know you could create generic methods (opposed to generic classes). Therefore, I added some default generic methods to the facade. Like so:

    public static List<T> GetAll<T>() where T : class
    {
        using (var unitOfWork = new UnitOfWork())
        {
            return unitOfWork.context.Set<T>().ToList();
        }
    }
    
    public static T GetSingleOrDefault<T>(Expression<Func<T, bool>> predicate) where T : class
    {
        using (var unitOfWork = new UnitOfWork())
        {
            return unitOfWork.context.Set<T>().SingleOrDefault(predicate);
        }
    }
    

    Note that it skips the repositories. For me this is okay, since methods defined in repositories are more complex and won't ever be needed directly from the front-end.

    I hope this helps anyone in the future.