Asp.Net mvc 与 ServiceSTack Mvc powerpack
AppHost中有一行:
ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));
这是我的代码
public class BaseController<T>:Controller
{
protected IRepository<T> Repository {get;set;}
public ActionResult Detail(T t){}
}
public class CarController:BaseController<Car>
{
}
我将在 App.Host 中使用此代码
container.Register<IRepository<Car>>(c => new CarRepository());
如何使用 AutoWire 存储库?
最佳答案
仅注入(inject)公共(public)属性或构造函数,因此请尝试将存储库设为公共(public):
public class BaseController<T>:Controller
{
public IRepository<T> Repository {get;set;}
public ActionResult Detail(T t){}
}
public class CarController:BaseController<Car> {}
此外,如果您使用 MVC PowerPack您可能想继承ServiceStackController相反,这给你带来了很多好处,比如使用ServiceStack的JSON序列化,可以访问ServiceStack的共享Cache和 session 提供者以及能够共享 ServiceStack Authentication/Authorization MVC Controller 和 ServiceStack 上的功能网络服务(如果您正在使用任何服务)。
关于asp.net-mvc - ServiceStack 如何在 MyController<T> 上 Autowiring 自定义 IRepository<T> 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10791489/