1 / 33

WPF & EF 4 Tales From the Trenches

WPF & EF 4 Tales From the Trenches. Introductions. Nathan Allen-Wagner Senior .NET Architect Bennett Adelson Consulting www.bennettadelson.com nallen-wagner@bennettadelson.com. Introductions. Me - Online. Agenda. Quick Intro Entity Framework Models, Repositories, and T4 Templates

Download Presentation

WPF & EF 4 Tales From the Trenches

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. WPF & EF 4Tales From the Trenches

  2. Introductions Nathan Allen-Wagner Senior .NET Architect Bennett Adelson Consulting www.bennettadelson.com nallen-wagner@bennettadelson.com

  3. Introductions Me - Online

  4. Agenda Quick Intro Entity Framework • Models, Repositories, and T4 Templates • Demos WPF • Tips, Tricks, and Approaches • Undo / Redo • Demos

  5. Session Goal… If I Succeed Today… • We’ll cover a lot of topics • I’ll convince you to download and explore the demo app. • I’ll introduce you to a couple open-source libraries

  6. Quick Intro – High Level Entity Framework WPF / MVVM INotifyPropertyChanged Undo / Redo MVVM & Locators Keyboard Focus Reactive Programming Threading • Self Tracking Entities • Repositories • Enabling Change Tracking • Saving Changes • Customized template • Partial Classes / Methods • Delayed Initialization • Undo

  7. Quick Intro – Relevant Scenarios How does this apply to ME?

  8. Preview – The Demo App Demo! Before we dive in… … Here’s our demo app

  9. PART 2 – WPF PART 1 Entity Framework

  10. Definition of Terms: Models Models Are… …the “M” in MVC, MVP, MVVM Model View Controller ModelView Presenter Model View ViewModel …the Data …the “Single Source of Truth” …Behavioral (methods)

  11. Definition of Terms – Repositories Repositories are… …a way to get a model …a way to save changes to a model Persistence Ignorance… …hides data storage / access choices RDO, ADO, ADO.NET Nhibernate, EF, Linq2SQL …separates concerns …makes unit testing easier

  12. Entity Framework – Model Designer & Code Generator Entity Framework: Model Designer • Database First • Model First Code Generator (T4 Templates) • Default Template • Self Tracking Entities Template • Plain Old CLR Objects (POCO) Template

  13. Entity Framework – Self Tracking Entities Self Tracking Entities… Originally designed to enable EF over WCF • Share entity dll with client • Client entities track their changes • Changes sent back to service • Service applies changes to context But… works great for the Desktop too!

  14. Entity Framework – Build your own Repository Goals for a Repository: • Use interfaces for definition • Expose methods to get, save and query • Keep all EF “stuff” inside. • Clients should not need a reference to System.Data.Entities

  15. Entity Framework – Repository Demo Demo – EF Basics • EF Model • Getting and Applying the STE template • Separating entities into separate project • Interfaces for repository • Multiple Repositories: SQL & XML

  16. Entity Framework – Repository Tips Demo – EF Tips & Tricks • Loading Children From the DB • .Include(“”) vs.LoadProperty(o => o.Child) • Saving • ApplyChanges(), SaveChanges() • MarkAllAddedAsUnmodified(),RefreshAll() • AcceptAllChanges(),StartTrackingAll() • Deletes • Parent.Remove() does not delete the record…

  17. Entity Framework – Template Tips Demo – T4 Customizations • Partial Classes / Methods • Partial Classes = Custom Logic / Properties • Partial Methods = Hooks into property changes • Undo Hooks • T4 includes calls to UndoServicefor property changes • OnInitializing() & OnInitialized() • Works with DelayedInitializationScope

  18. Entity Framework – Delayed Initialization Scope Demo - Delayed Init Scope • Entities call RegisterForEndInit() • … Then EndInit() called after last scope completes • Use in a using block • Supports nesting • ThreadStatic variables to track “current” • Callers can check whether a scope is active

  19. Entity Framework – Undo / Redo Tracking Demo – Undo / Redo Tracking • Each setter tells Undo framework about change • SelfTrackingEntityUndoService.OnChanging() • Undo framework adds a “change” to the undo stack • Each “change” knows how to apply the previous value • More on this later

  20. PART 2 – WPF PART 2 WPF

  21. WPF – Agenda Revisited WPF / MVVM • INotifyPropertyChanged • Undo / Redo • MVVM & Locators • Keyboard Focus • Reactive Programming • Threading

  22. WPF – App Goals Goals for the Demo App Good Architecture… … Community says MVVM for WPF / SL. “Single Source of Truth” rendered in one or more views, which stay in sync as data changes in the model. Model contains core data and behavior. View-specific concerns are in the ViewModel or View. Undo / Redo support

  23. WPF – Bindings and INotifyPropertyChanged WPF Bindings & INotifyPropertyChanged WPF means Powerful Binding Support! Two-Way bindings require Change Notification WPF UI elements use DependencyProperties for change notification. What about the model…???

  24. WPF – Bindings and INotifyPropertyChanged Model Change Notification: System.ComponentModel.INotifyPropertyChanged • Primary way to tell WPF that a property value changed System.Collections.ObjectModel.ObservableCollection • Primary way to tell WPF that a collection changed • Implements INotifyCollectionChanged • Raises an event whenever items added, removed, replaced or relocated.

  25. WPF – Using an Entity Framework Model Using a Model – Self Tracking Entities Partial Methods Revisited • RaisingPropertyChanged for “dependent” properties • Validating data Undo / Redo Support • Importance of INPC for Undo – It changes the model • Available from http://muf.codeplex.com/ • Hooking it up – Commands • Optimizations – Undo Batches

  26. WPF – Dealing with ViewModels Dealing With ViewModels “Completing the INPC loop” • Snippets for Implementing INPC • On My Blog (http://blog.alner.net/...) • Remember to raisePropertyChanged for “dependent” properties • Crazy Cool: ContinuousLinq’sReactiveObject • http://clinq.codeplex.com/

  27. WPF – Tips and Tricks WPF Tips and Tricks Keyboard Focus • Toolbars don’t cause “LostFocus” event • Use the CommitBindingsBehavior to force bindings. • Demo…

  28. WPF – Tips and Tricks WPF Tips and Tricks Design Time & “Blendability” • Use “design-time” detection to populate ViewModels. • Demo…

  29. WPF – Tips and Tricks WPF Tips and Tricks Threading • Use the BackgroundWorker for lengthy operations • Marshal all changes back to UI thread via: • Dispatcher.Invoke() / BeginInvoke() • SynchronizationContext.Send() / Post() • Warning! Collections are problematic. Some solutions exist, but each has problematic edge cases. • T4 Template includes SynchronizedContextCollection, which attempts to marshall changes to UI via SynchronizationContext.

  30. WPF – Weak References & Weak Events FYI: Weak References & Weak Events WPF uses these extensively Critical to allowing the GC to collect memory Important when you have GC roots holding on to things (like static instances). May need a memory profiler. Event Handlers create a reverse reference. If you don’t disconnect, itmight lock things in memory.

  31. WPF – ViewModel Locators ViewModel Locator Options Code-behind / Resource MVVM Light – Smart Resource Caliburn – ViewModel-First (awesome framework!) MEFed MVVM – Using MEF to create ViewModels My Preference: Converter Based Locators Demo (if time permits)

  32. Thanks! Congratulations! We made it to… The End! (If you’d like more info one of these topics, let us know)

  33. Thanks! Thanks! See you next month! (Please turn in evals) Nathan Allen-Wagner .NET Architect - Bennett Adelson nallen-wagner@bennettadelson.com

More Related