Last active
November 11, 2022 02:27
-
-
Save winnicki/03d42baa15f067798ba47810999f260f to your computer and use it in GitHub Desktop.
.NET MongoDB + Realm Sync models demonstrating relationships and embedded objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Realms; | |
namespace RealmDev.Core.RealmObjects | |
{ | |
public class Comment : EmbeddedObject | |
{ | |
public DateTimeOffset CreatedOn { get; set; } | |
public string Text { get; set; } | |
public string Author { get; set; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using MongoDB.Bson; | |
using MongoDB.Bson.Serialization.Attributes; | |
using Realms; | |
namespace RealmDev.Core.RealmObjects | |
{ | |
public class Contractor : RealmObject | |
{ | |
[PrimaryKey] | |
[MapTo("_id")] | |
public ObjectId Id { get; set; } = ObjectId.GenerateNewId(); | |
[MapTo("_partition")] | |
[Required] | |
public string Partition { get; set; } | |
[MapTo("name")] | |
[Required] | |
public string Name { get; set; } | |
[MapTo("title")] | |
[Required] | |
public string Title { get; set; } | |
// To-many relationship | |
[Backlink(nameof(Issue.Assignee))] | |
public IQueryable<Issue> Issues { get; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Document : RealmObject, IDocument | |
{ | |
[PrimaryKey] | |
[MapTo("_id")] | |
public ObjectId Id { get; set; } = ObjectId.GenerateNewId(); | |
[MapTo("_partition")] | |
[Required] | |
public string Partition { get; set; } | |
[Required] | |
public string Name { get; set; } | |
// Back-linked relationship | |
public Folder Folder { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using MongoDB.Bson; | |
using MongoDB.Bson.Serialization.Attributes; | |
using RealmDev.Core.Interfaces.Browse; | |
using Realms; | |
namespace RealmDev.Core.RealmObjects.Browse | |
{ | |
public class Folder : RealmObject, IFolder | |
{ | |
[PrimaryKey] | |
[MapTo("_id")] | |
public ObjectId Id { get; set; } = ObjectId.GenerateNewId(); | |
[MapTo("_partition")] | |
[Required] | |
public string Partition { get; set; } | |
[Required] | |
public string Name { get; set; } | |
// Back-linked relationship | |
public Folder ParentFolder { get; set; } | |
// To-many relationship | |
public IList<Folder> Folders { get; } | |
// To-many relationship | |
public IList<Document> Documents { get; } | |
[BsonIgnore] public IEnumerable<IFolder> FolderItems => Folders; | |
[BsonIgnore] public IEnumerable<IDocument> DocumentItems => Documents; | |
[BsonIgnore] public IFolder Parent => ParentFolder; | |
[BsonIgnore] public string Key => Id.ToString(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using MongoDB.Bson; | |
using MongoDB.Bson.Serialization.Attributes; | |
using RealmDev.Core.Enums; | |
using RealmDev.Core.Interfaces.Issues; | |
using Realms; | |
namespace RealmDev.Core.RealmObjects | |
{ | |
public class Issue : RealmObject, IIssue | |
{ | |
[PrimaryKey] | |
[MapTo("_id")] | |
public ObjectId Id { get; set; } = ObjectId.GenerateNewId(); | |
[MapTo("_partition")] | |
[Required] | |
public string Partition { get; set; } | |
[Required] | |
public string Description { get; set; } | |
public DateTimeOffset CreatedOn { get; set; } | |
public int StatusId { get; set; } | |
[BsonIgnore] | |
public IssueStatus Status | |
{ | |
get => (IssueStatus)StatusId; | |
set => StatusId = (int)value; | |
} | |
// Embedded objects | |
public IssuePriority Priority { get; set; } | |
public IList<Comment> Comments { get; } | |
public IList<Photo> Photos { get; } | |
public IList<IssueHistory> History { get; } | |
// Back-linked relationship | |
public Contractor Assignee { get; set; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class IssueHistory : EmbeddedObject | |
{ | |
public DateTimeOffset CreatedOn { get; set; } | |
public IssueStatus Status | |
{ | |
get => (IssueStatus)StateId; | |
set => StateId = (int)value; | |
} | |
public int StateId { get; set; } | |
public string ByUser { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class IssuePriority : EmbeddedObject | |
{ | |
public ObjectId Id { get; set; } | |
public int ValueId { get; set; } | |
[BsonIgnore] | |
public Enums.Priority Value | |
{ | |
get => (Enums.Priority)ValueId; | |
set => ValueId = (int)value; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Photo : EmbeddedObject | |
{ | |
public DateTimeOffset CreatedOn { get; set; } | |
public string Url { get; set; } | |
public string FileName { get; set; } | |
public string Author { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment