Skip to content
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

Update QuickStart to have consistent code examples #2873

Merged
merged 15 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 14 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
2 changes: 1 addition & 1 deletion examples/dotnet/ConsoleTests/ConsoleTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<ItemGroup>
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
<PackageReference Include="Nito.AsyncEx.Tasks" Version="5.1.2" />
<PackageReference Include="Realm" Version="11.1.1" />
<PackageReference Include="Realm" Version="11.1.2" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion examples/dotnet/ConsoleTests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static async Task MainAsync(string[] args)
//:snippet-end:
}

class Item : RealmObject
partial class Item : IRealmObject
{
public int Size { get; set; }
}
Expand Down
2 changes: 1 addition & 1 deletion examples/dotnet/DataBinding/DataBinding.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2578" />
<PackageReference Include="Xamarin.Essentials" Version="1.7.6" />
<PackageReference Include="Realm" Version="11.1.1" />
<PackageReference Include="Realm" Version="11.1.2" />
</ItemGroup>
<ItemGroup>
<None Remove="Realm" />
Expand Down
2 changes: 1 addition & 1 deletion examples/dotnet/Examples/Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
<PackageReference Include="Realm" Version="11.1.1" />
<PackageReference Include="Realm" Version="11.1.2" />
<PackageReference Include="Realm.Fody" Version="11.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
2 changes: 1 addition & 1 deletion examples/dotnet/Examples/Geospatial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,4 @@ public partial class Company : IRealmObject
public Company() { }
}
//:snippet-end:
}
}
2 changes: 1 addition & 1 deletion examples/dotnet/Examples/Objects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class IgnorantRenamer
// ...and the Image itself can be
// in-memory when the app is running:
[Ignored]
public Image Thumbnail { get; set; }
public Image? Thumbnail { get; set; }
// :snippet-end:
}
// :snippet-start: rename
Expand Down
62 changes: 47 additions & 15 deletions examples/dotnet/Examples/QuickStartExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,11 @@ public async Task Setup()
};

Realm realm = Realm.GetInstance(config);
realm.Write(() =>
{
realm.RemoveAll<Item>();
});

await realm.WriteAsync(() =>
{
realm.Add<Dog>(new Dog() { Name = "Fydaeu", Age = 1 });
realm.Add<Item>(new Item() { Name = "Fydaeu", Assignee = String.Empty });
});

}


Expand All @@ -61,7 +56,8 @@ public async Task WriteAndUpdate()
var testItem = new Item
{
Name = "Do this thing",
Status = ItemStatus.Open.ToString()
Status = ItemStatus.Open.ToString(),
Assignee = "Aimee"
};

await realm.WriteAsync(() =>
Expand All @@ -77,37 +73,70 @@ await realm.WriteAsync(() =>
return realm.Add<Item>(new Item
{
Name = "Do this thing, too",
Status = ItemStatus.InProgress.ToString()
Status = ItemStatus.InProgress.ToString(),
Assignee = "Satya"
});
}
);

// :snippet-end:
testItemId = testItem.Id;

//:snippet-start:read-all
var allItems = realm.All<Item>();
// :snippet-end:

//:snippet-start:read-open-items
var openItems = realm.All<Item>()
.Where(i => i.Status == "Open");
// :snippet-end:

//:snippet-start:sort-items
var sortedItems = realm.All<Item>()
.OrderBy(i => i.Status);
// :snippet-end:


// :snippet-start: upsert
var id = ObjectId.GenerateNewId();

var kerry = new Person { Id = id, Name = "Kerry" };
var item1 = new Item
{
Id = id,
Name = "Defibrillate the Master Oscillator",
Assignee = "Aimee"
};

// Add a new person to the realm. Since nobody with the existing Id
// has been added yet, this person is added.
await realm.WriteAsync(() =>
{
realm.Add(kerry, update: true);
realm.Add(item1, update: true);
});

var sarah = new Person { Id = id, Name = "Sarah" };
var item2 = new Item
{
Id = id,
Name = "Fluxify the Turbo Encabulator",
Assignee = "Aimee"
};

// Based on the unique Id field, we have an existing person,
// but with a different name. When `update` is true, you overwrite
// the original entry (i.e. Kerry -> Sarah).
// the original entry.
await realm.WriteAsync(() =>
{
realm.Add(sarah, update: true);
realm.Add(item2, update: true);
});
// item1 now has a Name of "Fluxify the Turbo Encabulator"
// and item2 was not added as a new Item in the collection.
// :snippet-end:
Assert.IsTrue(kerry.Name == "Sarah");

//:snippet-start:read-filter
var someItems = realm.All<Item>();
// :snippet-end:

Assert.IsTrue(item1.Name == "Fluxify the Turbo Encabulator");

