Note that there are some explanatory texts on larger screens.

plurals
  1. POS#arp Architecture Fluent mapping for self referencing entity (Tree structure)
    primarykey
    data
    text
    <p>I've come up against a problem in converting my Fluent NH mapping to Sharp Architecture. I like the platform for it's ease, however it seems to handle entity mappings slightly differently to pure Fluent NH.</p> <p>I have a Entity 'Category' that is a simple tree structure. I have to override the auto-mapping as there is a M:M property that I need to add in (not included in code below).</p> <p>When I create tests on the repository, the GetAll method returns all Categories as it should, however the Children property just infinitely loops itself. i.e. the list of children for each category only contains itself, in and unending loop.</p> <p><strong>/// The Entity ///</strong></p> <pre><code>public class Category : Entity { public Category() { InitMembers(); } /// &lt;summary&gt; /// Creates valid domain object /// &lt;/summary&gt; public Category(string name) : this() { Name = name; } /// &lt;summary&gt; /// Creates valid domain object /// &lt;/summary&gt; public Category(string name, int depth) : this() { Name = name; Depth = depth; } private void InitMembers() { Children = new List&lt;Category&gt;(); } [DomainSignature] [NotNullNotEmpty] public virtual string Name { get; protected set; } [DomainSignature] public virtual int Depth { get; protected set; } public virtual Category Parent { get; set; } public virtual IList&lt;Category&gt; Children { get; private set; } public virtual void AddChild(Category category) { category.Parent = this; Children.Add(category); } } </code></pre> <p><strong>/// The Mapping ///</strong></p> <pre><code>public class CategoryMap : IAutoMappingOverride&lt;Category&gt; { public void Override(AutoMap&lt;Category&gt; mapping) { mapping.Id(x =&gt; x.Id, "CategoryId") .WithUnsavedValue(0) .GeneratedBy.Identity(); mapping.Map(x =&gt; x.Name).WithLengthOf(50); mapping.Map(x =&gt; x.Depth); mapping.HasMany&lt;Category&gt;(x =&gt; x.Children) .Inverse() .Cascade.All() .KeyColumnNames.Add("Parent_id") .AsBag(); } } </code></pre> <p><strong>/// The Data Repository Tests ///</strong></p> <pre><code>[TestFixture] [Category("DB Tests")] public class CategoryRepositoryTests : RepositoryTestsBase { private readonly IRepository&lt;Category&gt; _repository = new Repository&lt;Category&gt;(); protected override void LoadTestData() { CreatePersistedCategory("Root 1"); CreatePersistedCategory("Root 2"); CreatePersistedCategoryWithChildren("Level 1", "Level 2", "Level 3"); } [Test] public void CanGetAllCategories() { var categories = _repository.GetAll(); categories.ShouldNotBeNull(); categories.Count.ShouldEqual(5); } [Test] public void CanGetCategoryById() { var category = _repository.Get(1); category.Name.ShouldEqual("Root 1"); category.Depth.ShouldEqual(1); } [Test] public void CanGetCategoryChildren() { var category = _repository.Get(3); category.Name.ShouldEqual("Level 1"); category.Depth.ShouldEqual(1); category.Children.ShouldNotBeNull(); category.Children.Count.ShouldEqual(1); category.Children[0].Name.ShouldEqual("Level 2"); category.Children[0].Depth.ShouldEqual(2); category.Children[0].Children.ShouldNotBeNull(); category.Children[0].Children.Count.ShouldEqual(1); category.Children[0].Children[0].Name.ShouldEqual("Level 3"); category.Children[0].Children[0].Depth.ShouldEqual(3); } private void CreatePersistedCategory(string categoryName) { var category = new Category(categoryName, 1); _repository.SaveOrUpdate(category); FlushSessionAndEvict(category); } private void CreatePersistedCategoryWithChildren(string category1, string category2, string category3) { var cat1 = new Category(category1, 1); var cat2 = new Category(category2, 2) { Parent = cat1 }; var cat3 = new Category(category3, 3) { Parent = cat2 }; cat1.AddChild(cat2); cat2.AddChild(cat3); _repository.SaveOrUpdate(cat1); FlushSessionAndEvict(cat1); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload