嗨,我是新來的存儲庫模式。我想對我所遵循的方法有所反饋。常見項目的存儲庫模式
要求:建立爲當前登錄的用戶菜單
我的解決方案:
我創建了一個服務,將由控制器叫了菜單項。
public interface IApplicationHelperService { List<Menu> GetMenuForRoles(); }
的服務
public class ApplicationHelperService : IApplicationHelperService { private readonly IMenuRepository _menuRepository; //this fecthes the entire menu from the datastore private readonly ICommonService _commonService; //this is a Service that contained common items eg. UserDetails, ApplicationName etc. public ApplicationHelperService(IMenuRepository menuRepository,ICommonService commonService) { this._menuRepository = menuRepository; this._commonService = commonService; } public List<Menu> ApplicationMenu { get { return _menuRepository.GetMenu(_commonService.ApplicationName); } } List<Menu> IApplicationHelperService.GetMenuForRoles() { return ApplicationMenu.Where(p => p.ParentID == null && p.IsInRole(_commonService.CurrentUser.Roles)).OrderBy(p => p.MenuOrder).ToList(); } }
然後CommonService實施(用於服務所需要的公用物品如。CurrentUser
public interface ICommonService { IUser CurrentUser { get; } string ApplicationName { get; } }
類上的實現ICommonService,在其他工作中,我使用上下文獲取當前用戶ds我的服務層不知道HttpContext,因爲將來可能會將其用於其他類型的應用程序。因此,我可以通過當前用戶以不同方式處理所有應用程序,但我的服務層不會介意。
因此,你應該給予反饋的是,這種方法將這種通用服務注入到所有服務中是一種好方法,還是有另一種方式來做到這一點,我問的原因是在稍後階段,我會需要當前用戶的詳細信息用於審計目的或出於任何原因。
希望這對某人有意義。 :-)
我懷疑我們將使用WCF服務。關於參數的傳遞,我懷疑這應該是需要的。這更像是一種「靜態類」方法。我會看看基類,雖然... – Captain0 2012-08-07 16:14:57
我忘了在答案中提到,但如果您需要解決服務類中的多個存儲庫;您也可能需要使用UnitOfWork模式,以便保存事務中所有存儲庫的更改。 – daryal 2012-08-08 06:16:47