Skip to content

Commit 799f7b0

Browse files
committed
Added Ef5Repository which has an EF5 rather than EF4 dependency. Code is essentially duplicated from EfRepository and there are no tests in place yet. Needed it quickly -- so I pooped it out.
1 parent 0782736 commit 799f7b0

25 files changed

Lines changed: 37486 additions & 0 deletions
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
6+
</configSections>
7+
<entityFramework>
8+
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
9+
</entityFramework>
10+
</configuration>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Data.Entity;
3+
using SharpRepository.Repository.Caching;
4+
5+
namespace SharpRepository.Ef5Repository
6+
{
7+
/// <summary>
8+
/// Entity Framework repository layer
9+
/// </summary>
10+
/// <typeparam name="T">The Entity type</typeparam>
11+
/// <typeparam name="TKey">The type of the primary key.</typeparam>
12+
public class Ef5Repository<T, TKey> : Ef5RepositoryBase<T, TKey> where T : class, new()
13+
{
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="EfRepository&lt;T, TKey&gt;"/> class.
16+
/// </summary>
17+
/// <param name="dbContext">The Entity Framework DbContext.</param>
18+
/// <param name="cachingStrategy">The caching strategy to use. Defaults to <see cref="NoCachingStrategy&lt;T, TKey&gt;" /></param>
19+
public Ef5Repository(DbContext dbContext, ICachingStrategy<T, TKey> cachingStrategy = null) : base(dbContext, cachingStrategy)
20+
{
21+
if (dbContext == null) throw new ArgumentNullException("dbContext");
22+
}
23+
}
24+
25+
/// <summary>
26+
/// Entity Framework repository layer
27+
/// </summary>
28+
/// <typeparam name="T">The Entity type</typeparam>
29+
public class Ef5Repository<T> : Ef5RepositoryBase<T, int> where T : class, new()
30+
{
31+
/// <summary>
32+
/// Initializes a new instance of the <see cref="EfRepository&lt;T&gt;"/> class.
33+
/// </summary>
34+
/// <param name="dbContext">The Entity Framework DbContext.</param>
35+
/// <param name="cachingStrategy">The caching strategy to use. Defaults to <see cref="NoCachingStrategy&lt;T, TKey&gt;" /></param>
36+
public Ef5Repository(DbContext dbContext, ICachingStrategy<T, int> cachingStrategy = null) : base(dbContext, cachingStrategy)
37+
{
38+
if (dbContext == null) throw new ArgumentNullException("dbContext");
39+
}
40+
}
41+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System;
2+
using System.Data;
3+
using System.Data.Entity;
4+
using System.Linq;
5+
using SharpRepository.Repository;
6+
using SharpRepository.Repository.Caching;
7+
using SharpRepository.Repository.FetchStrategies;
8+
9+
namespace SharpRepository.Ef5Repository
10+
{
11+
public class Ef5RepositoryBase<T, TKey> : LinqRepositoryBase<T, TKey> where T : class, new()
12+
{
13+
protected IDbSet<T> DbSet { get; private set; }
14+
protected DbContext Context { get; private set; }
15+
16+
internal Ef5RepositoryBase(DbContext dbContext, ICachingStrategy<T, TKey> cachingStrategy = null) : base(cachingStrategy)
17+
{
18+
Initialize(dbContext);
19+
}
20+
21+
private void Initialize(DbContext dbContext)
22+
{
23+
Context = dbContext;
24+
DbSet = Context.Set<T>();
25+
}
26+
27+
protected override void AddItem(T entity)
28+
{
29+
if (typeof(TKey) == typeof(Guid) || typeof(TKey) == typeof(string))
30+
{
31+
TKey id;
32+
if (GetPrimaryKey(entity, out id) && Equals(id, default(TKey)))
33+
{
34+
id = GeneratePrimaryKey();
35+
SetPrimaryKey(entity, id);
36+
}
37+
}
38+
DbSet.Add(entity);
39+
}
40+
41+
protected override void DeleteItem(T entity)
42+
{
43+
DbSet.Remove(entity);
44+
}
45+
46+
protected override void UpdateItem(T entity)
47+
{
48+
// mark this entity as modified, in case it is not currently attached to this context
49+
Context.Entry(entity).State = EntityState.Modified;
50+
}
51+
52+
protected override void SaveChanges()
53+
{
54+
Context.SaveChanges();
55+
}
56+
57+
protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy)
58+
{
59+
var query = DbSet.AsQueryable();
60+
return fetchStrategy == null ? query : fetchStrategy.IncludePaths.Aggregate(query, (current, path) => current.Include(path));
61+
}
62+
63+
// we override the implementation fro LinqBaseRepository becausee this is built in and doesn't need to find the key column and do dynamic expressions, etc.
64+
protected override T GetQuery(TKey key)
65+
{
66+
return DbSet.Find(key);
67+
}
68+
69+
// TODO: use logic like this to override GetPrimaryKey
70+
// below is using the older EF stuff and it doesn't translate exactly
71+
//private EntityKey GetEntityKey(object keyValue)
72+
//{
73+
// var entitySetName = GetEntityName();
74+
// var keyPropertyName = _dbSet.EntitySet.ElementType.KeyMembers[0].ToString();
75+
// return new EntityKey(entitySetName, new[] { new EntityKeyMember(keyPropertyName, keyValue) });
76+
77+
//}
78+
79+
//private string GetEntityName()
80+
//{
81+
// return string.Format("{0}.{1}", Context.DefaultContainerName, QueryBase.EntitySet.Name);
82+
//}
83+
84+
public override void Dispose()
85+
{
86+
Dispose(true);
87+
GC.SuppressFinalize(this);
88+
}
89+
90+
protected virtual void Dispose(bool disposing)
91+
{
92+
if (!disposing) return;
93+
if (Context == null) return;
94+
95+
Context.Dispose();
96+
Context = null;
97+
}
98+
99+
private TKey GeneratePrimaryKey()
100+
{
101+
if (typeof(TKey) == typeof(Guid))
102+
{
103+
return (TKey)Convert.ChangeType(Guid.NewGuid(), typeof(TKey));
104+
}
105+
106+
if (typeof(TKey) == typeof(string))
107+
{
108+
return (TKey)Convert.ChangeType(Guid.NewGuid().ToString(), typeof(TKey));
109+
}
110+
111+
throw new InvalidOperationException("Primary key could not be generated. This only works for GUID, Int32 and String.");
112+
}
113+
}
114+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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("SharpRepository.Ef5Repository")]
9+
[assembly: AssemblyDescription("")]
10+
11+
// The following GUID is for the ID of the typelib if this project is exposed to COM
12+
[assembly: Guid("416e6168-5049-42df-88f8-d59c58fac34b")]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{7ACC7E0F-2EB9-45E1-8841-A00A40BCF0B5}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>SharpRepository.Ef5Repository</RootNamespace>
12+
<AssemblyName>SharpRepository.Ef5Repository</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
35+
<SpecificVersion>False</SpecificVersion>
36+
<HintPath>..\packages\EntityFramework.5.0.0\lib\net45\EntityFramework.dll</HintPath>
37+
</Reference>
38+
<Reference Include="System" />
39+
<Reference Include="System.ComponentModel.DataAnnotations" />
40+
<Reference Include="System.Core" />
41+
<Reference Include="System.Data.Entity" />
42+
<Reference Include="System.Xml.Linq" />
43+
<Reference Include="System.Data.DataSetExtensions" />
44+
<Reference Include="Microsoft.CSharp" />
45+
<Reference Include="System.Data" />
46+
<Reference Include="System.Xml" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="..\CommonAssembly.cs">
50+
<Link>Properties\CommonAssembly.cs</Link>
51+
</Compile>
52+
<Compile Include="Ef5Repository.cs" />
53+
<Compile Include="Ef5RepositoryBase.cs" />
54+
<Compile Include="Properties\AssemblyInfo.cs" />
55+
</ItemGroup>
56+
<ItemGroup>
57+
<None Include="App.config" />
58+
<None Include="packages.config" />
59+
</ItemGroup>
60+
<ItemGroup>
61+
<ProjectReference Include="..\SharpRepository.Repository\SharpRepository.Repository.csproj">
62+
<Project>{710DEE79-25CE-4F68-B8B1-D08A135AD154}</Project>
63+
<Name>SharpRepository.Repository</Name>
64+
</ProjectReference>
65+
</ItemGroup>
66+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
67+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
68+
Other similar extension points exist, see Microsoft.Common.targets.
69+
<Target Name="BeforeBuild">
70+
</Target>
71+
<Target Name="AfterBuild">
72+
</Target>
73+
-->
74+
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
4+
</packages>

