• No results found

Windows Forms 2.0 Data Binding. Informatik IT-Uddannelse og IT-Udvikling

N/A
N/A
Protected

Academic year: 2021

Share "Windows Forms 2.0 Data Binding. Informatik IT-Uddannelse og IT-Udvikling"

Copied!
31
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Morten Mertner

ƒ Senior Consultant, Teknologisk Institut

[email protected] / [email protected]

ƒ Architect and Software Developer

ƒ Instructor

ƒ Speaker (conferences and gatherings)

ƒ MCSD.NET, MCT

ƒ Open Source

- Gentle.NET (object persistence framework / object-relational mapper) - MbUnit (unit test framework)

(3)

Agenda

ƒ Data Binding - Concepts - Interfaces ƒ Demonstration - Gentle 2.0

- Data Binding with Windows Forms 2.0 (Brian Noyes)

(4)

Concepts – Data Binding

ƒ What is data binding?

- semi-automatic transfer of object state

- synchronized state between multiple objects

- easy wiring between UI controls and data/business objects

ƒ Why do we need it?

- productivity (less code to write, maintain and understand) - reduced complexity

- intuitive programming model - consistent results

(5)

Concepts – Types of Data Binding

ƒ Simple data binding - single value

ƒ Complex data binding - multiple values - collections

ƒ Data binding information

- direction (one-way or two-way) - timing (when is data transferred)

(6)

Concepts – Data Binding ”roles”

ƒ Entity

- single object

ƒ Collection

- container class for storing entities

ƒ Control

(7)

Interfaces

ƒ How it works

- we enable support for data binding by implementing interfaces - most are optional

- functionality/behavior depends on which interfaces we implement

ƒ Implementation

- interfaces for entities - interfaces for collections - interfaces for controls

(8)

Interfaces – Entities

ƒ IEditableObject

ƒ INotifyPropertyChanged

ƒ IDataErrorInfo

(9)

Interfaces – IEditableObject

ƒ Purpose

- support for transactional editing of objects (deferring commit until editing is completed)

- entities should maintain internal object state (new or dirty)

- allows validation to use final state rather than individual values - use when object has co-dependent properties

ƒ Implementation Effort

- code can be placed in base class - low effort

(10)

Interfaces – INotifyPropertyChanged

ƒ Purpose

- notify a container class when a property changes (this allows the list to raise a ListChanged event)

- performance optimization (less use of reflection by collection class) - flexibility (other code may benefit from events as well)

- semi-optional (collection can implement IRaiseItemChangedEvents instead, providing slightly less functionality)

ƒ Implementation Effort

- an event must be raised whenever a property is modified - code is difficult to put in base class (affects all properties) - high effort

(11)

Interfaces – IDataErrorInfo

ƒ Purpose

- support for storing and exposing error information - exposes a top-level error (for the entire object)

- exposes individual errors (for each property) using an indexer - can be used by controls (e.g. DataGridView) to highlight errors

ƒ Implementation Effort

- code can be placed in base blass

- validation is obviously custom code (but not specific to data binding) - low effort (excl. validation code)

(12)

Interfaces – ICustomTypeDescriptor

ƒ Purpose

- allows an object to provide custom property information - increase performance (by avoiding reflection)

- increase flexibility (by customizing which properties are exposed) - optional (unless you want to hide properties, etc.)

ƒ Implementation Effort

- code can be placed in base class

- code may require additional helper classes (e.g. attributes) - medium effort

(13)

Interfaces – Collections

ƒ IEnumerable<T> ƒ IEnumerator<T> ƒ ICollection<T> ƒ IList<T> ƒ IListSource ƒ ITypedList ƒ IBindingList<T> ƒ IBindingListView ƒ ICancelAddNew

(14)

Interfaces – IEnumerable<T> (and IEnumerator<T>)

ƒ Purpose

- enable iteration of items in collection

- helper class allows for more than one iteration strategy

- support for one-way data binding when used with BindingSource

ƒ Implementation Effort

- code can be placed in base collection class - easy to implement

(15)

Interfaces – ICollection<T>

ƒ Purpose

- enable direct access to items in a collection - modify the collection (add/remove items)

- count items, copy to array, synchronization/locking - extends IEnumerable<T>

ƒ Implementation Effort

- code can be placed in base collection class - easy to implement

(16)

Interfaces – IList<T>

ƒ Purpose

- enable positional access to items in a collection (indexing) - support for complex data binding

- extends ICollection<T>

ƒ Implementation Effort

- code can be placed in base collection class - easy to implement

