If I'm using Entity Framework 6, with VS2013, is a generic repository really necessary?

Mar 25, 2014 at 5:48 PM
Edited Mar 25, 2014 at 7:06 PM
I am re-examining my implementation of the generic unit of work and repository framework.

I am using EF6, and VS2013. As such, VS contains WebAPI controller templates that auto-generate WebAPI 2 OData Controller with Actions, using Entity Framework code like this:

// GET odata/UserProjects(5)/WebsiteRequiredKeywords
    [Queryable]
    public IQueryable<WebsiteRequiredKeyword> GetWebsiteRequiredKeywords([FromODataUri] int key)
    {
        return _db.Websites.Where(m => m.WebsiteId == key).SelectMany(m => m.WebsiteRequiredKeywords);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            _db.Dispose();
        }
        base.Dispose(disposing);
    }

    private bool WebsiteExists(int key)
    { . . .
In looking at the CustomerController class in the sample code - it all looks very familiar. However, I would have to refactor the auto-generated code to make use of the generic repository syntax, modify the constructor, etc.

I am trying to now find the value in having to perform this additional work. While I have successfully used the generic repository and unit of work patterns in previous projects, for new ones, it is just more work than it's worth?

If you're using DI, then I can see the value in the generic repository. However there is a lot of code-refactoring that would need to be done to the WebAPI 2 OData Controller with Actions, using Entity Framework generated code.

Using this pattern, do you have an MVC5 controller code template for WebAPI 2 controller variants?
Coordinator
Mar 26, 2014 at 4:21 PM
Please copy the CodeTemplates folder to your MVC application, this will generate you an ODataController using the Repository and UnitOfWork: https://genericunitofworkandrepositories.codeplex.com/SourceControl/latest#main/Sample/Northwind.Web/CodeTemplates .

There is plenty of documentation and my documented thoughts around the benefits of implementing Unit Of Work and the Repository pattern, you should do your research and make the the decision if this is the right thing for you, obviously it's a best practice if your going to ask me.
Mar 26, 2014 at 7:33 PM
Great stuff!

I copied the folder into my project, but don't see a new OData WebAPI Controller Add/New Scaffolded Item or Add/Controller. I also opened the Sample\Northwind.Web related solution and project and was unable to see the option in VS.

How do you go about making use of the new template?


Thanks.
Coordinator
Mar 26, 2014 at 8:54 PM
Thanks, the template simply overrides the default one therefore you will not get a new option, so continue selecting the Web Api OData Controller.
Marked as answer by lelong37 on 3/31/2014 at 10:22 PM
Sep 16, 2014 at 5:05 PM
It looks like that file is no longer there, was it removed in an update?