Skip to content

Commit fe7be96

Browse files
author
Jeff Treuting
committed
DynamicProxy integration test
Apparently the DynamicProxies for lazy loading don't happen in SqlServer CE it seems. So I created a testing regular SQL database to run some tests against. I added a SQL script to use to populate the databse. Just create a local SharpRepositoryTest database and run the script against it. Got test passing. Using caching and getting an already cached item out and updating and saving it. Not throwing an exception now because I'm caching the POCO class and not the Dynamic Proxy. I also removed some old EF bits and the old EfRepository.
1 parent 2d043f5 commit fe7be96

44 files changed

Lines changed: 2167 additions & 31648 deletions

Some content is hidden

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

SharpRepository.Ef5Repository/App.config

Lines changed: 0 additions & 10 deletions
This file was deleted.

SharpRepository.Ef5Repository/DynamicProxyCloneInjection.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
4+
using System.Linq;
35
using Omu.ValueInjecter;
46

57
namespace SharpRepository.Ef5Repository
@@ -8,6 +10,8 @@ namespace SharpRepository.Ef5Repository
810
// maybe we have a default max and allow it to be changed via config files
911
public class DynamicProxyCloneInjection : LoopValueInjection
1012
{
13+
private const string DynamicProxyNamespace = "System.Data.Entity.DynamicProxies";
14+
1115
// here we store the DyamicProxy classes that we've seen already going down the tree
1216
// that way we can check to see if we've already visited this object and if so it's a circular reference so we can ignore the 2nd time we see it
1317
private readonly HashSet<object> _foundProxies;
@@ -48,7 +52,7 @@ protected override object SetValue(object v)
4852
return null;
4953

5054
var type = v.GetType();
51-
if (type.Namespace == "System.Data.Entity.DynamicProxies")
55+
if (type.Namespace == DynamicProxyNamespace)
5256
{
5357
var baseType = type.BaseType;
5458
if (baseType != null)
@@ -58,6 +62,23 @@ protected override object SetValue(object v)
5862
}
5963
}
6064

65+
// let's check for a collection of DynamicProxies, if so we need to clean it up
66+
if (type.Name == "HashSet`1")
67+
{
68+
var genericType = type.GetGenericArguments()[0];
69+
var cleanHashSet = Activator.CreateInstance(type);
70+
var addMethod = type.GetMethod("Add");
71+
72+
foreach (var item in (IEnumerable)v)
73+
{
74+
var tmp = Activator.CreateInstance(genericType).InjectFrom(new DynamicProxyCloneInjection(_maxDepth, _foundProxies, _currentDepth), item);
75+
addMethod.Invoke(cleanHashSet, new object[] {tmp});
76+
//cleanHashSet.Add(tmp);
77+
}
78+
79+
return cleanHashSet;
80+
}
81+
6182
return base.SetValue(v);
6283
}
6384
}

SharpRepository.Ef5Repository/Ef5RepositoryBase.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,16 @@ protected override void DeleteItem(T entity)
5151
protected override void UpdateItem(T entity)
5252
{
5353
// mark this entity as modified, in case it is not currently attached to this context
54-
Context.Entry(entity).State = EntityState.Modified;
54+
try
55+
{
56+
Context.Entry(entity).State = EntityState.Modified;
57+
}
58+
catch (InvalidOperationException ex)
59+
{
60+
// ignore the "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key" error
61+
// it is already being tracked so not need to deal with it further
62+
}
63+
5564
}
5665

5766
protected override void SaveChanges()

SharpRepository.Ef5Repository/SharpRepository.Ef5Repository.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
<Compile Include="Properties\AssemblyInfo.cs" />
6363
</ItemGroup>
6464
<ItemGroup>
65-
<None Include="App.config" />
6665
<None Include="packages.config" />
6766
</ItemGroup>
6867
<ItemGroup>

SharpRepository.EfRepository/App.config

Lines changed: 0 additions & 14 deletions
This file was deleted.

SharpRepository.EfRepository/EfRepository.cs

Lines changed: 0 additions & 42 deletions
This file was deleted.

SharpRepository.EfRepository/EfRepositoryBase.cs

Lines changed: 0 additions & 100 deletions
This file was deleted.

SharpRepository.EfRepository/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

SharpRepository.EfRepository/SharpRepository.EfRepository.csproj

Lines changed: 0 additions & 70 deletions
This file was deleted.

SharpRepository.EfRepository/packages.config

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)