在基本控制器中管理 ISession...

发布于 2024-11-27 05:28:27 字数 1611 浏览 1 评论 0原文

我避免将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奶茶白久 2024-12-04 05:28:27

您还引入了对 ReadOnlyGenericRepository 的固定依赖项。我认为单元测试和实现都将更难以理解/可读。就我个人而言,我认为这个解决方案给你带来的负面影响比你不需要添加单个构造函数参数所带来的负面影响更多(无论如何,这都是通过 Resharper 中的简单 Alt-Enter 完成的)。

正面:

  • 不需要构造函数参数(或者换句话说:在 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:

  • No constructor argument required (Or to say it differently: Saves a single Alt-Enter strike in Resharper)

Negative:

  • Fix dependency to ReadOnlyGenericRepository
  • Requires a testclass overriding GetReadOnlySession() for each controller
  • Less understandable unittests and implementation

Also think about aggregation instead of inheritance in case there is more you have to pass to the base constructor.

苍白女子 2024-12-04 05:28:27

使用下面第二种方法管理对象的生命周期有什么问题吗?

对于基本控制器中的这个静态对象,单独对控制器进行单元测试可能会有点困难。另外,我不知道这个 IReadOnlySession 应该代表什么,但因为它是静态的,所以它将在所有用户之间共享 =>所有请求,因此您应该确保它是线程安全的,...我个人更喜欢通过 DI 框架注入到控制器中的存储库方法。

Is there any problems with managing the lifetime of the object using the second method below?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文