In this blog
The first thing
1: public interface IRepository<T> where T : IPerstistable
2: {
3: void Add(T item);
4:
5: IQueryable<T> GetAll();
6:
7: T GetItemByID(int id);
8:
9: void Delete(T item);
10:
11: int Count();
12:
13: void Save();
14: }
There is nothing too crazy here, I have created my generic interface and constrained my Type parameter saying that it must implement the IPersistable interface. This interface allows me to easily create a mock for testing!
Now I need to create a class that implements my
For
So we will first create a protected field of IObjectScope, which will allow all class that
1: protected IObjectScope scope;
2:
3: public Repository(IObjectScope objectScope)
4: {
5: scope = objectScope;
6: }
Also, when
1: public virtual void Add(T item)
2: {
3: scope.Add(item);
4: }
5:
6: public virtual IQueryable<T> GetAll()
7: {
8: var query = from items in scope.Extent<T>()
9: select items;
10:
11: return query;
12: }
13:
14: public virtual T GetItemByID(int id)
15: {
16: var query = from items in scope.Extent<T>()
17: where items.ID == id
18: select items;
19:
20: return query.FirstOrDefault<T>();
21: }
22:
23: public virtual void Delete(T item)
24: {
25: scope.Remove(item);
26:
27: }
28:
29: public virtual int Count()
30: {
31: return scope.Extent<T>().Count();
32: }
33:
34: public virtual void Save()
35: {
36: scope.Transaction.Commit();
37: }
Now we can create specific
Once these things are finished, we are ready to start working on our UI, so in the next blog we will take a look at large data concerns when working on with a web UI. So check back soon! Also, I have created a video that goes a little more in-depth about handling Object scope when using MVC, It should be uploaded shortly to Telerik TV.