Unityを使用した実装例を次に示します。これがお役に立てば幸いです。
コントローラから逆方向に作業しています...
MVCプロジェクト:DashboardController.cs
public class DashboardController : Controller
{
private readonly IDashboardService dashboardService;
public DashboardController(IDashboardService dashboardService)
{
this.dashboardService = dashboardService;
}
[HttpGet]
public ActionResult Index()
{
var model = this.dashboardService.BuildIndexViewModel();
return this.View(model);
}
}
MVCプロジェクト:Global.asax
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
// Standard MVC setup
// ...
// Application configuration
var container = new UnityContainer();
new AppName.Services.UnityBootstrap().Configure(container);
}
}
サービスプロジェクト:DashboardService.cs
public class DashboardService : IDashboardService
{
// ctor
// ...
public IndexViewModel BuildIndexViewModel()
{
var currentPerformanceYear = this.repository.GetMaxPerformanceYear().PerformanceYearID;
var staff = this.repository.GetStaffBySamAccountName(this.currentUser.Identity.Name);
var model = new IndexViewModel
{
StaffName = staff.FullName,
StaffImageUrl = staff.StaffImageUrl,
// ...
};
return model;
}
}
サービスプロジェクト:IDashboardService.cs
public interface IDashboardService
{
IndexViewModel BuildIndexViewModel();
}
サービスプロジェクト:UnityBootstrap.cs
public class UnityBootstrap : IUnityBootstrap
{
public IUnityContainer Configure(IUnityContainer container)
{
return container.RegisterType<IDashboardService, DashboardService>()
.RegisterType<ISharePointService, SharePointService>()
.RegisterType<IStaffService, StaffService>();
}
}
会社のエンタープライズライブラリユーティリティプロジェクト:IUnityBootstrap.cs
public interface IUnityBootstrap
{
IUnityContainer Configure(IUnityContainer container);
}