Search code examples
c#architecturemodularitydecoupling

Decoupled architecture


I'm working on a system where I'd like to have my layers decoupled as much as possible, you know, some kind of modular application to be able to switch databases and stuff without a serious modification of the rest of the system.

So, I've been watching for x-the time one of the talks of Robert C. Martin about good practices, clean code, decoupling architecture etc, to get some inspiration. What I find kinda weird is his description of the system Fitnesse and the way they've implemented store/load methods for WikiPages. I'm linking the video as well: Robert C. Martin - Clean Architecture and Design

What's he describing (at least from my understanding) is that the entity is aware of the mechanism how to store and load itself from some persistent layer. When he wanted to store WikiPages in-memory, he simply overrode the WikiPage and created a new InMemoryWikiPage. When he wanted to store them in a database, he did the same thing...

So, one of my questions is - what is this approach called? I've been learning the whole time about Repository patterns and stuff, and why should be classes like this persistence-ignorant, but I can't seem to find any materials on this thing he did. Because my application will consist of modules, I think this may help to solve my problems without a need for creating some centralized store for my entities... Every module would simply take care of itself including persistence of its entities.

I think the code would look like is something like this:

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

   public void Save()
   {
       ..
   }

   public void Update()
   {
   }

   public void Delete()
   {
   }

   ...
}

Seems a bit weird, but... Or maybe I misunderstood what he said in the video?

My second question would be, if you don't agree with this approach, what would be the path you'd take in such modular application?

Please provide an example if possible with some explanation.


Solution

  • The pattern you posted is an Active Record.

    The difference between Repository and Active Record Pattern is that in Active Record pattern, data query and persistence, and the domain object are in one class, where as in Repository, the data persistence and query are decoupled from the domain object itself.

    Another pattern that you may want to look into is the Query Object which, unlike respository pattern where its number of methods will increase in every possible query (filter, sorting, grouping, etc) the query object can use fluent interface to be expressive [1] or dedicated in which one you may pass parameter [2]

    Lastly, you may look at Command Query Responsibility Segregation architecture for ideas. I personally loosely followed it, just picked up ideas that can help me.

    Hope this helps.

    Update base on comment

    One variation of Repository pattern is this

    UserRepository
    {
        IEnumerable<User> GetAllUsers()
        IEnumerable<User> GetAllByStatus(Status status)
        User GetUserById(int id)
        ...
    }
    

    This one does not scale since the repository get's updated for additional query that way be requested

    Another variation is to pass query object as parameter to the data query

    UserRepository
    {
        IEnumerable<User> GetAll(QueryObject)
        User GetUserById(int id)
        ...
    }
    
    
    var query = new UserQueryObject(status: Status.Single)
    var singleUsers = userRepo.GetAll(query)
    

    Some in .Net world, Linq expression is passed instead of QueryObject

    var singleUsers = userRepo.GetAll(user => user.Status == Status.Single)
    

    Another variation is to do dedicate Repository for retrieval on one entity by its unique identifier and save it, while query object is used to submit data retrieval, just like in CQRS.

    Update 2

    I suggest you get familiar with the SOLID principles. These principles are very helpful in guiding you creating a loosely coupled, high cohesive architecture.

    Los Techies compilation on SOLID pricples contains good introductory articles regarding SOLID priciples.