joi, 20 mai 2010

Patterns Series

Patterns Series



Adapter Pattern

Supervising Pattern and Presentation Pattern

Supervising Pattern and Presentation Pattern

About

There are specialization of Model-View-Presenter
In this model there are 3 components involved:
  • UI
    • it is around the notion of View
    • sometimes it contains login in the view's code-behind file.
    • in WPF case, the pure UI: the view may be declaratively defined in XAML as a data template or control with no code-behind
  • Presentation
    • the application logic that is concerned with the application's use cases (or user stories) and that defines the logical behavior and structure of the application and that is independent of any specific user interface implementation
    • no direct reference to any UI control
  • Business Logic
    • any application logic that is concerned with the retrieval and management of application data and for making sure that any business rules that ensure data consistency and validity are imposed
    • should not contain any behavior or application logic that is specific to a use case or user story

Use to maximize testability.

Supervising Pattern

  • It is known,and, as the Supervising Controller pattern
  • The view class:
    • uses data binding to retrieve and update data directly from the model.
    • manages the controls on the user interface and forwards user events to a presenter class. 
    • manages the user interface controls and encapsulates any visual state or behavior that is specific to the UI
    • the view is updated through presenter and data binding
    • it is like of an observer of the model
  • The presenter 
    • coordinates any additional interaction between the view and the model that cannot be achieved through simple data binding
    • encapsulates presentation logic that interprets user gestures into actions against the model
    • communicates with the View through a interface (good for testability)
    • contains the logic to respond to the events, update the model 
  • Model
    • To support data binding, the model should implement the INotifyPropertyChanged interface.
    • Any objects provided to the view through properties should similarly support the INotifyPropertyChanged or INotifyCollectionChanged interfaces



Presentation Pattern

  • Provides a façade over the model and coordinates all of the view's interaction with it
  • The data that you want to display requires some form of conversion or adaptation before it can be displayed in the user interface.
  • The data that you want to display requires some form of validation before it can be updated by the user interface. 
  • The view acts as an observer of the Presentation Model, and the Presentation Model acts as an observer of the Model
  • The presentation model
    • should be designed to provide a public interface that can be easily consumed by a view
    • should be designed to support data binding, commands, and data templates.
      • Data Binding:
        • Presentation can be bound to app data
      • Commands
        • allow the user interface to interact with the application's functionality in a declarative and automatic way without requiring any complex event handling code in the view's code-behind file.
        • can be invoked as the user interacts with the UI, and the UI can be automatically updated as the underlying command becomes enabled or disabled.
        • WPF controls can be easily associated with a command through the Command property
        • The Composite Application Library provides the DelegateCommand and CompositeCommand classes to make this easy to do
      • A data template declares user interface controls that will be data bound at run time to the underlying object.

<ListBox.ItemTemplate>
  <DataTemplate>
     <StackPanel>
       <TextBlock Text="{Binding Path=TaskName}" /> 
       
<TextBlock Text="{Binding Path=Description}"/>
       
<TextBlock Text="{Binding Path=Priority}"/>
     </StackPanel>
   </DataTemplate>
</ListBox.ItemTemplate>






Model-View-View Model



WPF View: 
- should be as much as possible only XAML. 

View Model:
  • it is the DataContext for the WPF View
  • it will contain the logic
  • exposes public properties that WPF view will bind to
    • Dependency properties: for individual property like text, enable
    • ObservableCollection: for controls like ListBoxes, GridViews


Model: 
- the source of data

References:

Composite Application Guidance for WPF and Silverlight - October 2009
http://martinfowler.com/eaaDev/SupervisingPresenter.html

[DesignPattern] State Pattern

State Pattern


  • When the state inside an object changes, it can change its behavior by switching to a set of different operations
  • The Context represents information that is mostly fixed, whereas the State can change between the available options as the program progresses
  • The State pattern relies on simple mechanisms: interfaces and aggregation.
  • The States can be classes, or if they are quite simple—without much data— they can be implemented as delegates.






Elements:
  • Context: A class that maintains an instance of a State that defines the current context and the interface of interest to clients
  • IState: Defines an interface for a particular state of the Context
    - The State has full access to the data in the Context.
    - At any point, either the Context or the active State can decide that it is time to switch states.
  • StateA and StateB: Classes that implement behavior associated with a state of the Context

miercuri, 12 mai 2010

[1][3][WPF]Visual Tree - Logical Tree

[1][3][WPF]Visual Tree - Logical Tree

