在基本控制器中管理 ISession...
我避免将 ISession 注入到每个控制器中,从而避免这种情况:Ninject with a base controller?< /a>
我正在尝试访问基本控制器内的 IRepository,以便我可以在整个应用程序中使用它,使用下面的第二种方法管理对象的生命周期是否有任何问题?基本上我只想在每次操作请求需要它时再次创建它...
//使用 Ninject 注入...
private readonly IReadOnlySession _repo;
public SimpleController(IReadOnlySession repo)
{
_repo = repo;
}
//从基本控制器获取 ->我的首选方法...
public abstract class AbstractBaseController : Controller
{
public AbstractBaseController() { }
private static IReadOnlyGenericRepository readonlysession;
public static IReadOnlyGenericRepository ReadOnlySession
{
get { return (readonlysession ?? (readonlysession = new ReadOnlyGenericRepository())); }
}
}
//然后使用IReadOnlyGenericRepository访问
var detail = ReadOnlySession.Single<Cat>(x=> x.CatID== _catid);
简单地继承自:
public interface IReadOnlySession
{
T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class, new();
System.Linq.IQueryable<T> All<T>() where T : class, new();
}
更新:在我的AbstractBaseController中声明下面的代码,可以实现单元测试吗?
private IReadOnlySession readonlysession;
public IReadOnlySession _repo
{
get { return (readonlysession ?? (readonlysession = GetReadOnlySession())); }
}
protected virtual IReadOnlySession GetReadOnlySession()
{
return new ReadOnlyGenericRepository();
}
I am avoiding injecting in my ISession into every controller, thus avoiding this: Ninject with a base controller?
I am trying to access my IRepository inside my base controller so i could use it throughout my app, is there any problems with managing the lifetime of the object using the second method below? Basically I only want to create it again every time I need it per action request...
//Using Ninject to inject...
private readonly IReadOnlySession _repo;
public SimpleController(IReadOnlySession repo)
{
_repo = repo;
}
//Get from Base controller -> my preferred method...
public abstract class AbstractBaseController : Controller
{
public AbstractBaseController() { }
private static IReadOnlyGenericRepository readonlysession;
public static IReadOnlyGenericRepository ReadOnlySession
{
get { return (readonlysession ?? (readonlysession = new ReadOnlyGenericRepository())); }
}
}
//Then access using
var detail = ReadOnlySession.Single<Cat>(x=> x.CatID== _catid);
IReadOnlyGenericRepository simply inherits from:
public interface IReadOnlySession
{
T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class, new();
System.Linq.IQueryable<T> All<T>() where T : class, new();
}
Update: Declaring the code below in my AbstractBaseController, can unit testing be achieved?
private IReadOnlySession readonlysession;
public IReadOnlySession _repo
{
get { return (readonlysession ?? (readonlysession = GetReadOnlySession())); }
}
protected virtual IReadOnlySession GetReadOnlySession()
{
return new ReadOnlyGenericRepository();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还引入了对
ReadOnlyGenericRepository
的固定依赖项。我认为单元测试和实现都将更难以理解/可读。就我个人而言,我认为这个解决方案给你带来的负面影响比你不需要添加单个构造函数参数所带来的负面影响更多(无论如何,这都是通过 Resharper 中的简单 Alt-Enter 完成的)。正面:
负面:
ReadOnlyGenericRepository
的依赖关系GetReadOnlySession()
的测试类对于每个控制器还要考虑聚合而不是继承,以防您必须传递更多内容给基本构造函数。
You also introduce a fixed dependency to
ReadOnlyGenericRepository
. And I in my opinion the unittests and implementation both will be much less understandable/readable. Personally, I think this solution gives you more and worse negative points than what you win by not having to add a single constructor parameter (Which is done by a simple Alt-Enter in Resharper anyway).Positive:
Negative:
ReadOnlyGenericRepository
GetReadOnlySession()
for each controllerAlso think about aggregation instead of inheritance in case there is more you have to pass to the base constructor.
对于基本控制器中的这个静态对象,单独对控制器进行单元测试可能会有点困难。另外,我不知道这个
IReadOnlySession
应该代表什么,但因为它是静态的,所以它将在所有用户之间共享 =>所有请求,因此您应该确保它是线程安全的,...我个人更喜欢通过 DI 框架注入到控制器中的存储库方法。Unit testing your controllers in isolation might be a little hard with this static object in the base controller. Also I don't know what is this
IReadOnlySession
supposed to represent but because it is static it will be shared among all users => all requests, so you should ensure that it is thread-safe, ... Personally I prefer the repository approach which is being injected into the controllers through a DI framework.