这是使用 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);
}