Two element trees exist within a WPF application: the logical tree and the visual tree
  • The logical tree is the hierarchical structure containing the exact elements of your WPF application as defined either declaratively within a XAML file, or imperatively in code
    • The logical tree outlines the 1:1 mapping of the nested XAML elements declared in the code snippet to their appropriate classes in the WPF API.
    • The LogicalTreeHelper class provides methods such as GetChildren and GetParent for querying the tree object to extract a list of child collections of elements from the tree of object elements.
    • _buttonCtrl.FindName(“_textCtrl");
  • Visual Tree
    • For each element in the logical tree, additional elements may be created to represent visual aspects required by the element. The combination of application elements and the elements created for visualization support is known as the visual tree.
    • Not all logical tree nodes appear in the visual tree; only the elements that derive from System.Windows.Media.Visual or System.Windows.Media.Visual3D are included
  • You can access both the logical and visual trees from code
    • You can easily traverse both the logical and visual trees using the somewhat symmetrical System.Windows.LogicalTreeHelper and System.Windows.Media.VisualTreeHelper classes
  • LogicalTreeHelper.GetChildren(obj as DependencyObject)
  • OnContentRendered, which doesn’t get called until after layout occurs
  • Visual class contains three protected members (VisualParent, VisualChildrenCount, and GetVisualChild) for examining its visual parent and children.


Sample:

static void PrintLogicalTree(int i_depth, object i_object)
{
// Print the object with preceding spaces that represent its depth
Debug.WriteLine(new string(' ', i_depth) + i_object);
// Sometimes leaf nodes aren’t DependencyObjects (e.g. strings)
if (!(i_object is DependencyObject)) return;
// Recursive call for each logical child
foreach (object child in LogicalTreeHelper.GetChildren(i_object as DependencyObject))
{
PrintLogicalTree(i_depth + 1, child);
}
}

static void PrintVisualTree(int i_depth, DependencyObject i_obj)
{
// Print the object with preceding spaces that represent its depth
Debug.WriteLine(new string(' ', i_depth) + i_obj);
// Recursive call for each visual child
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(i_obj); i++)
{
PrintVisualTree(i_depth + 1, VisualTreeHelper.GetChild(i_obj, i));
}
}

marți, 11 mai 2010

Debugging - Tips& Tricks

Debugging - Tips& Tricks 


Breakpoints
  • F10 - Step Over (execute, for example, a method at a time)
  • F11 - Step Into (the debugger will go inside the method)
  • Shift - F11 - Step Out (wthin the current method, the execution will complete and will pause at the next statement from where it called)
  • F5 - continue
  • Ctrl + Shit + F10 - Set Next Statement (set execution path to a new line) - option available on the right click, also
  • Ctrl + * - Show Next Statement
  • Conditional breakpoint - set a condition on the breakpoint when you want it to be reach (Right click on breakpoint -> Condition...)
    • Run a Macro
  • Labeling in Break Point (new feature of VS 2010) - in Breakpoint tabs or on breakpoint in code - right click on breakpoint -> Edit Labels...
  • Import/Export Breakpoints (From Breakpoint tab) - there are saved in XML format
  • Breakpoint Filter
  • Last session breakpoint value (VS2010)


DebuggerDisplayAttribute Class 
  • Determines how a class or field is displayed in the debugger variable windows.
    [DebuggerDisplay("Count = {Count}")] class Stat { public int Count {get; set; } }
  • [DebuggerBrowsable(DebuggerBrowsableState.Never)] - the following DebuggerBrowsableAttribute prevents the property following it // from appearing in the debug window for the class.

Output Window:

Highlighting References [C# 4.0]

  • When you click a symbol in source code, all instances of that symbol are highlighted in the document. To move to the next or previous highlighted symbol, you can use CTRL+SHIFT+DOWN ARROW or CTRL+SHIFT+UP ARROW. For more information, see How to: Use Reference Highlighting.



Watch Windows:
  • Local (Ctrl-Alt-V, L)
  • Autos (Ctrl-Alt-V, A)
  • Watch (Ctrl+D, W)
  • Immediate Window (Ctrl-Alt-I)
  • Call Stack
  • Breakpoints window (Ctrl-Alt-B)
    • Ctrl-Shift-F9 - Clear all breakpoints in the project
    • Ctrl-F9 - Enable/Disable a breakpoint
  • Quick Watch Dialog (Ctrl-Alt-Q)
  • Treads Window
    • Main tread has a green rectangle
  • Processes Dialog (Ctrl-Alt-P)
  • Modules
  • IntelliTrace Window
  • Call Hierarchy window (VS 2010)
    • Call Hierarchy (available in C# and C++) enables you to navigate through your code by displaying all calls to and from a selected method, property, or constructor. 

miercuri, 5 mai 2010

WPF Content

WPF Content


  1. WPF - About
    1. Routed Events
    2. Commands
    3. Visual Tree - Logical Tree
    4. Mouse Inputs
  2. Comparison: WPF - Windows Forms
  3. WPF Architecture
    1. System.Windows.DependencyObject
    2. System.Threading.DispatcherObject
      1. DispatcherTimer
  4. XAML Overview
  5. WPF Elements
    1. Items Controls
    2. Window
    3. Application Object
  6. Layout
  7. Panel
    1. ItemsControl

  8. Resources
  9. Data Binding
    1. Converters
    2. WPF ViewTree with TreeViewItems of Different Types
  10. Styles, Templates, Skins, and Themes

Others:
  • TaskDialog
  • XAML Browser Applications (XBAP)
  • Interoperability
  • Embedding Windows Forms Controls in WPF Applications
  • Embedding WPF Controls in Windows Forms Applications