Skip to content

Commit

Permalink
fix and support disconnected entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Mizel committed Dec 14, 2016
1 parent 203ea1d commit d153b91
Show file tree
Hide file tree
Showing 15 changed files with 184 additions and 72 deletions.
6 changes: 3 additions & 3 deletions DemoConsole/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
</configuration>
</configuration>
5 changes: 3 additions & 2 deletions DemoConsole/DemoConsole.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand All @@ -9,8 +9,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DemoConsole</RootNamespace>
<AssemblyName>DemoConsole</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down
10 changes: 5 additions & 5 deletions DevMentor.Context.MongoDb/App.config
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
<parameter value="mssqllocaldb"/>
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup></configuration>
5 changes: 3 additions & 2 deletions DevMentor.Context.MongoDb/DevMentor.Context.MongoDb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DevMentor.Context.MongoDb</RootNamespace>
<AssemblyName>DevMentor.Context.MongoDb</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -69,7 +70,7 @@
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
<None Include="Properties\packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DevMentor.Context\DevMentor.Context.csproj">
Expand Down
7 changes: 0 additions & 7 deletions DevMentor.Context.MongoDb/packages.config

This file was deleted.

3 changes: 2 additions & 1 deletion DevMentor.Context.Test/DevMentor.Context.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DevMentor.Context.Test</RootNamespace>
<AssemblyName>DevMentor.Context.Test</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
46 changes: 39 additions & 7 deletions DevMentor.Context.Test/XmlStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ namespace DevMentor.Context.Test
[TestClass]
public class XmlStoreTests
{
IStoreStrategy store=new XmlStoreStrategy();
IStoreStrategy store = new XmlStoreStrategy();
[TestMethod]
public void XmlStoreInsertTest()
{
var user=new Data.Entities.User
var user = new Data.Entities.User
{
LastName = "Insert",
FirstName = "FirstName "+DateTime.Now.ToString()
FirstName = "FirstName " + DateTime.Now.ToString()
};


Expand All @@ -29,7 +29,7 @@ public void XmlStoreInsertTest()
unit.Save();

unit = new UnitOfWork(store);
var datauser=unit.UserRepository.GetByID(user.Id);
var datauser = unit.UserRepository.GetByID(user.Id);

Assert.IsNotNull(datauser);
Assert.AreEqual(datauser.FirstName, user.FirstName);
Expand Down Expand Up @@ -59,7 +59,7 @@ public void XmlStoreDeleteTest()
var datauser = unit.UserRepository.GetByID(user.Id);

Assert.IsNull(datauser);

}


Expand All @@ -68,7 +68,7 @@ public void XmlStoreUpdateTest()
{
var user = new Data.Entities.User
{
LastName ="Update",
LastName = "Update",
FirstName = "FirstName " + DateTime.Now.ToString()
};

Expand All @@ -95,7 +95,39 @@ public void XmlStoreUpdateTest()
unit.UserRepository.Delete(user);
unit.Save();
}

[TestMethod]
public void XmlStoreUpdateDisconnectedTest()
{
var expected = "UpdatedLastname";
var id = Guid.NewGuid();
var user = new Data.Entities.User
{
Id = id,
LastName = "New",
FirstName = "FirstName " + DateTime.Now.ToString()
};

UnitOfWork unit = new UnitOfWork(store);
unit.UserRepository.Insert(user);
unit.Save();

user = new Data.Entities.User
{
Id = id,
LastName = expected,
FirstName = "FirstName " + DateTime.Now.ToString()
};

unit.UserRepository.Update(user);
unit.Save();

user = unit.UserRepository.GetByID(id);

Assert.IsNotNull(user);
Assert.AreEqual(user.LastName, expected);
}
}


}
10 changes: 5 additions & 5 deletions DevMentor.Context/App.config
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup></configuration>
3 changes: 2 additions & 1 deletion DevMentor.Context/DevMentor.Context.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DevMentor.Context</RootNamespace>
<AssemblyName>DevMentor.Context</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
77 changes: 74 additions & 3 deletions DevMentor.Context/FileSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Web;

