WPF EF 4 Tales From the Trenches Introductions

  • Slides: 33
Download presentation
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 Nathan Allen-Wagner Senior. NET Architect Bennett Adelson Consulting www. bennettadelson. com nallen-wagner@bennettadelson. com

Introductions Me - Online Blog http: //blog. alner. net Podcast http: //North. Coast. Code.

Introductions Me - Online Blog http: //blog. alner. net Podcast http: //North. Coast. Code. Cast. net Twitter @nathanaw

Agenda Quick Intro Entity Framework • Models, Repositories, and T 4 Templates • Demos

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

Session Goal… If I Succeed Today… 1. We’ll cover a lot of topics 2.

Session Goal… If I Succeed Today… 1. We’ll cover a lot of topics 2. I’ll convince you to download and explore the demo app. 3. I’ll introduce you to a couple opensource libraries

Quick Intro – High Level Entity Framework WPF / MVVM 1. Self Tracking Entities

Quick Intro – High Level Entity Framework WPF / MVVM 1. Self Tracking Entities 2. Repositories 1. 2. 3. 4. 5. 6. a. b. Enabling Change Tracking Saving Changes 3. Customized template a. b. c. Partial Classes / Methods Delayed Initialization Undo INotify. Property. Changed Undo / Redo MVVM & Locators Keyboard Focus Reactive Programming Threading

Quick Intro – Relevant Scenarios How does this apply to ME? Entity Framework Undo

Quick Intro – Relevant Scenarios How does this apply to ME? Entity Framework Undo MVVM WPF Y Y Y Win. Forms Y Y N Silverlight Y* Y* Y Y N N Web

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

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

PART 2 – WPF PART 1 Entity Framework

PART 2 – WPF PART 1 Entity Framework

Definition of Terms: Models Are… …the “M” in MVC, MVP, MVVM Model View Controller

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

Definition of Terms – Repositories are… …a way to get a model …a way

Definition of Terms – 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. NET Nhibernate, EF, Linq 2 SQL …separates concerns …makes unit testing easier

Entity Framework – Model Designer & Code Generator Entity Framework: Model Designer - Database

Entity Framework – Model Designer & Code Generator Entity Framework: Model Designer - Database First - Model First Code Generator (T 4 Templates) - Default Template - Self Tracking Entities Template - Plain Old CLR Objects (POCO) Template

Entity Framework – Self Tracking Entities… Originally designed to enable EF over WCF 1)

Entity Framework – Self Tracking Entities… Originally designed to enable EF over WCF 1) 2) 3) 4) 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!

Entity Framework – Build your own Repository Goals for a Repository: 1) Use interfaces

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

Entity Framework – Repository Demo – EF Basics • • • EF Model Getting

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

Entity Framework – Repository Tips Demo – EF Tips & Tricks • Loading Children

Entity Framework – Repository Tips Demo – EF Tips & Tricks • Loading Children From the DB §. Include(“”) vs. Load. Property(o => o. Child) • Saving § Apply. Changes(), Save. Changes() § Mark. All. Added. As. Unmodified(), Refresh. All() § Accept. All. Changes(), Start. Tracking. All() • Deletes § Parent. Remove() does not delete the record…

Entity Framework – Template Tips Demo – T 4 Customizations • Partial Classes /

Entity Framework – Template Tips Demo – T 4 Customizations • Partial Classes / Methods § Partial Classes = Custom Logic / Properties § Partial Methods = Hooks into property changes • Undo Hooks § T 4 includes calls to Undo. Service for property changes • On. Initializing() & On. Initialized() § Works with Delayed. Initialization. Scope

Entity Framework – Delayed Initialization Scope Demo - Delayed Init Scope • Entities call

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

Entity Framework – Undo / Redo Tracking Demo – Undo / Redo Tracking •

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

PART 2 – WPF PART 2 WPF

PART 2 – WPF PART 2 WPF

WPF – Agenda Revisited WPF / MVVM 1. 2. 3. 4. 5. 6. INotify.

WPF – Agenda Revisited WPF / MVVM 1. 2. 3. 4. 5. 6. INotify. Property. Changed Undo / Redo MVVM & Locators Keyboard Focus Reactive Programming Threading

WPF – App Goals for the Demo App Good Architecture… … Community says MVVM

WPF – App 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 View. Model or View. Undo / Redo support

WPF – Bindings and INotify. Property. Changed WPF Bindings & INotify. Property. Changed WPF

WPF – Bindings and INotify. Property. Changed WPF Bindings & INotify. Property. Changed WPF means Powerful Binding Support! Two-Way bindings require Change Notification WPF UI elements use Dependency. Properties for change notification. What about the model…? ? ?

WPF – Bindings and INotify. Property. Changed Model Change Notification: System. Component. Model. INotify.

WPF – Bindings and INotify. Property. Changed Model Change Notification: System. Component. Model. INotify. Property. Changed • Primary way to tell WPF that a property value changed System. Collections. Object. Model. Observable. Collection • Primary way to tell WPF that a collection changed • Implements INotify. Collection. Changed • Raises an event whenever items added, removed, replaced or relocated.

WPF – Using an Entity Framework Model Using a Model – Self Tracking Entities

WPF – Using an Entity Framework Model Using a Model – Self Tracking Entities Partial Methods Revisited § Raising Property. Changed 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

WPF – Dealing with View. Models Dealing With View. Models “Completing the INPC loop”

WPF – Dealing with View. Models Dealing With View. Models “Completing the INPC loop” § Snippets for Implementing INPC – On My Blog (http: //blog. alner. net/. . . ) § Remember to raise Property. Changed for “dependent” properties § Crazy Cool: Continuous. Linq’s Reactive. Object – http: //clinq. codeplex. com/

WPF – Tips and Tricks WPF Tips and Tricks Keyboard Focus § Toolbars don’t

WPF – Tips and Tricks WPF Tips and Tricks Keyboard Focus § Toolbars don’t cause “Lost. Focus” event § Use the Commit. Bindings. Behavior to force bindings. § Demo…

WPF – Tips and Tricks WPF Tips and Tricks Design Time & “Blendability” §

WPF – Tips and Tricks WPF Tips and Tricks Design Time & “Blendability” § Use “design-time” detection to populate View. Models. § Demo…

WPF – Tips and Tricks WPF Tips and Tricks Threading § Use the Background.

WPF – Tips and Tricks WPF Tips and Tricks Threading § Use the Background. Worker for lengthy operations § Marshal all changes back to UI thread via: – Dispatcher. Invoke() / Begin. Invoke() – Synchronization. Context. Send() / Post() § Warning! Collections are problematic. Some solutions exist, but each has problematic edge cases. § T 4 Template includes Synchronized. Context. Collection, which attempts to marshall changes to UI via Synchronization. Context.

WPF – Weak References & Weak Events FYI: Weak References & Weak Events WPF

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, it might lock things in memory.

WPF – View. Model Locators View. Model Locator Options Code-behind / Resource MVVM Light

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

Thanks! Congratulations! We made it to… The End! (If you’d like more info one

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

Thanks! See you next month! (Please turn in evals) Nathan Allen-Wagner. NET Architect -

Thanks! See you next month! (Please turn in evals) Nathan Allen-Wagner. NET Architect - Bennett Adelson nallen-wagner@bennettadelson. com Blog http: //blog. alner. net Podcast http: //North. Coast. Code. Cast. net Twitter @nathanaw