Skip to content

Commit dcb9313

Browse files
committed
Initial commit
1 parent 6bd6448 commit dcb9313

332 files changed

Lines changed: 283504 additions & 0 deletions

File tree

Some content is hidden

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

CommonAssembly.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
4+
[assembly: AssemblyCompany("SharpRepository")]
5+
[assembly: AssemblyProduct("SharpRepository")]
6+
[assembly: AssemblyCopyright("Copyright © SharpRepository 2012")]
7+
[assembly: AssemblyTrademark("")]
8+
[assembly: AssemblyCulture("")]
9+
10+
// Setting ComVisible to false makes the types in this assembly not visible
11+
// to COM components. If you need to access a type in this assembly from
12+
// COM, set the ComVisible attribute to true on that type.
13+
[assembly: ComVisible(false)]
14+
15+
// The AssemblyVersion is used by the CLR to bind to strongly named assemblies.
16+
// It is stored in the AssemblyDef manifest metadata table of the built assembly,
17+
// and in the AssemblyRef table of any assembly that references it.
18+
// Take care when changing the AssemblyVersion as it may impact referencing
19+
// assemblies.
20+
[assembly: AssemblyVersion("0.1")]
21+
22+
// The AssemblyFileVersion is intended to uniquely identify a build of the individual assembly
23+
// Ideally we would have the build server update this value. This version number is stored
24+
// in the Win32 version resource and can be seen when viewing the Windows Explorer property
25+
// pages for the assembly. The CLR does not care about nor examine the AssemblyFileVersion.
26+
[assembly: AssemblyFileVersion("0.1.0.0")]
27+
28+
// The AssemblyInformationalVersion is intended to represent the version of your entire product.
29+
// The CLR does not care about nor examine the AssemblyInformationalVersion. You will find this
30+
// value represented as the assembly's product version in Windows Explorer.
31+
[assembly: AssemblyInformationalVersion("0.1.0.0")]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
6+
</configSections>
7+
<entityFramework>
8+
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
9+
<parameters>
10+
<parameter value="Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True" />
11+
</parameters>
12+
</defaultConnectionFactory>
13+
</entityFramework>
14+
</configuration>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Data.Entity;
2+
using SharpRepository.Repository.Caching;
3+
4+
namespace SharpRepository.EfRepository
5+
{
6+
/// <summary>
7+
/// Entity Framework repository layer
8+
/// </summary>
9+
/// <typeparam name="T">The Entity type</typeparam>
10+
/// <typeparam name="TKey">The type of the primary key.</typeparam>
11+
public class EfRepository<T, TKey> : EfRepositoryBase<T, TKey> where T : class, new()
12+
{
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="EfRepository&lt;T, TKey&gt;"/> class.
15+
/// </summary>
16+
/// <param name="dbContext">The Entity Framework DbContext.</param>
17+
/// <param name="cachingStrategy">The caching strategy to use. Defaults to <see cref="NoCachingStrategy&lt;T, TKey&gt;" /></param>
18+
public EfRepository(DbContext dbContext, ICachingStrategy<T, TKey> cachingStrategy = null) : base(dbContext, cachingStrategy)
19+
{
20+
}
21+
}
22+
23+
/// <summary>
24+
/// Entity Framework repository layer
25+
/// </summary>
26+
/// <typeparam name="T">The Entity type</typeparam>
27+
public class EfRepository<T> : EfRepositoryBase<T, int> where T : class, new()
28+
{
29+
/// <summary>
30+
/// Initializes a new instance of the <see cref="EfRepository&lt;T&gt;"/> class.
31+
/// </summary>
32+
/// <param name="dbContext">The Entity Framework DbContext.</param>
33+
/// <param name="cachingStrategy">The caching strategy to use. Defaults to <see cref="NoCachingStrategy&lt;T, TKey&gt;" /></param>
34+
public EfRepository(DbContext dbContext, ICachingStrategy<T, int> cachingStrategy = null) : base(dbContext, cachingStrategy)
35+
{
36+
}
37+
}
38+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.EfRepository
10+
{
11+
public class EfRepositoryBase<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 EfRepositoryBase(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+
DbSet.Add(entity);
30+
}
31+
32+
protected override void DeleteItem(T entity)
33+
{
34+
DbSet.Remove(entity);
35+
}
36+
37+
protected override void UpdateItem(T entity)
38+
{
39+
// mark this entity as modified, in case it is not currently attached to this context
40+
Context.Entry(entity).State = EntityState.Modified;
41+
}
42+
43+
protected override void SaveChanges()
44+
{
45+
Context.SaveChanges();
46+
}
47+
48+
protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy)
49+
{
50+
var query = DbSet.AsQueryable();
51+
return fetchStrategy == null ? query : fetchStrategy.IncludePaths.Aggregate(query, (current, path) => current.Include(path));
52+
}
53+
54+
// 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.
55+
protected override T GetQuery(TKey key)
56+
{
57+
return DbSet.Find(key);
58+
}
59+
60+
// TODO: use logic like this to override GetPrimaryKey
61+
// below is using the older EF stuff and it doesn't translate exactly
62+
//private EntityKey GetEntityKey(object keyValue)
63+
//{
64+
// var entitySetName = GetEntityName();
65+
// var keyPropertyName = _dbSet.EntitySet.ElementType.KeyMembers[0].ToString();
66+
// return new EntityKey(entitySetName, new[] { new EntityKeyMember(keyPropertyName, keyValue) });
67+
68+
//}
69+
70+
//private string GetEntityName()
71+
//{
72+
// return string.Format("{0}.{1}", Context.DefaultContainerName, QueryBase.EntitySet.Name);
73+
//}
74+
75+
public override void Dispose()
76+
{
77+
Dispose(true);
78+
GC.SuppressFinalize(this);
79+
}
80+
81+
protected virtual void Dispose(bool disposing)
82+
{
83+
if (!disposing) return;
84+
if (Context == null) return;
85+
86+
Context.Dispose();
87+
Context = null;
88+
}
89+
}
90+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
4+
// General Information about an assembly is controlled through the following
5+
// set of attributes. Change these attribute values to modify the information
6+
// associated with an assembly.
7+
[assembly: AssemblyTitle("SharpRepository.EFRepository")]
8+
[assembly: AssemblyDescription("")]
9+
10+
// The following GUID is for the ID of the typelib if this project is exposed to COM
11+
[assembly: Guid("73443437-717a-440d-b686-1721d1e87d58")]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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>{DAFB3EB8-A771-4CC5-9375-E56F0DED60D6}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>SharpRepository.EfRepository</RootNamespace>
12+
<AssemblyName>SharpRepository.EfRepository</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">
35+
<HintPath>..\packages\EntityFramework.4.3.1\lib\net40\EntityFramework.dll</HintPath>
36+
</Reference>
37+
<Reference Include="System" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Data.Entity" />
40+
</ItemGroup>
41+
<ItemGroup>
42+
<Compile Include="..\CommonAssembly.cs">
43+
<Link>Properties\CommonAssembly.cs</Link>
44+
</Compile>
45+
<Compile Include="EfRepository.cs" />
46+
<Compile Include="EfRepositoryBase.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<None Include="App.config">
51+
<SubType>Designer</SubType>
52+
</None>
53+
<None Include="packages.config" />
54+
</ItemGroup>
55+
<ItemGroup>
56+
<ProjectReference Include="..\SharpRepository.Repository\SharpRepository.Repository.csproj">
57+
<Project>{710DEE79-25CE-4F68-B8B1-D08A135AD154}</Project>
58+
<Name>SharpRepository.Repository</Name>
59+
</ProjectReference>
60+
</ItemGroup>
61+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
62+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
63+
Other similar extension points exist, see Microsoft.Common.targets.
64+
<Target Name="BeforeBuild">
65+
</Target>
66+
<Target Name="AfterBuild">
67+
</Target>
68+
-->
69+
</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+
<package id="EntityFramework" version="4.3.1" />
4+
</packages>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using SharpRepository.Repository.Caching;
2+
3+
namespace SharpRepository.Repository
4+
{
5+
public class InMemoryRepository<T, TKey> : InMemoryRepositoryBase<T, TKey> where T : class, new()
6+
{
7+
public InMemoryRepository(ICachingStrategy<T, TKey> cachingStrategy = null) : base(cachingStrategy)
8+
{
9+
}
10+
}
11+
12+
public class InMemoryRepository<T> : InMemoryRepositoryBase<T, int> where T : class, new()
13+
{
14+
public InMemoryRepository(ICachingStrategy<T, int> cachingStrategy = null)
15+
: base(cachingStrategy)
16+
{
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)