Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.
-
Upload
nathan-lane -
Category
Documents
-
view
212 -
download
0
Transcript of Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.
![Page 1: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/1.jpg)
Top 7 Lessons From My First Big Silverlight Project
Benjamin Day Benjamin Day Consulting, Inc.
![Page 2: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/2.jpg)
About: Benjamin Day
• Brookline, MA• Consultant, Coach, Trainer• Microsoft MVP for Visual Studio ALM• Team Foundation Server, Software Testing,
Architecture, Windows Azure• TechEd, VSLive, DevTeach• Visual Studio Magazine, Redmond Developer
News• Scrum.org Classes
– Professional Scrum Developer (PSD)– Professional Scrum Foundations (PSF)
• www.benday.com• [email protected]• @benday
![Page 3: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/3.jpg)
OVERVIEW
![Page 4: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/4.jpg)
Goals
• Call out the pitfalls• Learn from my mistakes• Silverlight pain points• Hopefully, avoid the pitfalls
![Page 5: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/5.jpg)
• Regulatory application for a Federal agency
• As of 1/15/2011:– Silverlight
1,557 *.cs files11 MB of *.cs
– Service code507 *.cs files2.3 MB of *.cs
• Approximately 12 months of development
Background
![Page 6: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/6.jpg)
It’s reasonably large.
![Page 7: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/7.jpg)
• Silverlight 4• GalaSoft MvvmLight• Microsoft Unity• .NET 4• WCF • Enterprise Library 5.0 Data Access Block• SQL Server 2008 R2• Windows 2008 R2• Visual Studio 2010• Team Foundation Server 2010
Technologies
![Page 8: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/8.jpg)
We’re not using RIA Services.
![Page 9: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/9.jpg)
If you’re using RIA, that’s OK.
![Page 10: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/10.jpg)
(You’re not necessarily a bad person.)
![Page 11: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/11.jpg)
• In software, technologies that are supposed to save me time usually don't. They steal short-term time savings from long-term maintenance.
My $0.02 on RIA Services.
![Page 12: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/12.jpg)
• On enterprise projects, time to market matters but not as much as the long-term maintenance and operational concerns of the app.
My $0.02 on RIA Services.
![Page 13: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/13.jpg)
• If time to market matters more than maintenance or operations, RIA is an option.
• RIA introduces tight coupling between the client and server
My $0.02 on RIA Services.
![Page 14: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/14.jpg)
THE LESSONS.
![Page 15: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/15.jpg)
The 7 Lessons.
1. Client-side & Server-side: It’s 2 applications.
2. Unit test, unit test, unit test. 3. Async WCF calls dictate your architecture.4. Repository & Adapter patterns are your
friend.5. No shortcuts: Keep your ViewModels &
Models separate.6. Primitive Obsession in your ViewModel.7. x:Name is a code smell.
![Page 16: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/16.jpg)
Lesson 1: It’s 2 applications.
![Page 17: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/17.jpg)
Your app.
• Let’s say you have a Silverlight UI• Calls back to some WCF endpoints• WCF services write data in to a
database
• You’re probably writing all this at the same time
• Feels like you’re writing one application.
![Page 18: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/18.jpg)
Client-side & Server-side: They’re 2 applications
Service• Service-Oriented
Application (SOA)• Uses a database for
it’s persistence• “Domain Model”
implementation• User interface is a
collection of WCF endpoints
Client• Runs on a desktop
machine• Silverlight UI• Has it’s own object
model• Uses WCF services for
persistence
![Page 19: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/19.jpg)
Why do I think this?
• When data is returned via WCF, you’re leaving the AppDomain
• Objects on the server are converted to XML• XML is hierarchical
• When you’re writing WCF applications, it’s a mistake to think that you’re returning objects.
• Your application is actually “message-oriented”
![Page 20: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/20.jpg)
Why do I think this?
• Difficult to share code on both sides of the wire
• You’re writing both apps at the same time but the server-side code doesn’t need the Silverlight code.
• Silverlight definitely needs the service code.
![Page 21: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/21.jpg)
Benefits of thinking about 2 apps
• It’s like building a tunnel under a mountain.
• Meet in the middle– Design the endpoints– Server team works on server code– Client team works on client code
• Find problems faster• Iterate with your customer faster
![Page 22: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/22.jpg)
Benefits of thinking about 2 apps
• Code to abstractions• Fewer dependencies• Loosely coupled• Easier to test
![Page 23: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/23.jpg)
Lesson 2: Unit test, unit test, unit test.
![Page 24: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/24.jpg)
Disclaimer: I’m obsessed with unit testing.
![Page 25: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/25.jpg)
Why unit test?
• What’s “done” stays “done”.• You find problems early.• Bugs stay dead.• Refactoring is painless.
• If your application is large, it’s tough to know when you’ve broken something obscure.
• Unit tests tell you when you’ve broken your app.
![Page 26: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/26.jpg)
What do I test?
Silverlight• ViewModels
– Operations– Commands– INotifyPropertyChanged– Progress Bars– Show/Hide Logic– Login/Logout
• Domain Models• Domain Model Services• WCF logic
– I don’t test making WCF calls from Silverlight to the WCF Services
• Utility methods
Server-side
• Data access code– aka. Repositories
• Domain Model• Domain Model
Services• Services
– Direct calls without WCF
– Calls through WCF
![Page 27: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/27.jpg)
Which unit test frameworks?
• Server-side: Visual Studio 2010 (MSTest)
• Silverlight:Silverlight Control Toolkit Unit Test Framework– It’s not great.– Better than nothing.– At least it runs in Silverlight.
![Page 28: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/28.jpg)
Benefits of “2 applications” + unit testing
• Thinking of 2 apps helps isolate problems• Are the service unit tests passing?• Are the Silverlight unit tests passing?
Silverlight Passing?
Services Passing?
Conclusion
Yes No Service problem
No Yes Silverlight problem
Yes Yes New problem
No No Whew! $%^& is broken.
![Page 29: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/29.jpg)
Tip: Don’t write new code if the tests are broken.
• Always code on top of a known good state
• If you make changes and the tests stop passing, it’s your fault.– Do your team a favor. Please, don’t check in.
![Page 30: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/30.jpg)
Side note: TFS 2010 Gated Check-in
• Continuous integration on steroids.• Check in goes to a shelveset• A build kicks off that compiles the
shelved code• Runs unit tests• Compile fails or unit testing fails your
code doesn’t get checked in• No more broken builds!
![Page 31: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/31.jpg)
Lesson 3: Async WCF rules your architecture.
![Page 32: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/32.jpg)
(BTW…)
![Page 33: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/33.jpg)
(Just between you and me.)
![Page 34: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/34.jpg)
(This one nearly killed me.)
![Page 35: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/35.jpg)
My initial client-side architecture.
![Page 36: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/36.jpg)
My architecture after Async WCF beat me up and ate my lunch.
![Page 37: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/37.jpg)
Network traffic in Silverlight
• It has to be async.• If it isn’t, the UI thread locks…forever.
![Page 38: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/38.jpg)
Async Kills
• Your Repository methods can’t return populated objects must return void
• Exception handling is hard– Work happens on a different thread– Exceptions can’t “bubble up” the stack
• You could have your *.xaml.cs handle the callbacks – Ugly– Violates “separation of concerns”
![Page 39: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/39.jpg)
My Solution: ReturnResult<T>
• “Virtual” call stack• Notify(Exception) or Notify(T)
![Page 40: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/40.jpg)
The “glue” between method calls
![Page 41: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/41.jpg)
• Reactive Extensions for .NET • http
://msdn.microsoft.com/en-us/devlabs/ee794896.aspx
Alternate Solution
![Page 42: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/42.jpg)
Lesson 4:Repository & Adapter Patterns
are your friend
![Page 43: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/43.jpg)
What is Repository?
![Page 44: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/44.jpg)
The Repository Pattern
• “Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.”– http://martinfowler.com/eaaCatalog/repository.h
tml
• Encapsulates the logic of getting things saved and retrieved
![Page 45: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/45.jpg)
Synchronous Repository
![Page 46: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/46.jpg)
Synchronous SQL Server & WCF
![Page 47: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/47.jpg)
A Big Picture
![Page 48: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/48.jpg)
Async Repository
![Page 49: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/49.jpg)
What is Adapter?
![Page 50: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/50.jpg)
Adapter Pattern
• “…converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.”
• from “Head First Design Patterns”by Elisabeth & Eric Freeman
![Page 51: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/51.jpg)
My version of Adapter Pattern
• Take object of Type A and convert it in to object of Type B
![Page 52: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/52.jpg)
Why are these patterns your friend?
• Help focus your mind• Better design• Help contain bugs
– These conversions to/from will be buggy
• Help localize change– Service endpoint designs will change often
• Unit test the conversions separately– (Remember it’s a “unit” test.)
![Page 53: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/53.jpg)
SOLID Principles
• Robert C. Martin• http://butunclebob.com/
ArticleS.UncleBob.PrinciplesOfOod
![Page 54: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/54.jpg)
SOLID Principles of Class Design
Principle Purpose
Single Responsibility A class should have one, and only one, reason to change.
Open Closed You should be able to extend a class’s behavior without modifying it.
Liskov Substitution Derived classes must be substitutable for their base classes.
Interface Segregation Make fine grained interfaces that are client specific.
Dependency Inversion Depend on abstractions, not on concretions.
![Page 55: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/55.jpg)
Single Responsibility Principle
• Poster by Derick Bailey
![Page 56: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/56.jpg)
Keep the Adapt separated from the Retrieve
• Two classes– Repository knows how to talk to the WCF service– Adapter knows how to turn the Service Reference
types into Models
![Page 57: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/57.jpg)
Lesson 5:No shortcuts: Keep your ViewModels & Models
separate.
![Page 58: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/58.jpg)
No shortcuts: Keep your ViewModels & Models separate.
• It will be tempting to have your Repository/Adapter layer create ViewModels– (Don’t.)
• There’s a reason why it’s calledModel-View-ViewModel
![Page 59: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/59.jpg)
Why keep Model and ViewModel separated?
• ViewModel is a user interface design• Model is the state of your application
– aka. “Domain Model” pattern
• ViewModel advocates for the UI– 1-to-1 between a ViewModel and a *.xaml file– Might reference multiple Models
• Don’t have the ViewModel fields directly update the Model.
![Page 60: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/60.jpg)
It’s all about the Cancel button.
• If you’re “two way” data bound, How do you undo?
![Page 61: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/61.jpg)
Cancel: ViewModel wraps Model
• ViewModel populatesitself from the Model
• User edits the screen,ViewModel gets updated
• Model doesn’t get changed until Save button is clicked.
• Model is The Boss.
![Page 62: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/62.jpg)
Lesson 6:Primitive Obsession in your ViewModel.
![Page 63: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/63.jpg)
Primitive Obsession
• James Shore’s “Primitive Obsession”– Too many plain scalar values– Phone number isn’t really just
a string– http://www.jamesshore.com/Blog/
• Validation in the get / set properties is ok but is phone number validation really the responsibility of the Person class?
![Page 64: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/64.jpg)
Coarse-Grained vs. Fine-GrainedObject Model
• James Shore blog entry talks about Responsibilities– Fine-grained = More object-oriented– Data and properties are split into actual
responsibilities
• I’m concerned about Responsibilities + Code Duplication + Simplicity
![Page 65: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/65.jpg)
• Provides common functionality for a property on a ViewModel
ViewModelField<T>
![Page 66: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/66.jpg)
With & Without ViewModelField<T>
![Page 67: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/67.jpg)
Are your ViewModel propertiesCoarse or Fine?
• Fine-grained gives you room to grow• ViewModelField<T>• Create custom controls that know how
to talk to your ViewModelFields– Simplified binding expressions
• Add features later– Field validation later– Security
![Page 68: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/68.jpg)
• http://bit.ly/hcdjk2
Article: 6 MVVM Tips
![Page 69: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/69.jpg)
Lesson 7: x:Name is a code smell.
![Page 70: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/70.jpg)
• An indication that there *MIGHT* be a problem– Quality– Maintenance– Performance– Etc…
What’s a ‘Code Smell’?
![Page 71: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/71.jpg)
Bad for ViewModel & UnitTesting
Named
• Values populated by get/set statements
• Implies a lot of code in the “code behind” (*.xaml.cs)
Not Named
• Populated via binding expressions
• ViewModel-centric• Minimal *.xaml.cs
code• * BETTER TESTABILITY
*
![Page 72: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/72.jpg)
• CodedUI Tests and Manual Test Automation in Microsoft Test Manager (MTM) & VS2010 Ultimate– Visual Studio 2010 Feature Pack 2– Requires controls to be uniquely named in order to
automate simulated user interactions
• Animation Designers in Blend– (I have been told this but have not verified this
myself.)
Where my opinion breaks down…
![Page 73: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/73.jpg)
Remember: A “smell” is not necessarily bad.
![Page 74: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/74.jpg)
Bad Smell
• This is garbage.• Do not eat.• Smells awful.
![Page 75: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/75.jpg)
• This is Langres• From
Champagne-Adrenne region of France
• Cow’s milk• Tastes
awesome• Smells fairly
awful
Good Smell
Photo: Benjamin Day, 12/14/2008
![Page 76: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/76.jpg)
• Cheese is controlled rot. It’s a way of preserving food. It’s supposed to be that way.
• Garbage is garbage.
Good Smell vs. Bad Smell
![Page 77: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/77.jpg)
The Recap.
![Page 78: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/78.jpg)
The 7 Lessons.
1. Client-side & Server-side: It’s 2 applications.
2. Unit test, unit test, unit test. 3. Async WCF calls dictate your architecture.4. Repository & Adapter patterns are your
friend.5. No shortcuts: Keep your ViewModels &
Models separate.6. Primitive Obsession in your ViewModel.7. x:Name is a code smell.
![Page 79: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/79.jpg)
Any last questions?
![Page 80: Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.](https://reader030.fdocuments.in/reader030/viewer/2022032606/56649e8e5503460f94b9166a/html5/thumbnails/80.jpg)
Goals
• Call out the pitfalls• Learn from my mistakes• Silverlight pain points• Hopefully, avoid the pitfalls