在CarTrackr project中,它使用了一些技術,爲Asp.net Mvc網站中的所有請求僅創建一個存儲庫實例,並使用UnityControllerFactory類來管理所有存儲庫實例(發送到請求的控制器)。爲Asp.net Mvc應用程序使用單個Repository實例有什麼好處嗎?
與每個請求創建新存儲庫實例相比,使用單個存儲庫實例有什麼好處嗎?
我知道,它可能會提高整體性能。但是,它會導致任何傳送問題?
局部的Global.asax
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
RegisterDependencies();
}
protected static void RegisterDependencies() {
IUnityContainer container = new UnityContainer();
// Registrations
container.RegisterType<IUserRepository, UserRepository>(new ContextLifetimeManager<IUserRepository>());
container.RegisterType<ICarRepository, CarRepository>(new ContextLifetimeManager<ICarRepository>());
// Set controller factory
ControllerBuilder.Current.SetControllerFactory(
new UnityControllerFactory(container)
);
}
}
局部CarController.cs
[Authorize]
public class CarController : Controller
{
private IUserRepository UserRepository;
private ICarRepository CarRepository;
public CarController(IUserRepository userRepository, ICarRepository carRepository)
{
UserRepository = userRepository;
CarRepository = carRepository;
}
}
謝謝,
(回覆評論) – 2009-06-20 19:23:00