SharpRepository.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpRepository.Db4oReposit
3434
EndProject
3535
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpRepository.MongoDbRepository", "SharpRepository.MongoDbRepository\SharpRepository.MongoDbRepository.csproj", "{DC40FEBE-2E39-4CB8-AE11-DFE9F6865BD2}"
3636
EndProject
37+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpRepository.Ef5Repository", "SharpRepository.Ef5Repository\SharpRepository.Ef5Repository.csproj", "{7ACC7E0F-2EB9-45E1-8841-A00A40BCF0B5}"
38+
EndProject
3739
Global
3840
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3941
Debug|Any CPU = Debug|Any CPU
@@ -80,6 +82,10 @@ Global
8082
{DC40FEBE-2E39-4CB8-AE11-DFE9F6865BD2}.Debug|Any CPU.Build.0 = Debug|Any CPU
8183
{DC40FEBE-2E39-4CB8-AE11-DFE9F6865BD2}.Release|Any CPU.ActiveCfg = Release|Any CPU
8284
{DC40FEBE-2E39-4CB8-AE11-DFE9F6865BD2}.Release|Any CPU.Build.0 = Release|Any CPU
85+
{7ACC7E0F-2EB9-45E1-8841-A00A40BCF0B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
86+
{7ACC7E0F-2EB9-45E1-8841-A00A40BCF0B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
87+
{7ACC7E0F-2EB9-45E1-8841-A00A40BCF0B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
88+
{7ACC7E0F-2EB9-45E1-8841-A00A40BCF0B5}.Release|Any CPU.Build.0 = Release|Any CPU
8389
EndGlobalSection
8490
GlobalSection(SolutionProperties) = preSolution
8591
HideSolutionNode = FALSE
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<configuration>
2+
<configSections>
3+
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
4+
</configSections>
5+
</configuration>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<configuration>
2+
<configSections>
3+
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
4+
</configSections>
5+
</configuration>
1.06 MB
Binary file not shown.

0 commit comments

Comments
 (0)