Repository pattern with EF4 CTP5

前端 未结 2 763
梦如初夏
梦如初夏 2021-02-06 13:43

I\'m trying to implement the repository pattern with ef4 ctp5, I came up with something but I\'m no expert in ef so I want to know if what I did is good.

<
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-06 13:58

    You need to step back and think about what the repository should be doing. A repository is used for retrieving records, adding records, and updating records. The repository you created barely handles the first case, handles the second case but not efficiently, and doesn't at all handle the 3rd case.

    Most generic repositories have an interface along the lines of

    public interface IRepository where T : class
    {
        IQueryable Get();
        void Add(T item);
        void Delete(T item);
        void CommitChanges();
    }
    

    For retrieving records, you can't just call the whole set with AsEnumerable() because that will load every database record for that table into memory. If you only want Users with the username of username1, you don't need to download every user for the database as that will be a very large database performance hit, and a large client performance hit for no benefit at all.

    Instead, as you will see from the interface I posted above, you want to return an IQueryable object. IQuerables allow whatever class that calls the repository to use Linq and add filters to the database query, and once the IQueryable is run, it's completely run on the database, only retrieving the records you want. The database is much better at sorting and filtering data then your systems, so it's best to do as much on the DB as you can.

    Now in regards to inserting data, you have the right idea but you don't want to call SaveChanges() immediately. The reason is that it's best to call Savechanges() after all your db operations have been queued. For example, If you want to create a user and his profile in one action, you can't via your method, because each Insert call will cause the data to be inserted into the database then.

    Instead what you want is to separate out the Savechanges() call into the CommitChanges method I have above.

    This is also needed to handle updating data in your database. In order to change an Entity's data, Entity Framework keeps track of all records it has received and watches them to see if any changes have been made. However, you still have to tell the Entity Framework to send all changed data up to the database. This happenes with the context.SaveChanges() call. Therefore, you need this to be a separate call so you are able to actually update edited data, which your current implementation does not handle.


    Edit: Your comment made me realize another issue that I see. One downfall is that you are creating a data context inside of the repository, and this isn't good. You really should have all (or most) of your created repositories sharing the same instance of your data context.

    Entity Framework keeps track of what context an entity is tracked in, and will exception if you attempt to update an entity in one context with another. This can occur in your situation when you start editing entities related to one another. It also means that your SaveChanges() call is not transactional, and each entity is updated/added/deleted in it's own transaction, which can get messy.

    My solution to this in my Repositories, is that the DbContext is passed into the repository in the constructor.

提交回复
热议问题