none
Generics and EF Code First RRS feed

  • Question

  • It seems like there is an opportunity with Generics and the Entity Framework Code First model to greatly simplify CRUD activities. For example, instead of having a "Get" for 100 tables, it seems like these could be consolidated into a few using Generics.

    Could you please point me to some sample code using Generics, Code First, and Crud? 

    Saturday, December 19, 2015 5:52 PM

Answers

  • >>Could you please point me to some sample code using Generics, Code First, and Crud?

    Please refer to my blog post for an example of how implement a generic data access layer using Entity Framework: http://blog.magnusmontin.net/2013/05/30/generic-dal-using-entity-framework/

    The sample code is indeed based on database first, but as far as the generic methods are concerned the concepts are exactly the same:

    public interface IGenericDataRepository<T> where T : class
        {
            IList<T> GetAll(params Expression<Func<T, object>>[] navigationProperties);
            IList<T> GetList(Func<T, bool> where, params Expression<Func<T, object>>[] navigationProperties);
            T GetSingle(Func<T, bool> where, params Expression<Func<T, object>>[] navigationProperties);
            void Add(params T[] items);
            void Update(params T[] items);
            void Remove(params T[] items);
        }

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by DaveIII Monday, December 21, 2015 8:12 PM
    Saturday, December 19, 2015 8:57 PM

All replies

  • http://blog.falafel.com/implement-step-step-generic-repository-pattern-c/

    http://www.codeproject.com/Articles/814768/CRUD-Operations-Using-the-Generic-Repository-Patte

    I myself am not a big fan of the Repository pattern and prefer the Data Access Object pattern on a table by table basis to give me the granular  control that is needed.  

    Saturday, December 19, 2015 6:12 PM
  • EF is already generic.

    Each table needs to have a specific definition for it's columns and their metadata.

    You define those in classes.

    You can extend those using inheritance and partial classes (rather than generics).

    In fact it isn't necessary to specifically use code first because partial classes can inherit.

    I'm not really sure I follow what you mean about a "get" for 100 tables.

    You can use linq to get your data.

    And you can chain linq queries, so you can have a base query selects everything from a table. Then chain one that selects on department = x. And optionally chain it with numerous others.

    There's often not much value in making a generic get < t> though.

    You almost always have criteria - a whole table at a time isn't very practical.

    So having a base generic get isn't really saving anything.

    PS

    I also far prefer database first. It's quicker and you can see what you're getting easier.

    I've worked on several projects where devs had gone code first and generated database which were awfully badly designed. They didn't understand databases.

    As a result, performance was terrible.

    I fixed them up. On 2 of those projects they switched from code first to database first.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles


    • Edited by Andy ONeill Saturday, December 19, 2015 6:23 PM
    Saturday, December 19, 2015 6:20 PM
  • I also far prefer database first. It's quicker and you can see what you're getting easier.

    I've worked on several projects where devs had gone code first and generated database which were awfully badly designed. They didn't understand databases.

    As a result, performance was terrible.

    I fixed them up. On 2 of those projects they switched from code first to database first.

    I want to add to this, because code first seems to have become the gospel in MVC and WEB API controllers, which  is nothing but trouble, with developers talking nonsense that a DAL is not needed and the controller is the DAL for the naïve. :) Yeah,  that's all great for a mickey mouse tutorial, but I'll choose DB first every time for the more complicated ORM usage.

    Saturday, December 19, 2015 7:20 PM
  • >>Could you please point me to some sample code using Generics, Code First, and Crud?

    Please refer to my blog post for an example of how implement a generic data access layer using Entity Framework: http://blog.magnusmontin.net/2013/05/30/generic-dal-using-entity-framework/

    The sample code is indeed based on database first, but as far as the generic methods are concerned the concepts are exactly the same:

    public interface IGenericDataRepository<T> where T : class
        {
            IList<T> GetAll(params Expression<Func<T, object>>[] navigationProperties);
            IList<T> GetList(Func<T, bool> where, params Expression<Func<T, object>>[] navigationProperties);
            T GetSingle(Func<T, bool> where, params Expression<Func<T, object>>[] navigationProperties);
            void Add(params T[] items);
            void Update(params T[] items);
            void Remove(params T[] items);
        }

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by DaveIII Monday, December 21, 2015 8:12 PM
    Saturday, December 19, 2015 8:57 PM