Neo NET Entity Objects http neo codehaus org
Neo . NET Entity Objects http: //neo. codehaus. org Patterns and Best Practices Copyright © 2002 -2003 Erik Dörnenburg – Last updated: December 2003
Neo Contents 1. Use protected relations 2. Provide custom queries 3. Provide transparent many-to-many relations 4. Use entity maps sparingly
Neo Use protected relations Task: Given a publisher find all its titles Worst case: Implementation using factories directly in code-behind or some controller. My. Controller. cs Title. Factory f = new Title. Factory(my. Context); Title. List l = f. Find("Publisher = {0}", pub);
Neo Use protected relations Improvement 1: Declare an iref in schema and use public relation. My. Controller. cs Title. Relation l = pub. Titles; or a copy if it My. Controller. cs Title. List l = new Title. List(pub. Titles);
Neo Use protected relations Improvement 2: Make relation protected (in the template for base classes) and provide accessors Publisher. cs public Title. List Get. Titles() { return new Title. List(this. Titles); } public void Add. Title(Title a. Title) { this. Titles. Add(a. Title); } My. Controller. cs Title. List l = pub. Get. Titles();
Neo Provide 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. My. Controller. cs Title. Factory f = new Title. Factory(my. Context); Title. List l = f. Find("Publisher = {0}" + "and Royalties > 1000", pub);
Neo Provide custom queries Improvement 1: Move code into entity object. Publisher. cs public Title. List Get. Titles. With. Min. Royalty(int r) { /* several options */ } My. Controller. cs l = pub. Get. Titles. With. Min. Royalty(1000);
Neo Provide custom queries Option 1: Move query as is. Publisher. cs public Title. List Get. Titles. With. Min. Royalty(int r) { Title. Factory f = new Title. Factory(Context); Title. List l = f. Find("Publisher = {0}" + "and Royalty > {1}", this, r); return l; }
Neo Provide custom queries Option 2: Use internal relation; elegant and caching behaviour but has to bring all titles for publisher into memory. Publisher. cs public Title. List Get. Titles. With. Min. Royalty(int r) { return this. Titles. Find("Royalties > {0}", r); }
Neo Provide custom queries Option 3: Use internal relation and handwrite loop with comparison; improved performance over option 2 but slightly more code. Publisher. cs public Title. List Get. Titles. With. Min. Royalty(int r) { Title. List l = new Title. List(); foreach(Title t in this. Titles) if(t. Royalty > r) l. Add(t); return l; }
Neo Provide transparent many-to-many Task: Given a title find all its authors Solution: Add a method to Title. cs public Author. List Get. Authors() { Author. List l = new Author. List (); foreach(Title. Author ta in this. Title. Authors) l. Add(ta. Author); return l; }
Neo Provide transparent many-to-many Solution: Add a method to author and/or title. Author. cs public void Add. To. Title(Title title) { Title. Author. Factory f = new Title…(Context); Title. Author ta = f. Create. Object(title, this); } Title. cs public void Add. Author(Author author) { Title. Author. Factory f = new Title…(Context); Title. Author ta = f. Create. Object(this, author); }
Neo Entity. Maps Use entity maps sparingly and access through context: IEntity. Map. Factory f = context. Entity. Map. Factory; IEntity. Map map = f. Get. Map(typeof(Title));
- Slides: 13