使用DI
Unit
參考的文章
- http://netmvc.blogspot.tw/2012/04/dependency-injection-in-aspnet-mvc-4.html
- http://stackoverflow.com/questions/17460475/dependency-injection-in-asp-net-web-api-using-unity-error
- http://benfoster.io/blog/per-request-dependencies-in-aspnet-web-api-using-structuremap
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
return container;
HierarchicalLifetimeManager
http://www.asp.net/web-api/overview/advanced/dependency-injection
public static void Register(HttpConfiguration config)
{
var container = new UnityContainer();
container.RegisterType<IProductRepository, ProductRepository>(new HierarchicalLifetimeManager());
config.DependencyResolver = new UnityResolver(container);
// Other Web API configuration not shown.
}