Skip to content

Instantly share code, notes, and snippets.

@winnicki
Last active November 11, 2022 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winnicki/03d42baa15f067798ba47810999f260f to your computer and use it in GitHub Desktop.
Save winnicki/03d42baa15f067798ba47810999f260f to your computer and use it in GitHub Desktop.
.NET MongoDB + Realm Sync models demonstrating relationships and embedded objects
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; }
}
}
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; }
}
}
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; }
}
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();
}
}
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; }
}
}
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; }
}
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;
}
}
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