我有一個基本的服務類,並且我有一堆服務,在服務類中存儲數據是好的還是不好的做法?在服務層中存儲狀態
例如:
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