Skip to content

WIP: .NET Example Code project & first of dotnet doc updates #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
405 changes: 405 additions & 0 deletions examples/dotnet/.gitignore

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions examples/dotnet/Examples/Examples.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="Realm.Fody" Version="5.0.1-PR-2041-31">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Realm" Version="5.0.1-PR-2041-31" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\dotnet\dotnet.csproj" />
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions examples/dotnet/Examples/FodyWeavers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Realm />
</Weavers>
28 changes: 28 additions & 0 deletions examples/dotnet/Examples/FodyWeavers.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. -->
<xs:element name="Weavers">
<xs:complexType>
<xs:all>
<xs:element name="Realm" minOccurs="0" maxOccurs="1">
<xs:complexType></xs:complexType>
</xs:element>
</xs:all>
<xs:attribute name="VerifyAssembly" type="xs:boolean">
<xs:annotation>
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="VerifyIgnoreCodes" type="xs:string">
<xs:annotation>
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="GenerateXsd" type="xs:boolean">
<xs:annotation>
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
17 changes: 17 additions & 0 deletions examples/dotnet/Examples/Project.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using MongoDB.Bson;
using Realms;

namespace dotnet
{
public class Project : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; }
[MapTo("_partition")]
public string Partition { get; set; }
[MapTo("name")]
[Required]
public string Name { get; set; }
}
}
105 changes: 105 additions & 0 deletions examples/dotnet/Examples/RealmTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System.Linq;
using dotnet;
using MongoDB.Bson;
using NUnit.Framework;
using Realms;
using Realms.Sync;

namespace UnitTests
{
public class RealmTests
{
Realms.Sync.App app;
ObjectId testTaskId;
Realms.Sync.User user;
SyncConfiguration config;
const string myRealmAppId = "tuts-tijya";

[SetUp]
public async System.Threading.Tasks.Task Setup()
{
// :code-block-start: initialize-realm
app = Realms.Sync.App.Create(myRealmAppId);
// :code-block-end:
user = app.LogInAsync(Credentials.EmailPassword("[email protected]", "foobar")).Result;
config = new SyncConfiguration("My Project", user);
Realm realm = await Realm.GetInstanceAsync(config);
// :code-block-start: create
Task testTask = new Task()
{
Name = "Do this thing",
Status = TaskStatus.Open.ToString()
};

realm.Write(() =>
{
realm.Add(testTask);
});
// :code-block-end:
testTaskId = testTask.Id;
return;
}

[Test]
public async System.Threading.Tasks.Task GetsSyncedTasks()
{
// :code-block-start: anon-login
Realms.Sync.User user = app.LogInAsync(Credentials.Anonymous()).Result;
// :code-block-end:
// :code-block-start: config
config = new SyncConfiguration("My Project", user);
Realm realm = await Realm.GetInstanceAsync(config);
// :code-block-end:
// :code-block-start: read-all
var tasks = realm.All<Task>().ToList();
// :code-block-end:
Assert.AreEqual(1, tasks.Count);
// :code-block-start: read-some
tasks = realm.All<Task>().Where(t=>t.Status == "Open").ToList();
// :code-block-end:
Assert.AreEqual(1, tasks.Count);
return;
}

[Test]
public async System.Threading.Tasks.Task ModifiesATask()
{
config = new SyncConfiguration("My Project", user);
Realm realm = await Realm.GetInstanceAsync(config);
// :code-block-start: modify
Task t = realm.All<Task>()
.Where(t => t.Id == testTaskId)
.FirstOrDefault();

realm.Write(() =>
{
t.Status = TaskStatus.InProgress.ToString();
});

// :code-block-end:
var allTasks = realm.All<Task>().ToList();
Assert.AreEqual(1, allTasks.Count);
Assert.AreEqual(TaskStatus.InProgress.ToString(), allTasks.First().Status);

return;
}


[TearDown]
public async System.Threading.Tasks.Task TearDown()
{
config = new SyncConfiguration("My Project", user);
Realm realm = await Realm.GetInstanceAsync(config);
// :code-block-start: delete
realm.Write(() =>
{
realm.RemoveAll<Task>();
});
// :code-block-end:
// :code-block-start: logout
await user.LogOutAsync();
// :code-block-end:
return;
}
}
}
39 changes: 39 additions & 0 deletions examples/dotnet/Examples/Task.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// :code-block-start:task-object-model
using MongoDB.Bson;
using Realms;
namespace dotnet
{
public class Task : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; }

[MapTo("_partition")]
public string Partition { get; set; }

[MapTo("assignee")]
public User Assignee { get; set; }

[MapTo("name")]
[Required]
public string Name { get; set; }

[MapTo("status")]
[Required]
public string Status { get; set; }

public Task()
{
this.Id = ObjectId.GenerateNewId();
}
}

public enum TaskStatus
{
Open,
InProgress,
Complete
}
}
// :code-block-end:
20 changes: 20 additions & 0 deletions examples/dotnet/Examples/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using MongoDB.Bson;
using Realms;

namespace dotnet
{
public class User : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
[Required]
public string Id { get; set; }
[MapTo("_partition")]
public string Partition { get; set; }
[MapTo("image")]
public string Image { get; set; }
[MapTo("name")]
[Required]
public string Name { get; set; }
}
}
29 changes: 29 additions & 0 deletions examples/dotnet/dotnet.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples", "Examples\Examples.csproj", "{C97249A2-33A1-457A-AAEC-389B62984885}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Debug|iPhoneSimulator = Debug|iPhoneSimulator
Release|iPhoneSimulator = Release|iPhoneSimulator
Debug|iPhone = Debug|iPhone
Release|iPhone = Release|iPhone
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|Any CPU.Build.0 = Release|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|iPhone.Build.0 = Debug|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|iPhone.ActiveCfg = Release|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|iPhone.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Loading