SlideShare una empresa de Scribd logo
1 de 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]

Más contenido relacionado

La actualidad más candente

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
Tech_MX
 

La actualidad más candente (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 a Stephen Kennedy Silverlight 3 Deep Dive

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
bwullems
 
ASP.NET MVC Fundamental
ASP.NET MVC FundamentalASP.NET MVC Fundamental
ASP.NET MVC Fundamental
ldcphuc
 
Mvvm in the real world tccc10
Mvvm in the real world   tccc10Mvvm in the real world   tccc10
Mvvm in the real world tccc10
Bryan Anderson
 

Similar a Stephen Kennedy Silverlight 3 Deep Dive (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,...
 

Último

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Último (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 

Stephen Kennedy Silverlight 3 Deep Dive

  • 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

Notas del editor

  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