Skip to content

Commit

Permalink
Merge pull request #31 from sushihangover/NonManagedCopy
Browse files Browse the repository at this point in the history
Add `NonManagedCopy` extensions
  • Loading branch information
sushihangover authored Feb 19, 2017
2 parents 4e461ed + 97c276d commit d09a652
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 24 deletions.
31 changes: 29 additions & 2 deletions src/RealmJson.Extensions/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static class RealmDoesJson
/// <returns></returns>
/// <param name="realm">Realm Instance</param>
/// <param name="jsonString">Json string</param>
/// <param name="inTransaction">bool.</param>
/// <param name="inTransaction">bool.</param>
/// <typeparam name="T">RealmOject-based Class..</typeparam>
public static T CreateObjectFromJson<T>(this Realm realm, string jsonString, bool inTransaction = false) where T : RealmObject
{
Expand Down Expand Up @@ -225,7 +225,7 @@ static void CreateUpdateRecordsViaAutoMapper<T>(Realm realm, PropertyInfo pkProp
// });

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static T CreateObject<T>(Realm realm, T realmObject, bool updateRecord, bool inTransaction ) where T : RealmObject
static T CreateObject<T>(Realm realm, T realmObject, bool updateRecord, bool inTransaction) where T : RealmObject
{
if (inTransaction)
{
Expand Down Expand Up @@ -278,6 +278,33 @@ static RealmObject FindByPKDynamic(Realm realm, Type type, object primaryKeyValu
return realm.Find(type.Name, (string)primaryKeyValue);
}

public static T NonManagedCopy<T>(this RealmObject realmObject) where T : RealmObject
{
var newRecordConfiguration = new MapperConfiguration(cfg =>
{
cfg.CreateMap<T, T>();
});
var newMapper = newRecordConfiguration.CreateMapper();

var realmObjectCopy = (T)Activator.CreateInstance(typeof(T));
newMapper.Map(realmObject, realmObjectCopy);
return realmObjectCopy;
}

public static RealmObject NonManagedCopy(this RealmObject realmObject)
{
var objectType = realmObject.GetType();
var newRecordConfiguration = new MapperConfiguration(cfg =>
{
cfg.CreateMap(objectType, objectType);
});
var newMapper = newRecordConfiguration.CreateMapper();

var realmObjectCopy = Activator.CreateInstance(objectType) as RealmObject;
newMapper.Map(realmObject, realmObjectCopy);
return realmObjectCopy;
}

//void createOrUpdateAllFromJson(Class<E> clazz, InputStream in)
//Tries to update a list of existing objects identified by their primary key with new JSON data.
//<E extends RealmModel>
Expand Down
12 changes: 6 additions & 6 deletions src/RealmJson.Extensions/SushiHangover.RealmJson.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@
<Reference Include="AutoMapper">
<HintPath>..\packages\AutoMapper.5.1.1\lib\portable-net45+win8+wpa81\AutoMapper.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.9.0.2-beta1\lib\portable-net45+wp80+win8+wpa81\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="DotNetCross.Memory.Unsafe">
<HintPath>..\packages\DotNetCross.Memory.Unsafe.0.2.2\lib\DotNetCross.Memory.Unsafe.dll</HintPath>
</Reference>
<Reference Include="Remotion.Linq">
<HintPath>..\packages\Remotion.Linq.2.1.1\lib\portable-net45+win+wpa81+wp80\Remotion.Linq.dll</HintPath>
</Reference>
<Reference Include="Realm">
<HintPath>..\packages\Realm.Database.1.0.3\lib\portable-net45+sl5+wp8+wpa81+win8+monoandroid+Xamarin.iOS10+monotouch+Xamarin.Mac\Realm.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.9.0.2-beta2\lib\portable-net45+wp80+win8+wpa81\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="DotNetCross.Memory.Unsafe">
<HintPath>..\packages\DotNetCross.Memory.Unsafe.0.2.3.4\lib\netstandard1.0\DotNetCross.Memory.Unsafe.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
4 changes: 2 additions & 2 deletions src/RealmJson.Extensions/packages.config
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AutoMapper" version="5.1.1" targetFramework="portable45-net45+win8+wpa81" />
<package id="DotNetCross.Memory.Unsafe" version="0.2.2" targetFramework="portable45-net45+win8+wpa81" />
<package id="DotNetCross.Memory.Unsafe" version="0.2.3.4" targetFramework="portable45-net45+win8+wpa81" />
<package id="Fody" version="1.29.4" targetFramework="portable45-net45+win8+wpa81" developmentDependency="true" />
<package id="Newtonsoft.Json" version="9.0.2-beta1" targetFramework="portable45-net45+win8+wpa81" />
<package id="Newtonsoft.Json" version="9.0.2-beta2" targetFramework="portable45-net45+win8+wpa81" />
<package id="Realm.Database" version="1.0.3" targetFramework="portable45-net45+win8+wpa81" />
<package id="Remotion.Linq" version="2.1.1" targetFramework="portable45-net45+win8+wpa81" />
<package id="System.Collections" version="4.3.0" targetFramework="portable45-net45+win8+wpa81" />
Expand Down
80 changes: 80 additions & 0 deletions src/RealmJson.Shared.Tests/Tests.Shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,86 @@ public void CreateAllFromJson_InvalidStream()
}
}

[Test]
public void NonManagedCopyFromNonManagedGenericType()
{
var obj1 = new State
{
name = "ZZ",
abbreviation = "Z"
};
var obj2 = obj1.NonManagedCopy<State>();
Assert.False(obj2.IsManaged);
// Non-managed objects are never equivalate, this RealmObject.Equals can not be used
Assert.AreEqual(obj1.abbreviation, obj2.abbreviation);
Assert.AreEqual(obj1.name, obj2.name);
}

[Test]
public void NonManagedCopyFromNonManagedRealmObjectType()
{
var obj1 = new State
{
name = "ZZ",
abbreviation = "Z"
};
var obj2 = obj1.NonManagedCopy();
var obj3 = obj2 as State;
// Non-managed objects are never equivalate, this RealmObject.Equals can not be used
Assert.False(obj3.IsManaged);
Assert.AreEqual(obj1.abbreviation, obj3.abbreviation);
Assert.AreEqual(obj1.name, obj3.name);
}

[Test]
public void NonManagedCopyFromManagedGenericType()
{
using (var theRealm = Realm.GetInstance(RealmDBTempPath()))
{
State obj1 = null;
theRealm.Write(() =>
{
var tmp = new State
{
name = "ZZ",
abbreviation = "Z"
};
obj1 = theRealm.Add(tmp);
});
Assert.True(obj1.IsManaged);
var obj2 = obj1.NonManagedCopy<State>();
// Non-managed objects are never equivalate, this RealmObject.Equals can not be used
Assert.False(obj2.IsManaged);
Assert.AreEqual(obj1.abbreviation, obj2.abbreviation);
Assert.AreEqual(obj1.name, obj2.name);
}
}

[Test]
public void NonManagedCopyFromManagedRealmObjectType()
{
using (var theRealm = Realm.GetInstance(RealmDBTempPath()))
{
State obj1 = null;
theRealm.Write(() =>
{
var tmp = new State
{
name = "ZZ",
abbreviation = "Z"
};
obj1 = theRealm.Add(tmp);
});
Assert.True(obj1.IsManaged);
var obj2 = obj1.NonManagedCopy() as State;
// Non-managed objects are never equivalate, this RealmObject.Equals can not be used
Assert.False(obj2.IsManaged);
Assert.AreEqual(obj1.abbreviation, obj2.abbreviation);
Assert.AreEqual(obj1.name, obj2.name);
}
}


#endif

}
Expand Down
18 changes: 9 additions & 9 deletions src/RealmJson.Test.Droid/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
<package id="AutoMapper" version="5.1.1" targetFramework="monoandroid70" />
<package id="DotNetCross.Memory.Unsafe" version="0.2.3.4" targetFramework="monoandroid71" />
<package id="Fody" version="1.29.4" targetFramework="monoandroid70" developmentDependency="true" />
<package id="Microsoft.CSharp" version="4.0.1" targetFramework="monoandroid71" />
<package id="Microsoft.CSharp" version="4.3.0" targetFramework="monoandroid71" />
<package id="Newtonsoft.Json" version="9.0.2-beta2" targetFramework="monoandroid71" />
<package id="Realm.Database" version="1.0.3" targetFramework="monoandroid71" />
<package id="Remotion.Linq" version="2.1.1" targetFramework="monoandroid70" />
<package id="System.Collections" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.ComponentModel.TypeConverter" version="4.1.0" targetFramework="monoandroid71" />
<package id="System.ComponentModel.TypeConverter" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Dynamic.Runtime" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Globalization" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.IO" version="4.1.0" targetFramework="monoandroid71" />
<package id="System.IO" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Linq" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Linq.Expressions" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Linq.Queryable" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.ObjectModel" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Reflection" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Reflection.Extensions" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Resources.ResourceManager" version="4.0.1" targetFramework="monoandroid71" />
<package id="System.Runtime" version="4.1.0" targetFramework="monoandroid70" />
<package id="System.Runtime.Extensions" version="4.1.0" targetFramework="monoandroid70" />
<package id="System.Runtime.Numerics" version="4.0.1" targetFramework="monoandroid71" />
<package id="System.Runtime.Serialization.Primitives" version="4.1.1" targetFramework="monoandroid71" />
<package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Runtime" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Runtime.Extensions" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Runtime.Numerics" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Runtime.Serialization.Primitives" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Text.Encoding" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Text.Encoding.Extensions" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Text.RegularExpressions" version="4.1.0" targetFramework="monoandroid71" />
<package id="System.Text.RegularExpressions" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Threading" version="4.3.0" targetFramework="monoandroid70" />
<package id="System.Threading.Tasks" version="4.3.0" targetFramework="monoandroid71" />
<package id="System.Xml.ReaderWriter" version="4.3.0" targetFramework="monoandroid71" />
Expand Down
6 changes: 3 additions & 3 deletions src/RealmJson.Test.iOS/RealmJson.Test.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@
<Reference Include="AutoMapper">
<HintPath>..\packages\AutoMapper.5.1.1\lib\netstandard1.3\AutoMapper.dll</HintPath>
</Reference>
<Reference Include="DotNetCross.Memory.Unsafe">
<HintPath>..\packages\DotNetCross.Memory.Unsafe.0.2.2\lib\dotnet\DotNetCross.Memory.Unsafe.dll</HintPath>
</Reference>
<Reference Include="Remotion.Linq">
<HintPath>..\packages\Remotion.Linq.2.1.1\lib\portable-net45+win+wpa81+wp80\Remotion.Linq.dll</HintPath>
</Reference>
Expand All @@ -109,6 +106,9 @@
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.9.0.2-beta2\lib\netstandard1.1\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="DotNetCross.Memory.Unsafe">
<HintPath>..\packages\DotNetCross.Memory.Unsafe.0.2.3.4\lib\netstandard1.0\DotNetCross.Memory.Unsafe.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<InterfaceDefinition Include="LaunchScreen.storyboard" />
Expand Down
4 changes: 2 additions & 2 deletions src/RealmJson.Test.iOS/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AutoMapper" version="5.1.1" targetFramework="xamarinios10" />
<package id="DotNetCross.Memory.Unsafe" version="0.2.2" targetFramework="xamarinios10" />
<package id="DotNetCross.Memory.Unsafe" version="0.2.3.4" targetFramework="xamarinios10" />
<package id="Fody" version="1.29.4" targetFramework="xamarinios10" developmentDependency="true" />
<package id="Microsoft.CSharp" version="4.3.0" targetFramework="xamarinios10" />
<package id="Newtonsoft.Json" version="9.0.2-beta2" targetFramework="xamarinios10" />
Expand All @@ -23,7 +23,7 @@
<package id="System.Runtime" version="4.3.0" targetFramework="xamarinios10" />
<package id="System.Runtime.Extensions" version="4.3.0" targetFramework="xamarinios10" />
<package id="System.Runtime.Numerics" version="4.3.0" targetFramework="xamarinios10" />
<package id="System.Runtime.Serialization.Primitives" version="4.1.1" targetFramework="xamarinios10" />
<package id="System.Runtime.Serialization.Primitives" version="4.3.0" targetFramework="xamarinios10" />
<package id="System.Text.Encoding" version="4.3.0" targetFramework="xamarinios10" />
<package id="System.Text.Encoding.Extensions" version="4.3.0" targetFramework="xamarinios10" />
<package id="System.Text.RegularExpressions" version="4.3.0" targetFramework="xamarinios10" />
Expand Down

0 comments on commit d09a652

Please sign in to comment.