Lightness Perception and Lightness Illusions, E.H. Adelson (2000)
WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett...
-
Upload
tiara-eyre -
Category
Documents
-
view
213 -
download
0
Transcript of WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett...
![Page 1: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/1.jpg)
WPF & EF 4Tales From the Trenches
![Page 2: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/2.jpg)
Introductions
Nathan Allen-Wagner
Senior .NET Architect
Bennett Adelson Consulting
www.bennettadelson.com
![Page 3: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/3.jpg)
Introductions
Me - Online
Blog http://blog.alner.net
Podcast http://NorthCoastCodeCast.net
Twitter @nathanaw
![Page 4: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/4.jpg)
Agenda
Quick Intro
Entity Framework• Models, Repositories, and T4 Templates• Demos
WPF• Tips, Tricks, and Approaches• Undo / Redo• Demos
![Page 5: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/5.jpg)
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 open-source libraries
![Page 6: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/6.jpg)
Quick Intro – High Level
Entity Framework1. Self Tracking Entities
2. Repositoriesa. Enabling Change Tracking
b. Saving Changes
3. Customized templatea. Partial Classes / Methods
b. Delayed Initialization
c. Undo
WPF / MVVM1. INotifyPropertyChanged
2. Undo / Redo
3. MVVM & Locators
4. Keyboard Focus
5. Reactive Programming
6. Threading
![Page 7: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/7.jpg)
Quick Intro – Relevant Scenarios
How does this apply to ME?
Entity Framework Undo MVVM
WPF Y Y Y
WinForms Y Y N
Silverlight Y * Y * Y
Web Y N N
![Page 8: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/8.jpg)
Preview – The Demo App
Demo!
Before we dive in…
… Here’s our demo app
![Page 9: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/9.jpg)
PART 2 – WPF
PART 1
Entity Framework
![Page 10: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/10.jpg)
Definition of Terms: Models
Models Are…
…the “M” in MVC, MVP, MVVMModel View Controller
Model View Presenter
Model View ViewModel
…the Data
…the “Single Source of Truth”
…Behavioral (methods)
![Page 11: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/11.jpg)
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 choicesRDO, ADO, ADO.NET
Nhibernate, EF, Linq2SQL
…separates concerns
…makes unit testing easier
![Page 12: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/12.jpg)
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
![Page 13: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/13.jpg)
Entity Framework – Self Tracking Entities
Self Tracking Entities…
Originally designed to enable EF over WCF1) Share entity dll with client
2) Client entities track their changes
3) Changes sent back to service
4) Service applies changes to context
But… works great for the Desktop too!
![Page 14: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/14.jpg)
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
![Page 15: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/15.jpg)
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
![Page 16: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/16.jpg)
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…
![Page 17: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/17.jpg)
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 UndoService
for property changes
• OnInitializing() & OnInitialized() Works with DelayedInitializationScope
![Page 18: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/18.jpg)
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
![Page 19: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/19.jpg)
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
![Page 20: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/20.jpg)
PART 2 – WPF
PART 2
WPF
![Page 21: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/21.jpg)
WPF – Agenda Revisited
WPF / MVVM
1. INotifyPropertyChanged
2. Undo / Redo
3. MVVM & Locators
4. Keyboard Focus
5. Reactive Programming
6. Threading
![Page 22: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/22.jpg)
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
![Page 23: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/23.jpg)
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…???
![Page 24: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/24.jpg)
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.
![Page 25: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/25.jpg)
WPF – Using an Entity Framework Model
Using a Model – Self Tracking Entities
Partial Methods Revisited Raising PropertyChanged 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
![Page 26: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/26.jpg)
WPF – Dealing with ViewModels
Dealing With ViewModels
“Completing the INPC loop” Snippets for Implementing INPC
– On My Blog (http://blog.alner.net/...)
Remember to raise PropertyChanged for “dependent” properties
Crazy Cool: ContinuousLinq’s ReactiveObject– http://clinq.codeplex.com/
![Page 27: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/27.jpg)
WPF – Tips and Tricks
WPF Tips and Tricks
Keyboard Focus Toolbars don’t cause “LostFocus” event Use the CommitBindingsBehavior to force bindings. Demo…
![Page 28: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/28.jpg)
WPF – Tips and Tricks
WPF Tips and Tricks
Design Time & “Blendability” Use “design-time” detection to populate ViewModels. Demo…
![Page 29: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/29.jpg)
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.
![Page 30: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/30.jpg)
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.
![Page 31: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/31.jpg)
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)
![Page 32: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/32.jpg)
Thanks!
Congratulations! We made it to…
The End!
(If you’d like more info one of these topics, let us know)
![Page 33: WPF & EF 4 Tales From the Trenches. Introductions Nathan Allen-Wagner Senior.NET Architect Bennett Adelson Consulting nallen-wagner@bennettadelson.com.](https://reader036.fdocuments.net/reader036/viewer/2022070307/551b709f550346d6338b4e30/html5/thumbnails/33.jpg)
Thanks!
Thanks! See you next month!
(Please turn in evals)
Nathan Allen-Wagner.NET Architect - Bennett Adelson
Blog http://blog.alner.net
Podcast http://NorthCoastCodeCast.net
Twitter @nathanaw