Is it OK to store non-persistent logic in repository
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开发者_StackOverflow 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!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.
精彩评论