こんにちは、リポジトリ パターンは初めてです。私が従っているアプローチについてフィードバックが欲しいです。
要件 : 現在ログインしているユーザーのメニューを構築します。
私の解決策:
メニュー項目を取得するためにコントローラーによって呼び出されるサービスを作成しました。
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 を実装するクラスでは、コンテキストを使用して現在のユーザーを取得します。つまり、サービス レイヤーは HttpContext について知りません。これは、将来別の種類のアプリケーションに使用される可能性があるためです。したがって、この方法では、現在のユーザーがすべてのアプリケーションに対して異なる方法で処理できますが、サービス レイヤーは気にしません。
したがって、フィードバックを提供する必要があるのは、この種の共通サービスをすべてのサービスに注入するこのアプローチは良いアプローチですか、それともこれを行う別の方法がありますか、私が尋ねる理由は、後の段階で現在のユーザーの監査目的または何らかの理由での詳細。
これが誰かにとって意味があることを願っています。:-)