1

On an MVC / Entity Framework project I am moving away from the Repository pattern.

First, it has given me a few problemas ... And I think DBContext and IDBset kind of implement UnitOfWork and Repository.

So I am starting to use commands and queries. Here is an example:

public class ListPostsQuery {

  public ListPostsQuery() {
  }

  public List<Post> Execute(int currentPage, int pageSize) {

  }
}

How should I integrate or inject DBContext in my Queries / Commands?

Maybe using a wrapper for DBContext with Save method and exposing the sets?

Or should I just create a new context in Execute method?

  public List<Post> Execute(int currentPage, int pageSize) {

    using (Context = new DBContext) {
    }

  }

Could someone, please, advice me on this?

5
  • Why move away? What you can't do using Repository? The trade off looks the same for me! The question that you realy have to do here is, "How do I fix my repository?" Commented Sep 9, 2014 at 16:46
  • I am using a generic repository and it is quite complete ... But to me honest more and more I feel that using repositories is an abstraction over something that is already is it ... Commented Sep 9, 2014 at 16:49
  • I'm guessing you read Jimmy Bogard's article? He has a few examples lostechies.com/jimmybogard/2012/10/08/… Commented Sep 9, 2014 at 17:04
  • 1
    I would say repositories are definitely more testable than a query interface.. and a query interface is an abstraction as well. Both repository and query are abstractions. You can very well say that one abstraction is better than the other but if your only reason for moving away from repository is that "it's an abstraction", I've got bad news for you. Commented Sep 9, 2014 at 17:13
  • @jrummell yes I did read its article. I am just not sure what is its session ... Commented Sep 9, 2014 at 17:14

2 Answers 2

3

Use constructor injection, i.e. pass your context to service classes via constructor.

public class ListPostsQuery {

  private DbContext ctx;

  public ListPostsQuery( DbContext ctx ) {
     this.ctx = ctx;
  }

  public List<Post> Execute(int currentPage, int pageSize) {

     return ctx....

  }
}

This way you control the lifetime of your context once, e.g. in a controller factory or using an ioc framework of your choice.

Sign up to request clarification or add additional context in comments.

4 Comments

I am using StructureMap ... What life cycle would you use for DBContext? I wasn't sure if I should inject it directly or if I should wrap be in a class which implements an interface. Which one would you use?
@MDMoura you typically want to use one DbContext instance per request in a web application. I'm not sure what StructureMap calls that though.
Yes, that is what I am using: For<DbContext>().HybridHttpOrThreadLocalScoped().Use(() => new Context());
A custom controller factory for structuremap (first googled) joelabrahamsson.com/…
2

I would stick with

using (Context = new DBContext) 
{
}

The reason is that Context is not thread safe so if you will try to do things concurrently using the same context it could cause some really bad issues with your data.

Regarding the dependency injection: It is certainly great pattern but what is your profit of using it here? - you are still tightly coupled to the Context... - you won't be able to test your classes by injecting stub context...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.