File tree 4 files changed +58
-4
lines changed
4 files changed +58
-4
lines changed Original file line number Diff line number Diff line change
1
+ using PetaPoco ;
2
+
3
+ namespace UnitOfWork . DAL
4
+ {
5
+ //class does nothing with a DB and is used for testing
6
+ public class MockUnitOfWork : IUnitOfWork
7
+ {
8
+ private MockUnitOfWork ( )
9
+ {
10
+
11
+ }
12
+
13
+ private static IUnitOfWork _uow ;
14
+ public static IUnitOfWork Instance => _uow ?? ( _uow = new MockUnitOfWork ( ) ) ;
15
+
16
+ public void Dispose ( )
17
+ {
18
+
19
+ }
20
+
21
+ public void Commit ( )
22
+ {
23
+
24
+ }
25
+
26
+ public IDatabase Database { get ; }
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ namespace UnitOfWork . DAL
2
+ {
3
+ public class UnitOfWorkFactory
4
+ {
5
+ //having this setter her allows the unit tests to get a mocked uow if it wants or a DB bound one for integration\normal ops
6
+ public static UnitOfWorkType UnitOfWorkType { get ; set ; } = UnitOfWorkType . Database ;
7
+ public static IUnitOfWork Get ( string connectionString = "" )
8
+ {
9
+ if ( UnitOfWorkType == UnitOfWorkType . Mock )
10
+ {
11
+ //using a singleton here so that the mocking fw is happy that we've got the same instance
12
+ return MockUnitOfWork . Instance ;
13
+ }
14
+
15
+ return new PetaPocoUnitOfWork ( connectionString ) ;
16
+ }
17
+ }
18
+
19
+ public enum UnitOfWorkType
20
+ {
21
+ Database = 1 ,
22
+ Mock
23
+ }
24
+ }
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ class Program
9
9
static void Main ( string [ ] args )
10
10
{
11
11
//a unit of work is created
12
- using ( var uow = new PetaPocoUnitOfWork ( ) )
12
+ using ( var uow = UnitOfWorkFactory . Get ( ) )
13
13
{
14
14
//get an instance of a repo
15
15
var repo = new FooRepository ( ) ;
@@ -20,7 +20,7 @@ static void Main(string[] args)
20
20
}
21
21
22
22
//let's save something
23
- using ( var uow = new PetaPocoUnitOfWork ( ) )
23
+ using ( var uow = UnitOfWorkFactory . Get ( ) )
24
24
{
25
25
var repo = new FooRepository ( ) ;
26
26
@@ -36,7 +36,7 @@ static void Main(string[] args)
36
36
}
37
37
38
38
//let's delete something
39
- using ( var uow = new PetaPocoUnitOfWork ( ) )
39
+ using ( var uow = UnitOfWorkFactory . Get ( ) )
40
40
{
41
41
var repo = new FooRepository ( ) ;
42
42
@@ -52,7 +52,7 @@ static void Main(string[] args)
52
52
}
53
53
54
54
//the best part of a UOW pattern is that you can do many things across more than one repo and have them linked as a single transaction
55
- using ( var uow = new PetaPocoUnitOfWork ( ) )
55
+ using ( var uow = UnitOfWorkFactory . Get ( ) )
56
56
{
57
57
//look, we need to manipulate TWO repos in the same transaction
58
58
var fooRepo = new FooRepository ( ) ;
Original file line number Diff line number Diff line change 44
44
<Reference Include =" System.Xml" />
45
45
</ItemGroup >
46
46
<ItemGroup >
47
+ <Compile Include =" DAL\MockUnitOfWork.cs" />
47
48
<Compile Include =" DAL\PetaPocoUnitOfWork.cs" />
48
49
<Compile Include =" DAL\IUnitOfWork.cs" />
50
+ <Compile Include =" DAL\UnitOfWorkFactory.cs" />
49
51
<Compile Include =" Models\MyDbEntity.cs" />
50
52
<Compile Include =" Models\MyOtherDbEntity.cs" />
51
53
<Compile Include =" DAL\PetaPoco.cs" />
You can’t perform that action at this time.
0 commit comments