SlideShare a Scribd company logo
1 of 30
SMTC WenemRmnrnr XNXNXNNXNX SNS, EndndjHsmnwmd ndvbsn@infusion.com August 29, 2007 Silverlight 3 Best Practice Do’s and Don’ts
Stephen Kennedy (skennedy@infusion.com) Presenter
Design Patterns Development Optimizations Overview
Design patterns Silverlight 3
Model View Controller/Presenter Pattern Problem: Decoupling data display logic from the business logic that maintains and updates it Solution: MVC Pattern separates the modeling of the domain, presentation, and actions based on user input into three separate classes: Model: Manages behavior/data of the application domain responds to requests for information about its state (usually from View) responds to instructions to change its state (usually from Controller). View: Manages the display information Controller: Informs the Model to change its state by interpreting inputs from keyboard, mouse and external instructions.
Developed by Martin Fowler, and is an extension of MVP Again, the View is separated from its behavior and state A Presentation Model is introduced, which acts as an abstraction of the view – so the view is merely a rendering of the Presentation Model View has a reference to Presentation Model, but Presentation Model has no reference to view Presentation Model
MVP/MVC  nice for web and thick client Good for ASP.NET,WinForms, WPF (as a start) Good for Silverlight as a start What you’re used to
Identical to Presentation Model, but engineered by John Gossman to utilize WPF Dependency Properties and Commands to bind UI to abstraction Ideal as it allows abstraction, called the ViewModel, to be tested Also allows for any UI to bind to ViewModel fields to show and modify the data XAML controls / Elements bind to properties in the ViewModel class Use Model-View-ViewModel Pattern
Model POCO – no Dependency Properties / Routed Events No UI knowledge - pure data Design Patterns: MVVM
View Model No knowledge of the view Exposes the model(s) / custom properties used in the view Use ObservableCollection<T> for collections of data Implement INotifyPropertyChanged for custom properties Should be able to act as a command line interface to the view No Dependency Properties Design Patterns: MVVM
View Binds to the ViewModel (DataContext) Use converters to bind visual elements to ViewModel / Model properties ie: Model’s Temperature  Colour Brush Events are hooked up through either code-behind or commanding Commanding better, but no OOB support in SL Silverlight Extensions project on CodePlex Custom Behaviors Composite Application Guidance/Library (Prism) Design Patterns: MVVM
View and ViewModel Binding ViewModel.cs App.xaml.cs ModelData md; public ViewModel() {   md=new ModelData(); } public string Data {    get{  return md.Data;}    set{ md.Data = value;} } public App()  { View view = new View(); view.DataContext= new ViewModel();  //… }  View.xaml ... <TextBox  Content=“{Binding Data}” ... /> ...
Can use “regular” property procedures in ViewModel If binding a collection to a DataGrid, etc., prefer the ObservableCollection<> (or non-generic equivalent) to sync updates back to the Model automatically MVVM Data Binding
MVVM Collection Item Binding The View should never bind to the data directly, but bind to a ViewModel If a collection of data is to be displayed in the View, the main ViewModel should expose an ObservableCollection of an additional ViewModel class that exposes properties, and thus, data Property ... Property ... Property ... Property ... Property ... Property ... public ObservableCollectionMyOC ...{ return new ObservableCollection<ViewModel2> ... }  Property ... Property ... Property ... <DataGridItemsSource=“ {Binding MyOC}” .../> Property ... Property ... Property ... Property ... Property ... Property ... ViewModel2 The View ViewModel1
Issue: changing property values in ViewModel does not always update bound control in View Solution: have ViewModel implement INotifyPropertyChanged interface, and raise PropertyChanged event in each property Or better yet, create a base ViewModel class and have each ViewModel subclass it Property Binding
Attached Properties in XAML can also be used in lieu of missing Command  Create Command, expose as property in ViewModel Create static class with Attached Property Add namespace to XAML screen Place Attached Property in desired control Commands and Attached Properties
Attached Property
ViewModel and XAML with Attached Property
The View knows about the ViewModel, but the ViewModel does not know about the View ViewModel is assigned to the DataContext of the View (Silverlight XAML) In xaml.cs codebehind for page In App.xaml.cs In XAML via Resource Other Associating ViewModel with View
Create ViewModel base class (implement INotifyPropertyChanged) Have all ViewModels subclass base Expose NO models to View directly Use Attached Properties as ‘workaround’ to lack of Commands in Silverlight controls Possibly Behaviors … Good practices …
Development Silverlight 3
Easy to get sloppy – name all controls/elements UI elements should have the same naming convention as your private class variables (that’s what they become) ie: <Button x:Name=“_submitButton” /> Prefix every resource with a “namespace” “Storyboards.FlashUserImage” “Brushes.ButtonBackground” “Templates.GiftsListBox” Development
Use new Data Annotations framework (.NET 3.5/4.0) ,[object Object]
[Range(1,100)]
[StringLength(15)]
[RegularExpresion(“^M.*”)]
[CustomValidation(typeof(MyValidator, “MyValidationMethod”))Can be applied at class or property level Data Annotations are your friend
Import System.ComponentModel.DataAnnotations Annotate properties/methods Can specify custom validation method TIP: Use a convention pattern ,[object Object]
Use a method name convention (use a constant)Data Annotations
Pull common control appearance into styles Break app-wide resources into multiple files (by merging resource dictionaries into App.xaml) ,[object Object]

More Related Content

What's hot

Just a View: An Introduction To Model-View-Controller Pattern
Just a View:  An Introduction To Model-View-Controller PatternJust a View:  An Introduction To Model-View-Controller Pattern
Just a View: An Introduction To Model-View-Controller PatternAaron Nordyke
 
Ppt of Basic MVC Structure
Ppt of Basic MVC StructurePpt of Basic MVC Structure
Ppt of Basic MVC StructureDipika Wadhvani
 
Design Patterns in ZK: Java MVVM as Model-View-Binder
Design Patterns in ZK: Java MVVM as Model-View-BinderDesign Patterns in ZK: Java MVVM as Model-View-Binder
Design Patterns in ZK: Java MVVM as Model-View-BinderSimon Massey
 
Leveling up with AngularJS
Leveling up with AngularJSLeveling up with AngularJS
Leveling up with AngularJSAustin Condiff
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC PresentationVolkan Uzun
 
Mvc pattern and implementation in java fair
Mvc   pattern   and implementation   in   java fairMvc   pattern   and implementation   in   java fair
Mvc pattern and implementation in java fairTech_MX
 
Model view controller (mvc)
Model view controller (mvc)Model view controller (mvc)
Model view controller (mvc)icapetillos
 
Ui design patterns
Ui design patternsUi design patterns
Ui design patternsJorge Ortiz
 
Design Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVMDesign Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVMMudasir Qazi
 
Model view controller (mvc)
Model view controller (mvc)Model view controller (mvc)
Model view controller (mvc)M Ahsan Khan
 
Introduction to mvc architecture
Introduction to mvc architectureIntroduction to mvc architecture
Introduction to mvc architectureravindraquicsolv
 
Model View Controller(MVC)
Model View Controller(MVC)Model View Controller(MVC)
Model View Controller(MVC)Himanshu Chawla
 

What's hot (20)

Just a View: An Introduction To Model-View-Controller Pattern
Just a View:  An Introduction To Model-View-Controller PatternJust a View:  An Introduction To Model-View-Controller Pattern
Just a View: An Introduction To Model-View-Controller Pattern
 
Ppt of Basic MVC Structure
Ppt of Basic MVC StructurePpt of Basic MVC Structure
Ppt of Basic MVC Structure
 
MVC
MVCMVC
MVC
 
Design Patterns in ZK: Java MVVM as Model-View-Binder
Design Patterns in ZK: Java MVVM as Model-View-BinderDesign Patterns in ZK: Java MVVM as Model-View-Binder
Design Patterns in ZK: Java MVVM as Model-View-Binder
 
Why MVC?
Why MVC?Why MVC?
Why MVC?
 
Leveling up with AngularJS
Leveling up with AngularJSLeveling up with AngularJS
Leveling up with AngularJS
 
Mvc fundamental
Mvc fundamentalMvc fundamental
Mvc fundamental
 
Jsp with mvc
Jsp with mvcJsp with mvc
Jsp with mvc
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 
Mvc pattern and implementation in java fair
Mvc   pattern   and implementation   in   java fairMvc   pattern   and implementation   in   java fair
Mvc pattern and implementation in java fair
 
Model view controller (mvc)
Model view controller (mvc)Model view controller (mvc)
Model view controller (mvc)
 
Ui design patterns
Ui design patternsUi design patterns
Ui design patterns
 
Design Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVMDesign Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVM
 
MVVM In Use
MVVM In UseMVVM In Use
MVVM In Use
 
Model view controller (mvc)
Model view controller (mvc)Model view controller (mvc)
Model view controller (mvc)
 
Introduction to mvc architecture
Introduction to mvc architectureIntroduction to mvc architecture
Introduction to mvc architecture
 
Mvc, mvp, mvvm...
Mvc, mvp, mvvm...Mvc, mvp, mvvm...
Mvc, mvp, mvvm...
 
Why Use MVC?
Why Use MVC?Why Use MVC?
Why Use MVC?
 
MVC architecture
MVC architectureMVC architecture
MVC architecture
 
Model View Controller(MVC)
Model View Controller(MVC)Model View Controller(MVC)
Model View Controller(MVC)
 

Similar to Silverlight 3 Best Practice Do's and Don'ts

MVC Pattern. Flex implementation of MVC
MVC Pattern. Flex implementation of MVCMVC Pattern. Flex implementation of MVC
MVC Pattern. Flex implementation of MVCAnton Krasnoshchok
 
Building an enterprise app in silverlight 4 and NHibernate
Building an enterprise app in silverlight 4 and NHibernateBuilding an enterprise app in silverlight 4 and NHibernate
Building an enterprise app in silverlight 4 and NHibernatebwullems
 
MVC ppt presentation
MVC ppt presentationMVC ppt presentation
MVC ppt presentationBhavin Shah
 
Code Camp 06 Model View Presenter Architecture
Code Camp 06   Model View Presenter ArchitectureCode Camp 06   Model View Presenter Architecture
Code Camp 06 Model View Presenter Architecturebitburner93
 
ASP.NET MVC Fundamental
ASP.NET MVC FundamentalASP.NET MVC Fundamental
ASP.NET MVC Fundamentalldcphuc
 
Building xamarin.forms apps with prism and mvvm
Building xamarin.forms apps with prism and mvvmBuilding xamarin.forms apps with prism and mvvm
Building xamarin.forms apps with prism and mvvmMike Melusky
 
Understanding angular js
Understanding angular jsUnderstanding angular js
Understanding angular jsAayush Shrestha
 
SUE AGILE MVVM (English)
SUE AGILE MVVM (English)SUE AGILE MVVM (English)
SUE AGILE MVVM (English)Sabino Labarile
 
Mvvm in the real world tccc10
Mvvm in the real world   tccc10Mvvm in the real world   tccc10
Mvvm in the real world tccc10Bryan Anderson
 
Client Side MVC & Angular
Client Side MVC & AngularClient Side MVC & Angular
Client Side MVC & AngularAlexe Bogdan
 
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...Svetlin Nakov
 

Similar to Silverlight 3 Best Practice Do's and Don'ts (20)

MVC Pattern. Flex implementation of MVC
MVC Pattern. Flex implementation of MVCMVC Pattern. Flex implementation of MVC
MVC Pattern. Flex implementation of MVC
 
Fundaments of Knockout js
Fundaments of Knockout jsFundaments of Knockout js
Fundaments of Knockout js
 
MVC Training Part 2
MVC Training Part 2MVC Training Part 2
MVC Training Part 2
 
Test
TestTest
Test
 
MVC in PHP
MVC in PHPMVC in PHP
MVC in PHP
 
Building an enterprise app in silverlight 4 and NHibernate
Building an enterprise app in silverlight 4 and NHibernateBuilding an enterprise app in silverlight 4 and NHibernate
Building an enterprise app in silverlight 4 and NHibernate
 
MVC ppt presentation
MVC ppt presentationMVC ppt presentation
MVC ppt presentation
 
Code Camp 06 Model View Presenter Architecture
Code Camp 06   Model View Presenter ArchitectureCode Camp 06   Model View Presenter Architecture
Code Camp 06 Model View Presenter Architecture
 
ASP.NET MVC Fundamental
ASP.NET MVC FundamentalASP.NET MVC Fundamental
ASP.NET MVC Fundamental
 
Jinal desai .net
Jinal desai .netJinal desai .net
Jinal desai .net
 
Building xamarin.forms apps with prism and mvvm
Building xamarin.forms apps with prism and mvvmBuilding xamarin.forms apps with prism and mvvm
Building xamarin.forms apps with prism and mvvm
 
Understanding angular js
Understanding angular jsUnderstanding angular js
Understanding angular js
 
MVC
MVCMVC
MVC
 
MVC & backbone.js
MVC & backbone.jsMVC & backbone.js
MVC & backbone.js
 
SUE AGILE MVVM (English)
SUE AGILE MVVM (English)SUE AGILE MVVM (English)
SUE AGILE MVVM (English)
 
Mvvm in the real world tccc10
Mvvm in the real world   tccc10Mvvm in the real world   tccc10
Mvvm in the real world tccc10
 
MVC 4
MVC 4MVC 4
MVC 4
 
Client Side MVC & Angular
Client Side MVC & AngularClient Side MVC & Angular
Client Side MVC & Angular
 
MVC Training Part 1
MVC Training Part 1MVC Training Part 1
MVC Training Part 1
 
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
 

Recently uploaded

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 

Recently uploaded (20)

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 

Silverlight 3 Best Practice Do's and Don'ts

  • 1. SMTC WenemRmnrnr XNXNXNNXNX SNS, EndndjHsmnwmd ndvbsn@infusion.com August 29, 2007 Silverlight 3 Best Practice Do’s and Don’ts
  • 3. Design Patterns Development Optimizations Overview
  • 5. Model View Controller/Presenter Pattern Problem: Decoupling data display logic from the business logic that maintains and updates it Solution: MVC Pattern separates the modeling of the domain, presentation, and actions based on user input into three separate classes: Model: Manages behavior/data of the application domain responds to requests for information about its state (usually from View) responds to instructions to change its state (usually from Controller). View: Manages the display information Controller: Informs the Model to change its state by interpreting inputs from keyboard, mouse and external instructions.
  • 6. Developed by Martin Fowler, and is an extension of MVP Again, the View is separated from its behavior and state A Presentation Model is introduced, which acts as an abstraction of the view – so the view is merely a rendering of the Presentation Model View has a reference to Presentation Model, but Presentation Model has no reference to view Presentation Model
  • 7. MVP/MVC nice for web and thick client Good for ASP.NET,WinForms, WPF (as a start) Good for Silverlight as a start What you’re used to
  • 8. Identical to Presentation Model, but engineered by John Gossman to utilize WPF Dependency Properties and Commands to bind UI to abstraction Ideal as it allows abstraction, called the ViewModel, to be tested Also allows for any UI to bind to ViewModel fields to show and modify the data XAML controls / Elements bind to properties in the ViewModel class Use Model-View-ViewModel Pattern
  • 9. Model POCO – no Dependency Properties / Routed Events No UI knowledge - pure data Design Patterns: MVVM
  • 10. View Model No knowledge of the view Exposes the model(s) / custom properties used in the view Use ObservableCollection<T> for collections of data Implement INotifyPropertyChanged for custom properties Should be able to act as a command line interface to the view No Dependency Properties Design Patterns: MVVM
  • 11. View Binds to the ViewModel (DataContext) Use converters to bind visual elements to ViewModel / Model properties ie: Model’s Temperature  Colour Brush Events are hooked up through either code-behind or commanding Commanding better, but no OOB support in SL Silverlight Extensions project on CodePlex Custom Behaviors Composite Application Guidance/Library (Prism) Design Patterns: MVVM
  • 12. View and ViewModel Binding ViewModel.cs App.xaml.cs ModelData md; public ViewModel() { md=new ModelData(); } public string Data { get{ return md.Data;} set{ md.Data = value;} } public App() { View view = new View(); view.DataContext= new ViewModel(); //… } View.xaml ... <TextBox Content=“{Binding Data}” ... /> ...
  • 13. Can use “regular” property procedures in ViewModel If binding a collection to a DataGrid, etc., prefer the ObservableCollection<> (or non-generic equivalent) to sync updates back to the Model automatically MVVM Data Binding
  • 14. MVVM Collection Item Binding The View should never bind to the data directly, but bind to a ViewModel If a collection of data is to be displayed in the View, the main ViewModel should expose an ObservableCollection of an additional ViewModel class that exposes properties, and thus, data Property ... Property ... Property ... Property ... Property ... Property ... public ObservableCollectionMyOC ...{ return new ObservableCollection<ViewModel2> ... } Property ... Property ... Property ... <DataGridItemsSource=“ {Binding MyOC}” .../> Property ... Property ... Property ... Property ... Property ... Property ... ViewModel2 The View ViewModel1
  • 15. Issue: changing property values in ViewModel does not always update bound control in View Solution: have ViewModel implement INotifyPropertyChanged interface, and raise PropertyChanged event in each property Or better yet, create a base ViewModel class and have each ViewModel subclass it Property Binding
  • 16. Attached Properties in XAML can also be used in lieu of missing Command Create Command, expose as property in ViewModel Create static class with Attached Property Add namespace to XAML screen Place Attached Property in desired control Commands and Attached Properties
  • 18. ViewModel and XAML with Attached Property
  • 19. The View knows about the ViewModel, but the ViewModel does not know about the View ViewModel is assigned to the DataContext of the View (Silverlight XAML) In xaml.cs codebehind for page In App.xaml.cs In XAML via Resource Other Associating ViewModel with View
  • 20. Create ViewModel base class (implement INotifyPropertyChanged) Have all ViewModels subclass base Expose NO models to View directly Use Attached Properties as ‘workaround’ to lack of Commands in Silverlight controls Possibly Behaviors … Good practices …
  • 22. Easy to get sloppy – name all controls/elements UI elements should have the same naming convention as your private class variables (that’s what they become) ie: <Button x:Name=“_submitButton” /> Prefix every resource with a “namespace” “Storyboards.FlashUserImage” “Brushes.ButtonBackground” “Templates.GiftsListBox” Development
  • 23.
  • 27. [CustomValidation(typeof(MyValidator, “MyValidationMethod”))Can be applied at class or property level Data Annotations are your friend
  • 28.
  • 29. Use a method name convention (use a constant)Data Annotations
  • 30.
  • 38. Use Visibility rather than Opacity to hide objects Use transparent control backgrounds sparingly Avoid long-running JavaScript tasks and more Performance Tips …
  • 39. Never make the user wait and wonder Progress, progress, progress Minimize XAP size (this initial download) Make services prime, but remember Progress, progress, progress User interaction
  • 40. Resizing Blending Drawing resultant pixels onscreen ALL can be done with GPU acceleration enabled In <object> tag <parm name=“enableGPUAcceleration” value=“true” /> Hardware optimization

Editor's Notes

  1. Introduced in .NET 3.5 SP1, further support in .NET 4.0 Allows you to annotate your classes with what it means for an instance to be valid Place it on properties When value is set, annotations get evaluated and an exception is thrown if they fail Silverlight 3 controls automatically react to exceptions and display an error state (a visual state)
  2. Note the order is important