Presentation is loading. Please wait.

Presentation is loading. Please wait.

Neo.NET Entity Objects Patterns and Best Practices Copyright © 2002-2003 Erik Dörnenburg – Last updated: December 2003.

Similar presentations


Presentation on theme: "Neo.NET Entity Objects Patterns and Best Practices Copyright © 2002-2003 Erik Dörnenburg – Last updated: December 2003."— Presentation transcript:

1 Neo.NET Entity Objects http://neo.codehaus.org Patterns and Best Practices Copyright © 2002-2003 Erik Dörnenburg – Last updated: December 2003

2 Neo 1Contents 1.Use protected relations 2.Provide custom queries 3.Provide transparent many-to-many relations 4.Use entity maps sparingly

3 Neo 2Use protected relations Task: Given a publisher find all its titles Worst case: Implementation using factories directly in code-behind or some controller. MyController.cs TitleFactory f = new TitleFactory(myContext); TitleList l = f.Find("Publisher = {0}", pub);

4 Neo 3Use protected relations Improvement 1: Declare an iref in schema and use public relation. MyController.cs TitleRelation l = pub.Titles; or a copy if it MyController.cs TitleList l = new TitleList(pub.Titles);

5 Neo 4Use protected relations Improvement 2: Make relation protected (in the template for base classes) and provide accessors Publisher.cs public TitleList GetTitles() { return new TitleList(this.Titles); } public void AddTitle(Title aTitle) { this.Titles.Add(aTitle); } MyController.cs TitleList l = pub.GetTitles();

6 Neo 5Provide custom queries Task: Given a publisher find all its titles with additional criterion Worst case: Implementation using qualifiers directly in code-behind or some controller. MyController.cs TitleFactory f = new TitleFactory(myContext); TitleList l = f.Find("Publisher = {0}" + "and Royalties > 1000", pub);

7 Neo 6Provide custom queries Improvement 1: Move code into entity object. Publisher.cs public TitleList GetTitlesWithMinRoyalty(int r) { /* several options */ } MyController.cs l = pub.GetTitlesWithMinRoyalty(1000);

8 Neo 7Provide custom queries Option 1: Move query as is. Publisher.cs public TitleList GetTitlesWithMinRoyalty(int r) { TitleFactory f = new TitleFactory(Context); TitleList l = f.Find("Publisher = {0}" + "and Royalty > {1}", this, r); return l; }

9 Neo 8Provide custom queries Option 2: Use internal relation; elegant and caching behaviour but has to bring all titles for publisher into memory. Publisher.cs public TitleList GetTitlesWithMinRoyalty(int r) { return this.Titles.Find("Royalties > {0}", r); }

10 Neo 9Provide custom queries Option 3: Use internal relation and handwrite loop with comparison; improved performance over option 2 but slightly more code. Publisher.cs public TitleList GetTitlesWithMinRoyalty(int r) { TitleList l = new TitleList(); foreach(Title t in this.Titles) if(t.Royalty > r) l.Add(t); return l; }

11 Neo 10Provide transparent many-to-many Task: Given a title find all its authors Solution: Add a method to Title. Title.cs public AuthorList GetAuthors() { AuthorList l = new AuthorList (); foreach(TitleAuthor ta in this.TitleAuthors) l.Add(ta.Author); return l; }

12 Neo 11Provide transparent many-to-many Solution: Add a method to author and/or title. Author.cs public void AddToTitle(Title title) { TitleAuthorFactory f = new Title…(Context); TitleAuthor ta = f.CreateObject(title, this); } Title.cs public void AddAuthor(Author author) { TitleAuthorFactory f = new Title…(Context); TitleAuthor ta = f.CreateObject(this, author); }

13 Neo 12EntityMaps Use entity maps sparingly and access through context: IEntityMapFactory f = context.EntityMapFactory; IEntityMap map = f.GetMap(typeof(Title));


Download ppt "Neo.NET Entity Objects Patterns and Best Practices Copyright © 2002-2003 Erik Dörnenburg – Last updated: December 2003."

Similar presentations


Ads by Google