Académique Documents
Professionnel Documents
Culture Documents
Neo
Contents
1. Use protected relations 2. Provide custom queries 3. Provide transparent many-to-many relations
Neo
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);
Neo
or a copy if it
MyController.cs
Neo
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();
Neo
Task: Given a publisher find all its titles with additional criterion
TitleFactory f = new TitleFactory(myContext); TitleList l = f.Find("Publisher = {0}" + "and Royalties > 1000", pub);
Neo
l = pub.GetTitlesWithMinRoyalty(1000);
Neo
Neo
Option 2: Use internal relation; elegant and caching behaviour but has to bring all titles for publisher into memory.
Publisher.cs
Neo
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; }
Neo
10
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; }
Neo
11
Neo
EntityMaps
12