开发者

managing ISession in base controller...

开发者 https://www.devze.com 2023-03-24 06:49 出处:网络
I am avoiding injecting in my ISession into every controller, thus avoiding this: Ninject with a base controller?

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 Abstrac开发者_运维百科tBaseController, can unit testing be achieved?

private IReadOnlySession readonlysession;
public IReadOnlySession _repo
{
   get { return (readonlysession ?? (readonlysession = GetReadOnlySession())); }
}

protected virtual IReadOnlySession GetReadOnlySession()
{
  return new ReadOnlyGenericRepository();
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消