[英]Method belongs to the repository pattern or not?
Inside a concrete repository class, I have three methods:在具体的存储库 class 中,我有三种方法:
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.当我查看C方法时,感觉它不必属于存储库class。 The only reason behind that is because the method C contains zero query and executes some internal methods.
其背后的唯一原因是因为方法C包含零查询并执行一些内部方法。
What do you think about this "feeling"?您如何看待这种“感觉”? What could be a solution?
有什么解决办法?
Many thanks for trying to help me.非常感谢您尝试帮助我。
I prefer to have only method that store and retrieve data from the database on repository class.我更喜欢只有在存储库 class 上的数据库中存储和检索数据的方法。
If I need to do some other operation i define a "Service" class that uses repository.如果我需要做一些其他操作,我定义一个使用存储库的“服务”class。
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.有了这个方法,如果我需要编写一个新的存储库来从不同的数据库中获取数据,我应该只定义获取数据的方法(
methodA
和methodB
), methodC
的逻辑保持不变。
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.