Entity Framework and A Generic Repository
Entity Framework and A Generic Repository
Introduction
This article presents a high-level overview of multi-layered
.Net application that implements the Repository Pattern
along with the Entity Framework on its Data Access layer.
The application uses Inversion of Control to decouple
abstractions
(interfaces)
from
implementations.
More specifically,
it
uses
Ninject
to
implement
Dependency Injection.
This separation of concerns allows us to dissect modules
and swap them with other implementations. This is
achieved
by
relying
on
contracts
rather
than
implementations which results on code that is agnostic of
how operations are implemented behind the interfaces.
For example, if you design your business logic to rely on
an interface that follows the Repository Pattern as a
data persistence mechanism, for all intends and purposes,
your business logic will treat this dependency as a
memory domain object collection. Therefore, for
production you could provide an implementation of that
dependency that uses the Entity Framework behind the
scenes to store and retrieve data from a SQL database. For
automated unit testing, you could provide a completely
different implementation that stores and retrieves data
from in-memory objects.
Evidently, you could execute all your tests against the
database; however, this setting greatly helps isolating unit
tests and cut dependencies on other resources, such as
the database, which may need more work to setup up and
tear up for unit testing (especially true if you have other
team members).
Before moving forward, make sure you read What's
Inversion of Control? if you are not familiar with Inversion
of Control or Dependency Injection or would like to hear
more about the advantages of interface-base design
Note: This article does not get into details on how to use
all these technologies and patterns, instead it provides
references to other sources and shows you how they all fit
together in a single application.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{ get; set; }
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
Context.Dispose();
}
}
Usage
This is how you would declare your interface. Notice that
besides declaring the common code (Add, Delete, GetAll,
etc.) it also declares and additional method specific to this
repository GetSubordinates()
?
1
2
3
4
1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1
2
3
4
5
6
7
8
9
10
11
12
13
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
//repoositories
kernel.Bind<idbcontextfactory>().To<dbcontextfactory>().InRequ
kernel.Bind<iemployeerepository>().To<employeerepository>();
//... more repositories...
}
One thing to note is the use of the Request scope for the
DbContextFactory class which tells Ninject to create a
single instance of this object per HTTP request. In other
words, every time a user requests a page, no mater how
many repository classes are involved, Ninject will create a
single factory class and, therefore, a
single DbContext instance. This also ensures that the
object is destroyed once the request completes. Good
stuff..
Note: In the examples provided here I've been using
dependency injection through constructors.
Putting
it
all
Repositories
together
Using
the
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15