Last active
December 21, 2015 15:19
-
-
Save dmitrimaximov/6325925 to your computer and use it in GitHub Desktop.
This gist demonstrates simple EntitySets with additional relationship-related data and autosync feature
(http://support.x-tensive.com/question/5688/entitysets-with-relationship-definition)
This file contains 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
namespace Relationships | |
{ | |
public enum RelationshipKind | |
{ | |
GrandMa = 1, | |
GrandSon = 2, | |
} | |
[HierarchyRoot] | |
public class Person : Entity | |
{ | |
[Field, Key] | |
public int Id { get; set; } | |
[Field] | |
public string Name { get; set; } | |
[Field] | |
[Association(PairTo = "Person", OnOwnerRemove = OnRemoveAction.Cascade, | |
OnTargetRemove = OnRemoveAction.Clear)] | |
public RelationshipSet Relationships { get; private set; } | |
protected override void OnRemoving() { | |
base.OnRemoving(); | |
var items = this.Relationships.ToList(); | |
foreach (var item in items) { | |
item.Relative.Relationships.Remove(this); | |
} | |
} | |
public Person(Session session) | |
: base(session) {} | |
} | |
[HierarchyRoot] | |
public class Relationship : Entity | |
{ | |
[Field, Key(0)] | |
public Person Person { get; private set; } | |
[Field, Key(1)] | |
public Person Relative { get; private set; } | |
[Field] | |
public RelationshipKind Kind { get; private set; } | |
internal Relationship(Session session, Person owner, | |
Person relative, RelationshipKind kind) | |
: base (session, owner, relative) { | |
Kind = kind; | |
} | |
} | |
public class RelationshipSet : EntitySet<Relationship> | |
{ | |
public void Add(Person relative, RelationshipKind kind, | |
RelationshipKind? reverse = null) { | |
if (this.Any(r => r.Relative == relative)) | |
return; | |
this.Add(new Relationship(this.Session, this.Owner as Person, relative, kind)); | |
if (reverse != null) | |
relative.Relationships.Add(this.Owner as Person, reverse.Value); | |
} | |
public void Remove(Person relative) { | |
var relationship = this.SingleOrDefault(r => r.Relative == relative); | |
if (relationship == null) | |
return; | |
relationship.Remove(); | |
relative.Relationships.Remove(this.Owner as Person); | |
} | |
protected RelationshipSet(Entity owner, FieldInfo field) | |
: base(owner, field) {} | |
protected RelationshipSet(SerializationInfo info, StreamingContext context) | |
: base(info, context) {} | |
} | |
} |
This file contains 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
namespace Relationships | |
{ | |
public class Program | |
{ | |
[Test] | |
public static void Main() { | |
var domainConfiguration = DomainConfiguration.Load("Default"); | |
var domain = Domain.Build(domainConfiguration); | |
using (var session = domain.OpenSession()) { | |
using (var t = session.OpenTransaction()) { | |
var grandMa = new Person(session); | |
var grandSon = new Person(session); | |
grandMa.Relationships.Add(grandSon, RelationshipKind.GrandMa, RelationshipKind.GrandSon); | |
Assert.AreEqual(1, grandMa.Relationships.Count); | |
Assert.AreEqual(1, grandSon.Relationships.Count); | |
grandSon.Relationships.Remove(grandMa); | |
Assert.AreEqual(0, grandMa.Relationships.Count); | |
Assert.AreEqual(0, grandSon.Relationships.Count); | |
t.Complete(); | |
} | |
} | |
Console.WriteLine("OK"); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment