是否可以将非持久逻辑存储在存储库中
我有一个这样的类
public class Obj : IEntity
{
public virtual int ID { get; set; }
public virtual string Value { get; set; }
}
,我需要实现一个方法
1. 如果数据库中不存在给定值的 Obj,则创建这样一个 Obj
2. 如果DB中有多个Obj具有相同的Value,则抛出异常
3.返回具有给定值的Obj(现在我确定只有一个这样的对象)。
问题是 - 我应该把这个方法放在哪里。 ObjRepository 适合放置它吗?
比你提前!
I have a class like this
public class Obj : IEntity
{
public virtual int ID { get; set; }
public virtual string Value { get; set; }
}
and I need to implement a method which
1. If Obj with given value doesn't exist in database creates such an Obj
2. If there are several Obj with same Value in DB, throw an exception
3. Returns Obj with given value (Now I'm sure there's only one such an object).
The question is - where should I put this method. Is ObjRepository appropriate place for it?
Than you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说,这更多的是业务规则,而不是应该放入存储库的内容。
理想情况下,对象应该做一件事(或几件事,但处于相同的抽象级别)。处理数据库更新/读取/删除已经是一件事(而且是一件大事:))。
因此,我建议添加第三个对象(例如
ObjService
)来处理此业务场景。请注意,上下文(一如既往)才是王道。如果这是一个原型或您想要快速发布的东西,那么将逻辑放在存储库上,然后在时机成熟时将其重构为服务是没有问题的。
I would say that's more a business rule than something that should go in the repository.
Objects should do ideally one thing (or a few but at the same level of abstraction). Dealing with database updates/reads/deletes is already one thing (and a big one :) ).
So I would recommend adding a 3rd object (
ObjService
for example ) that takes care of this business scenarios.Note that context (as always) is king. If this is a prototype or something you want to get out there quick, there is no problem putting the logic on the repository and then refactor it to a service when the time comes.