Skip to content

Commit 6b13e42

Browse files
Added a new entity framework repository
1 parent b66ab3d commit 6b13e42

File tree

13 files changed

+571
-6
lines changed

13 files changed

+571
-6
lines changed

DotNetToolkit.Repository.sln

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27130.2003
4+
VisualStudioVersion = 15.0.27004.2006
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{DD273D5E-6D6C-41FA-A0C8-646CC53C4DC3}"
77
EndProject
@@ -11,6 +11,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{DAB3DD1E-A
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetToolkit.Repository.Test", "test\DotNetToolkit.Repository.Test\DotNetToolkit.Repository.Test.csproj", "{EAE7B507-D908-4650-A301-C3341F005A51}"
1313
EndProject
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetToolkit.Repository.EntityFramework", "src\DotNetToolkit.Repository.EntityFramework\DotNetToolkit.Repository.EntityFramework.csproj", "{8DBCF7DE-532D-45DF-A6B8-5D89415C5B77}"
15+
EndProject
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetToolkit.Repository.Integration.Test", "test\DotNetToolkit.Repository.Integration.Test\DotNetToolkit.Repository.Integration.Test.csproj", "{2F99B66F-A347-4E8D-8ACA-5A34246A1FA6}"
17+
EndProject
1418
Global
1519
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1620
Debug|Any CPU = Debug|Any CPU
@@ -25,13 +29,23 @@ Global
2529
{EAE7B507-D908-4650-A301-C3341F005A51}.Debug|Any CPU.Build.0 = Debug|Any CPU
2630
{EAE7B507-D908-4650-A301-C3341F005A51}.Release|Any CPU.ActiveCfg = Release|Any CPU
2731
{EAE7B507-D908-4650-A301-C3341F005A51}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{8DBCF7DE-532D-45DF-A6B8-5D89415C5B77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{8DBCF7DE-532D-45DF-A6B8-5D89415C5B77}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{8DBCF7DE-532D-45DF-A6B8-5D89415C5B77}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{8DBCF7DE-532D-45DF-A6B8-5D89415C5B77}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{2F99B66F-A347-4E8D-8ACA-5A34246A1FA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{2F99B66F-A347-4E8D-8ACA-5A34246A1FA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
38+
{2F99B66F-A347-4E8D-8ACA-5A34246A1FA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
39+
{2F99B66F-A347-4E8D-8ACA-5A34246A1FA6}.Release|Any CPU.Build.0 = Release|Any CPU
2840
EndGlobalSection
2941
GlobalSection(SolutionProperties) = preSolution
3042
HideSolutionNode = FALSE
3143
EndGlobalSection
3244
GlobalSection(NestedProjects) = preSolution
3345
{76BC5AB7-1198-4C99-B2C3-3B734931F8EE} = {DD273D5E-6D6C-41FA-A0C8-646CC53C4DC3}
3446
{EAE7B507-D908-4650-A301-C3341F005A51} = {DAB3DD1E-AD99-46C9-AC42-07E2F03D5A06}
47+
{8DBCF7DE-532D-45DF-A6B8-5D89415C5B77} = {DD273D5E-6D6C-41FA-A0C8-646CC53C4DC3}
48+
{2F99B66F-A347-4E8D-8ACA-5A34246A1FA6} = {DAB3DD1E-AD99-46C9-AC42-07E2F03D5A06}
3549
EndGlobalSection
3650
GlobalSection(ExtensibilityGlobals) = postSolution
3751
SolutionGuid = {96973E0C-81D1-42DE-9F78-7103241B4E07}

appveyor.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ build:
4444

4545
after_build:
4646
- dotnet pack .\src\DotNetToolkit.Repository\DotNetToolkit.Repository.csproj --configuration Release
47+
- dotnet pack .\src\DotNetToolkit.Repository.EntityFramework\DotNetToolkit.Repository.EntityFramework.csproj --configuration Release
4748

4849
#---------------------------------#
4950
# tests configuration #
5051
#---------------------------------#
5152
test_script:
5253
- dotnet test .\test\DotNetToolkit.Repository.Test\DotNetToolkit.Repository.Test.csproj --configuration Release
54+
- dotnet test .\test\DotNetToolkit.Repository.Integration.Test\DotNetToolkit.Repository.Integration.Test.csproj --configuration Release
5355

5456
artifacts:
5557
- path: '**\DotNetToolkit.Repository.*.nupkg' # find all NuGet packages recursively
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<Import Project="..\..\build\common.props" />
4+
5+
<PropertyGroup>
6+
<TargetFramework>net451</TargetFramework>
7+
<AssemblyName>DotNetToolkit.Repository.EntityFramework</AssemblyName>
8+
<RootNamespace>DotNetToolkit.Repository.EntityFramework</RootNamespace>
9+
<Description>A toolkit that provides a lightweight starter kit for using the Repository pattern for the Entity Framework.</Description>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="EntityFramework" Version="6.2.0" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\DotNetToolkit.Repository\DotNetToolkit.Repository.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace DotNetToolkit.Repository.EntityFramework
2+
{
3+
using System.Data.Entity;
4+
5+
/// <summary>
6+
/// Represents a repository for entity framework.
7+
/// </summary>
8+
public class EfRepository<TEntity, TKey> : EfRepositoryBase<TEntity, TKey> where TEntity : class
9+
{
10+
#region Constructors
11+
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="EfRepository{TEntity, TKey}" /> class.
14+
/// </summary>
15+
/// <param name="context">The database context.</param>
16+
public EfRepository(DbContext context) : base(context) { }
17+
18+
#endregion
19+
}
20+
21+
/// <summary>
22+
/// Represents a repository for entity framework with a default primary key value of type integer.
23+
/// </summary>
24+
public class EfRepository<TEntity> : EfRepositoryBase<TEntity, int> where TEntity : class
25+
{
26+
#region Constructors
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="EfRepository{TEntity}" /> class.
30+
/// </summary>
31+
/// <param name="context">The database context.</param>
32+
public EfRepository(DbContext context) : base(context) { }
33+
34+
#endregion
35+
}
36+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
namespace DotNetToolkit.Repository.EntityFramework
2+
{
3+
using FetchStrategies;
4+
using System;
5+
using System.Data.Entity;
6+
using System.Linq;
7+
8+
/// <summary>
9+
/// Represents a repository for entity framework.
10+
/// </summary>
11+
public abstract class EfRepositoryBase<TEntity, TKey> : RepositoryBase<TEntity, TKey> where TEntity : class
12+
{
13+
#region Fields
14+
15+
private bool _disposed;
16+
17+
#endregion
18+
19+
#region Properties
20+
21+
/// <summary>
22+
/// Gets the entity set.
23+
/// </summary>
24+
protected IDbSet<TEntity> DbSet { get; private set; }
25+
26+
/// <summary>
27+
/// Gets the database context.
28+
/// </summary>
29+
protected DbContext Context { get; private set; }
30+
31+
#endregion
32+
33+
#region Constructors
34+
35+
/// <summary>
36+
/// Initializes a new instance of the <see cref="EfRepositoryBase{TEntity, TKey}" /> class.
37+
/// </summary>
38+
/// <param name="context">The database context.</param>
39+
protected EfRepositoryBase(DbContext context)
40+
{
41+
if (context == null)
42+
throw new ArgumentNullException(nameof(context));
43+
44+
Context = context;
45+
DbSet = Context.Set<TEntity>();
46+
}
47+
48+
#endregion
49+
50+
#region Protected Methods
51+
52+
/// <summary>
53+
/// Releases unmanaged and - optionally - managed resources.
54+
/// </summary>
55+
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
56+
protected virtual void Dispose(bool disposing)
57+
{
58+
if (_disposed) return;
59+
60+
if (disposing)
61+
{
62+
if (Context != null)
63+
{
64+
Context.Dispose();
65+
}
66+
}
67+
68+
_disposed = true;
69+
}
70+
71+
#endregion
72+
73+
#region Overrides of RepositoryBase<TEntity,TKey>
74+
75+
/// <summary>
76+
/// A protected overridable method for adding the specified <paramref name="entity" /> into the repository.
77+
/// </summary>
78+
protected override void AddItem(TEntity entity)
79+
{
80+
DbSet.Add(entity);
81+
}
82+
83+
/// <summary>
84+
/// A protected overridable method for deleting the specified <paramref name="entity" /> from the repository.
85+
/// </summary>
86+
protected override void DeleteItem(TEntity entity)
87+
{
88+
DbSet.Remove(entity);
89+
}
90+
91+
/// <summary>
92+
/// A protected overridable method for updating the specified <paramref name="entity" /> in the repository.
93+
/// </summary>
94+
protected override void UpdateItem(TEntity entity)
95+
{
96+
Context.Entry(entity).State = EntityState.Modified;
97+
}
98+
99+
/// <summary>
100+
/// A protected overridable method for saving changes made in the current unit of work in the repository.
101+
/// </summary>
102+
protected override void SaveChanges()
103+
{
104+
Context.SaveChanges();
105+
}
106+
107+
/// <summary>
108+
/// A protected overridable method for getting an entity query that supplies the specified fetching strategy from the repository.
109+
/// </summary>
110+
protected override IQueryable<TEntity> GetQuery(IFetchStrategy<TEntity> fetchStrategy = null)
111+
{
112+
var query = DbSet.AsQueryable();
113+
return fetchStrategy == null ? query : fetchStrategy.IncludePaths.Aggregate(query, (current, path) => current.Include(path));
114+
}
115+
116+
/// <summary>
117+
/// Gets an entity query with the given primary key value from the repository.
118+
/// </summary>
119+
protected override TEntity GetQuery(TKey key, IFetchStrategy<TEntity> fetchStrategy)
120+
{
121+
return fetchStrategy == null ? DbSet.Find(key) : base.GetQuery(key, fetchStrategy);
122+
}
123+
124+
/// <summary>
125+
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
126+
/// </summary>
127+
public override void Dispose()
128+
{
129+
Dispose(true);
130+
GC.SuppressFinalize(this);
131+
}
132+
133+
#endregion
134+
}
135+
}

src/DotNetToolkit.Repository/IRepository.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
namespace DotNetToolkit.Repository
22
{
3+
using System;
34
using Traits;
45

56
/// <summary>
67
/// Represents a repository.
78
/// </summary>
89
/// <typeparam name="TEntity">The type of the entity.</typeparam>
910
/// <typeparam name="TKey">The type of the primary key.</typeparam>
10-
public interface IRepository<TEntity, in TKey> : ICanAggregate<TEntity>, ICanAdd<TEntity>, ICanUpdate<TEntity>, ICanDelete<TEntity, TKey>, ICanGet<TEntity, TKey>, ICanFind<TEntity>
11+
public interface IRepository<TEntity, in TKey> : ICanAggregate<TEntity>, ICanAdd<TEntity>, ICanUpdate<TEntity>, ICanDelete<TEntity, TKey>, ICanGet<TEntity, TKey>, ICanFind<TEntity>, IDisposable
1112
where TEntity : class
1213
{
1314
}

src/DotNetToolkit.Repository/RepositoryBase.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313
/// </summary>
1414
public abstract class RepositoryBase<TEntity, TKey> : IRepository<TEntity, TKey> where TEntity : class
1515
{
16+
#region Public Methods
17+
18+
/// <summary>
19+
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
20+
/// </summary>
21+
public abstract void Dispose();
22+
23+
#endregion
24+
1625
#region Protected Methods
1726

1827
/// <summary>
@@ -125,7 +134,7 @@ protected virtual ISpecification<TEntity> ByPrimaryKeySpecification(TKey key, IF
125134
/// <returns>The number of entities contained in the repository.</returns>
126135
public int Count()
127136
{
128-
return Count((ISpecification<TEntity>)null);
137+
return GetQuery().Count();
129138
}
130139

131140
/// <summary>
@@ -307,9 +316,6 @@ public TEntity Get(TKey key, IFetchStrategy<TEntity> fetchStrategy)
307316
if (key == null)
308317
throw new ArgumentNullException(nameof(key));
309318

310-
if (fetchStrategy == null)
311-
throw new ArgumentNullException(nameof(fetchStrategy));
312-
313319
return GetQuery(key, fetchStrategy);
314320
}
315321

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<configSections>
4+
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
5+
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
6+
</configSections>
7+
<entityFramework>
8+
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
9+
<parameters>
10+
<parameter value="System.Data.SqlServerCe.4.0" />
11+
</parameters>
12+
</defaultConnectionFactory>
13+
<providers>
14+
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
15+
<provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
16+
</providers>
17+
</entityFramework>
18+
<system.data>
19+
<DbProviderFactories>
20+
<remove invariant="System.Data.SqlServerCe.4.0" />
21+
<add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
22+
</DbProviderFactories>
23+
</system.data>
24+
<connectionStrings>
25+
<add name=" DefaultConnection" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=|DataDirectory|MyDatabase.sdf" />
26+
</connectionStrings>
27+
</configuration>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace DotNetToolkit.Repository.Integration.Test.Data
2+
{
3+
using System.Data.Common;
4+
using System.Data.Entity;
5+
6+
public class TestDbContext : DbContext
7+
{
8+
public DbSet<Customer> TestCustomerEntities { get; set; }
9+
10+
public TestDbContext(DbConnection connection) : base(connection, true)
11+
{
12+
}
13+
}
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace DotNetToolkit.Repository.Integration.Test.Data
2+
{
3+
using System.Data.Common;
4+
using System.IO;
5+
6+
public static class TestDbContextFactory
7+
{
8+
public static TestDbContext Create()
9+
{
10+
var currentFile = Path.GetTempFileName();
11+
var connectionString = $"Data Source={currentFile};Persist Security Info=False";
12+
var conn = DbProviderFactories.GetFactory("System.Data.SqlServerCe.4.0").CreateConnection();
13+
14+
conn.ConnectionString = connectionString;
15+
conn.Open();
16+
17+
var context = new TestDbContext(conn);
18+
19+
context.Database.CreateIfNotExists();
20+
21+
return context;
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)