namespace DevMentor.Context
Expand All @@ -19,9 +20,9 @@ public FileSet()
Local = new ObservableCollection<TEntity>();
LocalDeleted = new ObservableCollection<TEntity>();
LocalUpdated = new ObservableCollection<TEntity>();

}


//[SuppressMessage("Microsoft.Usage", "CA2225:OperatorOverloadsHaveNamedAlternates", Justification = "Intentionally just implicit to reduce API clutter.")]
//public static implicit operator FileSet(FileSet<TEntity> entry);
Expand Down Expand Up @@ -72,7 +73,7 @@ public TEntity Find(params object[] ids)
if (ids.Length != 1)
throw new ArgumentException("FileSet support only one id as parameter");

object id = ids[0];
object id = ids[0]; //looks like error ;-)

var type = typeof(TEntity);
var name = type.Name;
Expand Down Expand Up @@ -117,9 +118,79 @@ public TEntity Attach(TEntity item)
method.Invoke(item, new object[] { true });

LocalUpdated.Add(item);

object id = GetId(item);

var firstOrDefault = Local.FirstOrDefault(i => Comparer.Default.Compare(GetId(i), id) == 0);
if (firstOrDefault != null)
{ //map => update
CopyObjectData(item, firstOrDefault);
}
else {
//insert
Local.Add(item);
}


return item;
}

private object GetId(TEntity item)
{
var type = typeof(TEntity);
var name = type.Name;
var idPropInfo = type.GetProperty(type.Name + "Id");
if (idPropInfo == null)
idPropInfo = type.GetProperty("Id");
if (idPropInfo == null)
throw new ArgumentException("Type " + name + " don't have any Id name");

return idPropInfo.GetValue(item);
}

private void CopyObjectData(object source, object target)
{
string excludedProperties = null;
BindingFlags memberAccess = BindingFlags.Instance | BindingFlags.Public;
string[] excluded = null;
if (!string.IsNullOrEmpty(excludedProperties))
excluded = excludedProperties.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);

MemberInfo[] miT = target.GetType().GetMembers(memberAccess);
foreach (MemberInfo Field in miT)
{
string name = Field.Name;

// Skip over any property exceptions
if (!string.IsNullOrEmpty(excludedProperties) &&
excluded.Contains(name))
continue;

if (Field.MemberType == MemberTypes.Field)
{
FieldInfo SourceField = source.GetType().GetField(name);
if (SourceField == null)
continue;

object SourceValue = SourceField.GetValue(source);
((FieldInfo)Field).SetValue(target, SourceValue);
}
else if (Field.MemberType == MemberTypes.Property)
{
PropertyInfo piTarget = Field as PropertyInfo;
PropertyInfo SourceField = source.GetType().GetProperty(name, memberAccess);
if (SourceField == null)
continue;

if (piTarget.CanWrite && SourceField.CanRead)
{
object SourceValue = SourceField.GetValue(source, null);
piTarget.SetValue(target, SourceValue, null);
}
}
}
}

public TEntity Create()
{
return default(TEntity);
Expand Down
2 changes: 1 addition & 1 deletion DevMentor.Context/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.1" targetFramework="net45" />
<package id="EntityFramework" version="6.1.3" targetFramework="net45" />
<package id="MongoDB.Bson" version="2.2.3" targetFramework="net45" />
<package id="MongoDB.Driver" version="2.2.3" targetFramework="net45" />
<package id="MongoDB.Driver.Core" version="2.2.3" targetFramework="net45" />
Expand Down
10 changes: 5 additions & 5 deletions DevMentor.Data/App.config
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup></configuration>
5 changes: 3 additions & 2 deletions DevMentor.Data/DevMentor.Data.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand All @@ -9,8 +9,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DevMentor.Data</RootNamespace>
<AssemblyName>DevMentor.Data</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
Loading

0 comments on commit d153b91

Please sign in to comment.