Skip to content

Commit 78ad311

Browse files
committed
Add Unit Of Work implementation
1 parent c3b520e commit 78ad311

46 files changed

Lines changed: 3192 additions & 8 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

DbContextScope.sln

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,35 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 14
44
VisualStudioVersion = 14.0.24720.0
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "DbContextScope.Ef7", "DbContextScope.Ef7\DbContextScope.Ef7.xproj", "{AB70542C-890D-4630-B27E-14F9587DD278}"
7-
EndProject
8-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Demo Application", "DemoApplication\Demo Application.xproj", "{C7710DE2-92AE-49C8-8645-DB1300A2CD9C}"
9-
EndProject
106
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{81DA8C35-7725-4BEF-B90C-F6D3040498FE}"
117
ProjectSection(SolutionItems) = preProject
128
global.json = global.json
139
EndProjectSection
1410
EndProject
1511
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{6258D4E6-ECD4-45A6-951D-211A52A1183E}"
1612
EndProject
13+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "DbContextScope.UnitOfWork.Core", "src\DbContextScope.UnitOfWork.Core\DbContextScope.UnitOfWork.Core.xproj", "{1C3207F7-9381-453F-8783-B3D52A0C9B8B}"
14+
EndProject
15+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "DbContextScope.UnitOfWork.Ef7", "src\DbContextScope.Ef7.UnitOfWork\DbContextScope.UnitOfWork.Ef7.xproj", "{E053CA11-B054-46D7-AF4E-84AEFF0AA1E3}"
16+
EndProject
17+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "DbContextScope.Ef7", "src\DbContextScope.Ef7\DbContextScope.Ef7.xproj", "{AB70542C-890D-4630-B27E-14F9587DD278}"
18+
EndProject
19+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Demo Application", "src\DemoApplication\Demo Application.xproj", "{C7710DE2-92AE-49C8-8645-DB1300A2CD9C}"
20+
EndProject
1721
Global
1822
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1923
Debug|Any CPU = Debug|Any CPU
2024
Release|Any CPU = Release|Any CPU
2125
EndGlobalSection
2226
GlobalSection(ProjectConfigurationPlatforms) = postSolution
27+
{1C3207F7-9381-453F-8783-B3D52A0C9B8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28+
{1C3207F7-9381-453F-8783-B3D52A0C9B8B}.Debug|Any CPU.Build.0 = Debug|Any CPU
29+
{1C3207F7-9381-453F-8783-B3D52A0C9B8B}.Release|Any CPU.ActiveCfg = Release|Any CPU
30+
{1C3207F7-9381-453F-8783-B3D52A0C9B8B}.Release|Any CPU.Build.0 = Release|Any CPU
31+
{E053CA11-B054-46D7-AF4E-84AEFF0AA1E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32+
{E053CA11-B054-46D7-AF4E-84AEFF0AA1E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
33+
{E053CA11-B054-46D7-AF4E-84AEFF0AA1E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
34+
{E053CA11-B054-46D7-AF4E-84AEFF0AA1E3}.Release|Any CPU.Build.0 = Release|Any CPU
2335
{AB70542C-890D-4630-B27E-14F9587DD278}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2436
{AB70542C-890D-4630-B27E-14F9587DD278}.Debug|Any CPU.Build.0 = Debug|Any CPU
2537
{AB70542C-890D-4630-B27E-14F9587DD278}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -33,6 +45,8 @@ Global
3345
HideSolutionNode = FALSE
3446
EndGlobalSection
3547
GlobalSection(NestedProjects) = preSolution
48+
{1C3207F7-9381-453F-8783-B3D52A0C9B8B} = {6258D4E6-ECD4-45A6-951D-211A52A1183E}
49+
{E053CA11-B054-46D7-AF4E-84AEFF0AA1E3} = {6258D4E6-ECD4-45A6-951D-211A52A1183E}
3650
{AB70542C-890D-4630-B27E-14F9587DD278} = {6258D4E6-ECD4-45A6-951D-211A52A1183E}
3751
{C7710DE2-92AE-49C8-8645-DB1300A2CD9C} = {6258D4E6-ECD4-45A6-951D-211A52A1183E}
3852
EndGlobalSection
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>e053ca11-b054-46d7-af4e-84aeff0aa1e3</ProjectGuid>
10+
<RootNamespace>DbContextScope.UnitOfWork.Ef7</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
13+
</PropertyGroup>
14+
<PropertyGroup>
15+
<SchemaVersion>2.0</SchemaVersion>
16+
</PropertyGroup>
17+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
18+
<ProduceOutputsOnBuild>True</ProduceOutputsOnBuild>
19+
</PropertyGroup>
20+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
21+
<ProduceOutputsOnBuild>True</ProduceOutputsOnBuild>
22+
</PropertyGroup>
23+
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
24+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Data;
2+
using DbContextScope.UnitOfWork.Core.Interfaces;
3+
using Microsoft.Data.Entity.Storage;
4+
5+
namespace DbContextScope.UnitOfWork.Ef7
6+
{
7+
public interface IEntityFrameworkUnitOfWork<TContext> : IUnitOfWork where TContext : class
8+
{
9+
IRelationalTransaction BeginTransaction();
10+
11+
IRelationalTransaction BeginTransaction(IsolationLevel isolationLevel);
12+
}
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("DbContextScope.Ef7.UnitOfWork")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("DbContextScope.Ef7.UnitOfWork")]
13+
[assembly: AssemblyCopyright("Copyright © 2016")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("e053ca11-b054-46d7-af4e-84aeff0aa1e3")]
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Threading.Tasks;
6+
using DbContextScope.Ef7.Interfaces;
7+
using Microsoft.Data.Entity;
8+
9+
namespace DbContextScope.UnitOfWork.Ef7.Repository
10+
{
11+
/// <summary>
12+
/// Implements the IEntityFrameworkRepository<TEntity> interface to represent an Entity Framework repository.
13+
/// </summary>
14+
/// <typeparam name="TEntity"></typeparam>
15+
/// <typeparam name="TContext"></typeparam>
16+
public class EntityFrameworkRepository<TEntity, TContext> : IEntityFrameworkRepository<TEntity, TContext>
17+
where TEntity : class
18+
where TContext : DbContext
19+
{
20+
#region Attributes
21+
22+
readonly IAmbientDbContextLocator dbContextLocator;
23+
24+
#endregion
25+
26+
#region IEntityFrameworkRepository<TEntity, TContext> Implementation
27+
28+
/// <summary>
29+
/// Constructor that receives a DbContextLocator instance.
30+
/// </summary>
31+
/// <param name="dbContextLocator">DbContextLocator instance.</param>
32+
public EntityFrameworkRepository(IAmbientDbContextLocator dbContextLocator)
33+
{
34+
if (dbContextLocator == null) throw new ArgumentNullException(nameof(dbContextLocator));
35+
36+
this.dbContextLocator = dbContextLocator;
37+
}
38+
39+
public virtual void Add(TEntity entity)
40+
{
41+
GetDbSet().Add(entity);
42+
}
43+
44+
public IQueryable<TEntity> AsQueryable()
45+
{
46+
return GetDbSet().AsQueryable();
47+
}
48+
49+
public virtual void Attach(TEntity entity)
50+
{
51+
GetDbSet().Attach(entity);
52+
}
53+
54+
public virtual void Delete(TEntity entity)
55+
{
56+
if (GetDbContext().Entry(entity).State == EntityState.Detached)
57+
{
58+
GetDbSet().Attach(entity);
59+
}
60+
GetDbSet().Remove(entity);
61+
}
62+
63+
public virtual void Edit(TEntity entity)
64+
{
65+
GetDbContext().Entry(entity).State = EntityState.Modified;
66+
}
67+
68+
public virtual TEntity First(Expression<Func<TEntity, bool>> predicate = null)
69+
{
70+
return GetDbSet().First(predicate);
71+
}
72+
73+
public virtual TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate = null)
74+
{
75+
return GetDbSet().FirstOrDefault(predicate);
76+
}
77+
78+
public virtual async Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate = null)
79+
{
80+
return await GetDbSet().FirstOrDefaultAsync(predicate);
81+
}
82+
83+
public virtual IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> predicate = null,
84+
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
85+
params Expression<Func<TEntity, object>>[] includeProperties)
86+
{
87+
return PrepareGetQuery(predicate, orderBy, includeProperties).ToList();
88+
}
89+
90+
public virtual async Task<IEnumerable<TEntity>> GetAsync(Expression<Func<TEntity, bool>> predicate = null,
91+
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
92+
params Expression<Func<TEntity, object>>[] includeProperties)
93+
{
94+
return await PrepareGetQuery(predicate, orderBy, includeProperties).ToListAsync();
95+
}
96+
97+
public virtual TEntity LastOrDefault(Expression<Func<TEntity, bool>> predicate = null)
98+
{
99+
return GetDbSet().LastOrDefault(predicate);
100+
}
101+
102+
public virtual TEntity Single(Expression<Func<TEntity, bool>> predicate)
103+
{
104+
return GetDbSet().Single(predicate);
105+
}
106+
107+
public virtual async Task<TEntity> SingleAsync(Expression<Func<TEntity, bool>> predicate)
108+
{
109+
return await GetDbSet().SingleAsync(predicate);
110+
}
111+
112+
public virtual TEntity SingleOrDefault(Expression<Func<TEntity, bool>> predicate)
113+
{
114+
return GetDbSet().SingleOrDefault(predicate);
115+
}
116+
117+
public virtual async Task<TEntity> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate)
118+
{
119+
return await GetDbSet().SingleOrDefaultAsync(predicate);
120+
}
121+
122+
public virtual void Update(TEntity entityToUpdate)
123+
{
124+
GetDbSet().Attach(entityToUpdate);
125+
GetDbContext().Entry(entityToUpdate).State = EntityState.Modified;
126+
}
127+
128+
#endregion
129+
130+
#region Private Methods
131+
132+
DbContext GetDbContext()
133+
{
134+
return dbContextLocator.Get<TContext>();
135+
}
136+
137+
DbSet<TEntity> GetDbSet()
138+
{
139+
return dbContextLocator.Get<TContext>().Set<TEntity>();
140+
}
141+
142+
/// <summary>
143+
///
144+
/// </summary>
145+
/// <param name="predicate"></param>
146+
/// <param name="orderBy"></param>
147+
/// <param name="includeProperties"></param>
148+
/// <returns></returns>
149+
IQueryable<TEntity> PrepareGetQuery(Expression<Func<TEntity, bool>> predicate = null,
150+
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
151+
params Expression<Func<TEntity, object>>[] includeProperties)
152+
{
153+
var query = AsQueryable();
154+
155+
if (predicate != null)
156+
{
157+
query = query.Where(predicate);
158+
}
159+
160+
foreach (var includeProperty in includeProperties)
161+
{
162+
query = query.Include(includeProperty);
163+
}
164+
165+
if (orderBy != null)
166+
{
167+
return orderBy(query);
168+
}
169+
170+
return query;
171+
}
172+
173+
#endregion
174+
}
175+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using DbContextScope.UnitOfWork.Core.Repository;
2+
using Microsoft.Data.Entity;
3+
4+
namespace DbContextScope.UnitOfWork.Ef7.Repository
5+
{
6+
/// <summary>
7+
/// Defines a generic Entity Framework repository.
8+
/// </summary>
9+
/// <typeparam name="TEntity">Type of the entities that the repository will manage.</typeparam>
10+
/// <typeparam name="TContext">Type of the Entity Framework context.</typeparam>
11+
public interface IEntityFrameworkRepository<TEntity, TContext> : IRepository<TEntity>
12+
where TEntity : class
13+
where TContext : DbContext
14+
{
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": "1.0.0-*",
3+
"description": "DbContextScope.UnitOfWork.Ef7 Class Library",
4+
"authors": [ "Camilo" ],
5+
"tags": [ "" ],
6+
"projectUrl": "",
7+
"licenseUrl": "",
8+
9+
"dependencies": {
10+
"DbContextScope.Ef7": "0.1.0",
11+
"DbContextScope.UnitOfWork.Core": ""
12+
},
13+
14+
"frameworks": {
15+
"dnx451": { },
16+
"dnx46": { }
17+
}
18+
}

0 commit comments

Comments
 (0)