P. Orlando
P. Orlando

Reputation: 93

Method belongs to the repository pattern or not?

Inside a concrete repository class, I have three methods:

  1. Two methods that grab some data from a database. We can call them A and B.
  2. The last method (named C) executes A and B, operates just few conditions on them and returns data.

Here is an example:

public Object A() { return data; }

public Object B() { return data; }

public Object C() {
    Object dataFromA = A();
    
    if (dataFromA == null && /* some conditions */)
        return B();

    return dataFromA;
}

When I look to the C method, it feels that it does not have to belong to the repository class. The only reason behind that is because the method C contains zero query and executes some internal methods.

What do you think about this "feeling" ? What could be a solution ?

Many thanks for trying to help me.

Upvotes: 0

Views: 425

Answers (1)

doscio
doscio

Reputation: 89

I prefer to have only method that store and retrieve data from the database on repository class.

If I need to do some other operation i define a "Service" class that uses repository.

Whit this approch, if I need to write a new repository to fetch the data from a different database, i should only define the method that fetch the data (methodA and methodB), the logic of methodC remain the same.

class Repository {

    Object methodA() {
        return data;
    }

    Object methodB() {
        return data;
    }
}

class Service {

    private Repository repo;

    Object getA() {
        return repo.methodA();
    }


    Object getB() {
        return repo.methodB();
    }

    Object methodC(){
        Object dataFromA = repo.methodA();
        
        if (dataFromA == null && /* some conditions */)
            return repo.methodB();

        return dataFromA;
    }

}

Upvotes: 1

Related Questions