Skip to content

Commit fe0b001

Browse files
committed
Add a testable uow factory
1 parent 285af06 commit fe0b001

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

UnitOfWork/DAL/MockUnitOfWork.cs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

UnitOfWork/DAL/UnitOfWorkFactory.cs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

UnitOfWork/Program.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Program
99
static void Main(string[] args)
1010
{
1111
//a unit of work is created
12-
using (var uow = new PetaPocoUnitOfWork())
12+
using (var uow = UnitOfWorkFactory.Get())
1313
{
1414
//get an instance of a repo
1515
var repo = new FooRepository();
@@ -20,7 +20,7 @@ static void Main(string[] args)
2020
}
2121

2222
//let's save something
23-
using (var uow = new PetaPocoUnitOfWork())
23+
using (var uow = UnitOfWorkFactory.Get())
2424
{
2525
var repo = new FooRepository();
2626

@@ -36,7 +36,7 @@ static void Main(string[] args)
3636
}
3737

3838
//let's delete something
39-
using (var uow = new PetaPocoUnitOfWork())
39+
using (var uow = UnitOfWorkFactory.Get())
4040
{
4141
var repo = new FooRepository();
4242

@@ -52,7 +52,7 @@ static void Main(string[] args)
5252
}
5353

5454
//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())
5656
{
5757
//look, we need to manipulate TWO repos in the same transaction
5858
var fooRepo = new FooRepository();

UnitOfWork/UnitOfWork.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@
4444
<Reference Include="System.Xml" />
4545
</ItemGroup>
4646
<ItemGroup>
47+
<Compile Include="DAL\MockUnitOfWork.cs" />
4748
<Compile Include="DAL\PetaPocoUnitOfWork.cs" />
4849
<Compile Include="DAL\IUnitOfWork.cs" />
50+
<Compile Include="DAL\UnitOfWorkFactory.cs" />
4951
<Compile Include="Models\MyDbEntity.cs" />
5052
<Compile Include="Models\MyOtherDbEntity.cs" />
5153
<Compile Include="DAL\PetaPoco.cs" />

0 commit comments

Comments
 (0)