var myid = ObjectId.GenerateNewId();
// :snippet-start: modify-collection
Expand Down Expand Up @@ -191,12 +220,13 @@ public async Task TearDown()
App app = App.Create(myRealmAppId);
using (var realm = Realm.GetInstance(config))
{
var myItem = new Item() { Name = "foo2", Status = ItemStatus.Complete.ToString() };
var myItem = new Item() { Name = "foo2", Status = ItemStatus.Complete.ToString(), Assignee = String.Empty };
realm.Write(() =>
{
realm.Add(myItem);
});

//:snippet-start:delete-one-item
realm.Write(() =>
{
realm.Remove(myItem);
Expand All @@ -206,6 +236,8 @@ public async Task TearDown()
{
realm.RemoveAll<Item>();
});

//:snippet-end:
var user = await app.LogInAsync(Credentials.Anonymous());
// :snippet-start: logout
await user.LogOutAsync();
Expand Down
15 changes: 8 additions & 7 deletions examples/dotnet/Examples/Task.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
// :snippet-start:task-object-model
using MongoDB.Bson;
using MongoDB.Bson;
using Realms;
using User = Examples.Models.User;

namespace Examples.Models
{
public class Item : RealmObject
//:snippet-start:item-model
public partial class Item : IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();

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

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

[MapTo("status")]
public string Status { get; set; }
public string? Status { get; set; }
}
// :snippet-end:
//:snippet-end:
public enum ItemStatus
{
Open,
Expand Down
6 changes: 3 additions & 3 deletions examples/dotnet/Examples/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Examples.Models
{
public class User : RealmObject
public partial class User : IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public string _id { get; set; }
public string Id { get; set; }

[MapTo("image")]
public string Image { get; set; }
public string? Image { get; set; }
}
}
1 change: 0 additions & 1 deletion examples/dotnet/Examples/WorkWithRealm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public async System.Threading.Tasks.Task Setup()
{
var appConfig = new AppConfiguration(myRealmAppId)
{
// LogLevel = LogLevel.Debug,
DefaultRequestTimeout = TimeSpan.FromMilliseconds(1500)
};

Expand Down
1 change: 0 additions & 1 deletion examples/dotnet/LocalOnly/Guitar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace LocalOnly
{

public partial class Guitar : IRealmObject
{
[PrimaryKey]
Expand Down
2 changes: 1 addition & 1 deletion examples/dotnet/LocalOnly/LocalOnly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Realm" Version="11.1.1" />
<PackageReference Include="Realm" Version="11.1.2" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion source/examples/generated/dotnet/Objects.snippet.ignore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
// ...and the Image itself can be
// in-memory when the app is running:
[Ignored]
public Image Thumbnail { get; set; }
public Image? Thumbnail { get; set; }
7 changes: 0 additions & 7 deletions source/examples/generated/dotnet/Program.snippet.delete.cs

This file was deleted.

This file was deleted.

This file was deleted.

9 changes: 0 additions & 9 deletions source/examples/generated/dotnet/Program.snippet.update.cs

This file was deleted.

10 changes: 0 additions & 10 deletions source/examples/generated/dotnet/Program.snippet.write.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
realm.Write(() =>
{
realm.Remove(myItem);
});

realm.Write(() =>
{
realm.RemoveAll<Item>();
});

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var allItems = realm.All<Item>();
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var openItems = realm.All<Item>()
.Where(i => i.Status == "Open");
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var sortedItems = realm.All<Item>()
.OrderBy(i => i.Status);
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
var id = ObjectId.GenerateNewId();

var kerry = new Person { Id = id, Name = "Kerry" };
var item1 = new Item { Id = id, Name = "Defibrillate the Master Oscillator" };

// Add a new person to the realm. Since nobody with the existing Id
// has been added yet, this person is added.
await realm.WriteAsync(() =>
{
realm.Add(kerry, update: true);
realm.Add(item1, update: true);
});

var sarah = new Person { Id = id, Name = "Sarah" };
var item2 = new Item { Id = id, Name = "Fluxify the Turbo Encabulator" };

// Based on the unique Id field, we have an existing person,
// but with a different name. When `update` is true, you overwrite
// the original entry (i.e. Kerry -> Sarah).
// the original entry.
await realm.WriteAsync(() =>
{
realm.Add(sarah, update: true);
realm.Add(item2, update: true);
});
// item1 now has a Name of "Fluxify the Turbo Encabulator"
// and item2 was not added as a new Item in the collection.
15 changes: 15 additions & 0 deletions source/examples/generated/dotnet/Task.snippet.item-model.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public partial class Item : IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();

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

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

[MapTo("status")]
public string? Status { get; set; }
}
20 changes: 0 additions & 20 deletions source/examples/generated/dotnet/Task.snippet.task-object-model.cs

This file was deleted.

This file was deleted.

Loading