Skip to content

Commit ae39152

Browse files
authored
WIP: .NET Example Code project & first of dotnet doc updates (#501)
* WIP: .NET Example Code project & first of dotnet doc updates * rename, remove extras, update literalincludes
1 parent 9770aef commit ae39152

34 files changed

+1222
-0
lines changed

examples/dotnet/.gitignore

Lines changed: 405 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="nunit" Version="3.12.0" />
11+
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
13+
<PackageReference Include="Realm.Fody" Version="5.0.1-PR-2041-31">
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
<PrivateAssets>all</PrivateAssets>
16+
</PackageReference>
17+
<PackageReference Include="Realm" Version="5.0.1-PR-2041-31" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\dotnet\dotnet.csproj" />
22+
</ItemGroup>
23+
</Project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
2+
<Realm />
3+
</Weavers>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3+
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. -->
4+
<xs:element name="Weavers">
5+
<xs:complexType>
6+
<xs:all>
7+
<xs:element name="Realm" minOccurs="0" maxOccurs="1">
8+
<xs:complexType></xs:complexType>
9+
</xs:element>
10+
</xs:all>
11+
<xs:attribute name="VerifyAssembly" type="xs:boolean">
12+
<xs:annotation>
13+
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation>
14+
</xs:annotation>
15+
</xs:attribute>
16+
<xs:attribute name="VerifyIgnoreCodes" type="xs:string">
17+
<xs:annotation>
18+
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation>
19+
</xs:annotation>
20+
</xs:attribute>
21+
<xs:attribute name="GenerateXsd" type="xs:boolean">
22+
<xs:annotation>
23+
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation>
24+
</xs:annotation>
25+
</xs:attribute>
26+
</xs:complexType>
27+
</xs:element>
28+
</xs:schema>

examples/dotnet/Examples/Project.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using MongoDB.Bson;
2+
using Realms;
3+
4+
namespace dotnet
5+
{
6+
public class Project : RealmObject
7+
{
8+
[PrimaryKey]
9+
[MapTo("_id")]
10+
public ObjectId Id { get; set; }
11+
[MapTo("_partition")]
12+
public string Partition { get; set; }
13+
[MapTo("name")]
14+
[Required]
15+
public string Name { get; set; }
16+
}
17+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System.Linq;
2+
using dotnet;
3+
using MongoDB.Bson;
4+
using NUnit.Framework;
5+
using Realms;
6+
using Realms.Sync;
7+
8+
namespace UnitTests
9+
{
10+
public class RealmTests
11+
{
12+
Realms.Sync.App app;
13+
ObjectId testTaskId;
14+
Realms.Sync.User user;
15+
SyncConfiguration config;
16+
const string myRealmAppId = "tuts-tijya";
17+
18+
[SetUp]
19+
public async System.Threading.Tasks.Task Setup()
20+
{
21+
// :code-block-start: initialize-realm
22+
app = Realms.Sync.App.Create(myRealmAppId);
23+
// :code-block-end:
24+
user = app.LogInAsync(Credentials.EmailPassword("[email protected]", "foobar")).Result;
25+
config = new SyncConfiguration("My Project", user);
26+
Realm realm = await Realm.GetInstanceAsync(config);
27+
// :code-block-start: create
28+
Task testTask = new Task()
29+
{
30+
Name = "Do this thing",
31+
Status = TaskStatus.Open.ToString()
32+
};
33+
34+
realm.Write(() =>
35+
{
36+
realm.Add(testTask);
37+
});
38+
// :code-block-end:
39+
testTaskId = testTask.Id;
40+
return;
41+
}
42+
43+
[Test]
44+
public async System.Threading.Tasks.Task GetsSyncedTasks()
45+
{
46+
// :code-block-start: anon-login
47+
Realms.Sync.User user = app.LogInAsync(Credentials.Anonymous()).Result;
48+
// :code-block-end:
49+
// :code-block-start: config
50+
config = new SyncConfiguration("My Project", user);
51+
Realm realm = await Realm.GetInstanceAsync(config);
52+
// :code-block-end:
53+
// :code-block-start: read-all
54+
var tasks = realm.All<Task>().ToList();
55+
// :code-block-end:
56+
Assert.AreEqual(1, tasks.Count);
57+
// :code-block-start: read-some
58+
tasks = realm.All<Task>().Where(t=>t.Status == "Open").ToList();
59+
// :code-block-end:
60+
Assert.AreEqual(1, tasks.Count);
61+
return;
62+
}
63+
64+
[Test]
65+
public async System.Threading.Tasks.Task ModifiesATask()
66+
{
67+
config = new SyncConfiguration("My Project", user);
68+
Realm realm = await Realm.GetInstanceAsync(config);
69+
// :code-block-start: modify
70+
Task t = realm.All<Task>()
71+
.Where(t => t.Id == testTaskId)
72+
.FirstOrDefault();
73+
74+
realm.Write(() =>
75+
{
76+
t.Status = TaskStatus.InProgress.ToString();
77+
});
78+
79+
// :code-block-end:
80+
var allTasks = realm.All<Task>().ToList();
81+
Assert.AreEqual(1, allTasks.Count);
82+
Assert.AreEqual(TaskStatus.InProgress.ToString(), allTasks.First().Status);
83+
84+
return;
85+
}
86+
87+
88+
[TearDown]
89+
public async System.Threading.Tasks.Task TearDown()
90+
{
91+
config = new SyncConfiguration("My Project", user);
92+
Realm realm = await Realm.GetInstanceAsync(config);
93+
// :code-block-start: delete
94+
realm.Write(() =>
95+
{
96+
realm.RemoveAll<Task>();
97+
});
98+
// :code-block-end:
99+
// :code-block-start: logout
100+
await user.LogOutAsync();
101+
// :code-block-end:
102+
return;
103+
}
104+
}
105+
}

examples/dotnet/Examples/Task.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// :code-block-start:task-object-model
2+
using MongoDB.Bson;
3+
using Realms;
4+
namespace dotnet
5+
{
6+
public class Task : RealmObject
7+
{
8+
[PrimaryKey]
9+
[MapTo("_id")]
10+
public ObjectId Id { get; set; }
11+
12+
[MapTo("_partition")]
13+
public string Partition { get; set; }
14+
15+
[MapTo("assignee")]
16+
public User Assignee { get; set; }
17+
18+
[MapTo("name")]
19+
[Required]
20+
public string Name { get; set; }
21+
22+
[MapTo("status")]
23+
[Required]
24+
public string Status { get; set; }
25+
26+
public Task()
27+
{
28+
this.Id = ObjectId.GenerateNewId();
29+
}
30+
}
31+
32+
public enum TaskStatus
33+
{
34+
Open,
35+
InProgress,
36+
Complete
37+
}
38+
}
39+
// :code-block-end:

examples/dotnet/Examples/User.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using MongoDB.Bson;
2+
using Realms;
3+
4+
namespace dotnet
5+
{
6+
public class User : RealmObject
7+
{
8+
[PrimaryKey]
9+
[MapTo("_id")]
10+
[Required]
11+
public string Id { get; set; }
12+
[MapTo("_partition")]
13+
public string Partition { get; set; }
14+
[MapTo("image")]
15+
public string Image { get; set; }
16+
[MapTo("name")]
17+
[Required]
18+
public string Name { get; set; }
19+
}
20+
}

examples/dotnet/dotnet.sln

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples", "Examples\Examples.csproj", "{C97249A2-33A1-457A-AAEC-389B62984885}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|Any CPU = Debug|Any CPU
9+
Release|Any CPU = Release|Any CPU
10+
Debug|iPhoneSimulator = Debug|iPhoneSimulator
11+
Release|iPhoneSimulator = Release|iPhoneSimulator
12+
Debug|iPhone = Debug|iPhone
13+
Release|iPhone = Release|iPhone
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
21+
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
22+
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
23+
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
24+
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|iPhone.ActiveCfg = Debug|Any CPU
25+
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|iPhone.Build.0 = Debug|Any CPU
26+
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|iPhone.ActiveCfg = Release|Any CPU
27+
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|iPhone.Build.0 = Release|Any CPU
28+
EndGlobalSection
29+
EndGlobal

0 commit comments

Comments
 (0)