(17)

Interfaces – IListSource

ƒ Purpose

- find out whether a collection contain entities or other collections - select the default collection for data binding

- obtain a collection for data binding from objects that are not collections

ƒ Implementation Effort

- code can be placed in base class (when using reflection) - easy to implement

(18)

Interfaces – ITypedList

ƒ Purpose

- expose information on properties of the data binding source (entities) - allows for great flexibility

- information is exposed using the PropertyDescriptor class

ƒ Implementation Effort

- can be placed in base collection class - fairly easy to implement

(19)

Interfaces – IBindingList<T>

ƒ Purpose

- support for full-featured data binding - sorting

- searching

- control changes to the collection (allow new/edit/remove) - change notifications (events)

ƒ Implementation Effort

- can be implemented in base collection class - medium effort

(20)

Interfaces – IBindingListView

ƒ Purpose

- support for advanced (multi-property) sorting - support for filtering

- allows a custom collection to do the same as DataView

ƒ Implementation Effort

- can be implemented in base collection class - medium effort

(21)

Interfaces – ICancelAddNew

ƒ Purpose

- support for “transactional” adding or editing of items

- allows collection class to handle cancellation of operation

(removes the coupling otherwise needed between entity and collection)

ƒ Implementation Effort

- can be implemented in base collection class - low effort

(22)

Interfaces – IRaiseItemChangedEvents

ƒ Purpose

- collection will notify when property changes on contained items

- only works for changes made through PropertyDescriptor class (UI) - use when contained items do not implement INotifyPropertyChanged

ƒ Implementation Effort

- can be implemented in base collection class - low effort

(23)

Interfaces – Controls

ƒ ISupportInitialize

ƒ ISupportInitializeNotification

(24)

Interfaces – ISupportInitialize

ƒ Purpose

- allows a control to wait until all changes (to a data-bound object) have been committed

- allows a control to remain ignorant of co-dependent properties

ƒ Implementation Effort

- code can be placed in base control - low effort

(25)

Interfaces – ISupportInitializeNotification

ƒ Purpose

- notification of interdependent objects when initialization is complete - allows for complex initialization scenarios

ƒ Implementation Effort - low effort

(26)

Interfaces – ICurrencyManagerProvider

ƒ Purpose

- lets a control provide its own currency manager - implemented by BindingSource

- you should not implement this interface (but might use it to access the CurrencyManager for a data-bound collection within a control)

ƒ Implementation Effort - not required

(27)

Demonstration

ƒ Gentle 2.0

- Entity base class

- ListBase<TEntity> collection class - Sample project

(28)

Demonstration

ƒ Entity base class for (persistent) business objects

ƒ Data binding interfaces

- INotifyPropertyChanged (raise events when properties are modified) - IEditableObject (event handlers for data binding events)

- IDataErrorInfo (error tracking)

ƒ Custom interfaces

- IEntity (object identity and persistency state)

(29)

Demonstration

ƒ ListBase<TEntity> collection class - inherits from BindingList<T>

ƒ Data binding interfaces - BindingList<T> - IBindingListView

- IRaiseItemChangedEvents - ITypedList

(30)

Conclusion

ƒ IList is the minimum requirement

- BindingSource transforms an IEnumerable to an IList

ƒ IBindingList should be the minimum for collections

- IBindingListView adds advanced functionality known from DataView

ƒ INotifyPropertyChanged is important for rich data-binding (also detects programmatic changes to properties)

ƒ PropertyDescriptor class is the mediator between properties and controls, and is used when a value is modified through the UI)

(31)

References

Related documents

* This paper is presented to the 2nd KRIS-Brookings Joint Conference on &#34;Security and Diplomatic Cooperation between ROK and US for the Unification of the

The five factors that to measure employees’ retention intention were compensation, work-life balance, organizational commitment, career opportunity and supervisor

Investigating the Relationship between Ethical Leadership and Deviant Behaviors in the Workplace: The Mediating Role of Emotional Commitment and Moral Moral, Organizational

and globally, remain on the negative side of the digital divide. This age-based digital divide is of concern because the internet enables users to expand their

     •  Location of event      •  Date(s) of event  Webcasting

Communication apprehension in a first language and self-perceived competence as predictors of communica- tion apprehension in a second language: A study of speakers of English as

shoulders. What Trible believes this tells us about God is interesting, and probably the biggest logical hurdle of the piece. If, as she previously outlined, the “male and

But if experience does not have, in addition to its subjective character, an objective nature that can be apprehended from many different points of view, then how can it be