Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

The Full Stack, Part 5: Implementing the repository with EF Code First and Dependency Injection

Download

Right click “Save as…”

In this episode, Jon moves converts the previous data access code to use the Repository pattern, looking at some new features in Entity Framework 4 CTP 5 which make this a lot easier. Then we wrap up by using Dependency Injection using Ninject and MVC 3's new support for service location.

Tags:

Follow the Discussion

  • Thank you guys. The Full Stack is great! Smiley

  • ChevalN2Cheval Why not null?

    2 repositories? Doesn't that waste time testing the fake repository as you can create bugs in the real one? Why not just pass in a context to assign to a local context interface variable so that you can have one repository that handles everything to test once? The benefit also is that you can have multiple data sources; test data, development data (great for design and debug time in WPF & SilverLight) and run time data contexts. It also makes the controller explaination simplier as you still have a context but it's now the repository that is doing the work so that you can use the same code in multiple places and test only once.

    Another code smell "db.People.Find(", what ever happened to DRY (Don't Repeat Yourself!) How about for Exits:
    var person = this.Rerieve(id);
    return person != null;

    Why are you using IEnumerable<Person> and not IList<Person>? Wouldn't you end up converting it later anyway in most cases?

    Back to the repository thing, I see further on that you use dependency injection and I suppose that creating a gateway to assign the right context to Ninject to supply both a context and a repository might be over kill but I think that is still better than 2 repositories when someone is creating a real application in the long run. Learning the right way first is easier than unlearning the wrong way later is what I say.

    This was a good episode as it gets to the meat of design and development.

     

  • Bob CravensBob Cravens

    Great work guys. As someone already mentioned, this episode gets into a lot of the architecture choices that lead to a maintainable solution. A few questions that maybe you can answer:
    1. Why not create a generic repository that is used to implement the PersonRepository? Seems like you have a very specific repository that can only be used to manage Person entities. A little bit of generics and you can write that code once. It can then be reused to manage other entities. I know that you currently don't have any other entities, but it seems worth the effort. Thoughts?
    2. At the end dependency inject was introduced...go Ninject! Time was spent cleaning up the repository dependency in the controller. What about the context dependency in the repository? I think there is value in letting Ninject wire all that up for you. What do you guys think?
    Bob

  • Unfortunately I cant watch the videos. They say my browser doesnt support HTML5 or silverlight, but im using IE9 and have the current Silverlight version installed. Hopefully smn will figure this out because I'm really interested what they have to say about the subject.

  • MarkusMarkus

    @Sikter: I can't use Silverlight either (for some reason I get no sound). Try one of the Download links on the right - works great for me.

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.