我有一个基本的服务类,并且我有一堆服务,在服务类中存储数据是好的还是不好的做法?在服务层中存储状态
例如:
public interface IFunkyService
{
void AddComment(int quoteid, string comment);
void SetProirirty(int quoteid, Importance proirity);
}
public class FunkyService : CustomServiceBase, IFunkyService
{
private readonly IRepo _repo;
private readonly IUserSession _user;
public FunkyService(IRepo repo, IUserSession user)
{
_repo = repo;
_user = user;
}
public void SetProirirty(int quoteid, Important priority)
{
//get quote object then persists
}
public void AddComment(int quoteid, string comment)
{
//get quote object then persists
}
}
我可以简单地只具有存储类上的报价对象的私有方法? 例如
private Quote _quote {get;set;} //store on the class???
private void GetQuote(int quoteid)
{
_quote = _repo.single(quoteid); //or whatever
}
对我来说,最大的好处就是让“在一个地方给我一个引用对象”,如果我调用一个方法或将对象传递给另一个方法,我不想每次重新实例化对象。在一个调用中,我想完成我的任务,如果权衡是我不能用这个对象再次调用(在7.完成之后),那很好。你怎么看? – Haroon
我不确定我是否理解你。您应该可以将'Quote'对象从method传递到method,而不必一次又一次地从数据库中获取参数。如果你将它存储在类中以保持它作为实际上是一个坏主意**(如全局变量)的方法之间的引用。你能否添加一些代码来演示一个将被调用的方法链的例子? – mellamokb
你绝对可以“在一个地方给我一个报价对象”,这没有什么错。但不是将其存储在全局类变量中,而是从方法'private Quote GetQuote(int quoteid){return _repo.single(quoteid); }'。然后将方法引用到方法。合理? – mellamokb