wpf and databases
TRANSCRIPT
WPF and DatabasesWPF and DatabasesLinq-to-Entities, Data Persisters, Linq-to-Entities, Data Persisters,
Master DatailMaster Datail
Doncho MinkovDoncho Minkov
Telerik School Telerik School AcademyAcademyhttp://schoolacademy.telerik.com
Technical TrainerTechnical Trainerhttp://www.minkov.it
Table of ContentsTable of Contents
Data Access LayerData Access Layer Linq-to-EntitiesLinq-to-Entities Linq-to-XMLLinq-to-XML
Accessing the Data LayerAccessing the Data Layer DataContext DataContext
Master DetailMaster Detail Data PersisterData Persister PagingPaging WPF and DatabasesWPF and Databases
Data Access LayerData Access Layer
Data Access LayerData Access Layer The Data Access Layer is a The Data Access Layer is a
essentialessential Used for access to the databaseUsed for access to the database
Class Library with single Class Library with single EntityModel EntityModel classclass
This way we can use the same DAL This way we can use the same DAL with many applicationswith many applications
Data Access LayerData Access LayerLive DemoLive Demo
Accessing the Data Accessing the Data LayerLayer
The DataContextThe DataContext When adding a reference to the DAL When adding a reference to the DAL
projectproject We can instantiate an entity model We can instantiate an entity model
in the XAMLin the XAML
And set it for And set it for DataContextDataContext<Window <Window … … xmlns:datalayer="xmlns:datalayer=" clr-namespace:NorthwindDataLayer;assembly=clr-namespace:NorthwindDataLayer;assembly= NorthwindDataLayer">NorthwindDataLayer"> <Window.DataContext> <Window.DataContext> <datalayer:NorthwindEntities /><datalayer:NorthwindEntities /> </Window.DataContext></Window.DataContext>
Accessing DALAccessing DAL After instantiating the After instantiating the EntityModel EntityModel
we can simply bind some of the we can simply bind some of the entities to a entities to a ItemCollectionItemCollection ListBoxListBox, , ListViewListView, , TreeViewTreeView, etc., etc.
In MVVM it is not possible to access In MVVM it is not possible to access the DAL directlythe DAL directly Done through the ViewModelDone through the ViewModel
In MVVM we need a In MVVM we need a DataPersister DataPersister objectobject
<TreeView x:Name="TreeViewCategories" <TreeView x:Name="TreeViewCategories" ItemsSource="{Binding Categories}">ItemsSource="{Binding Categories}">
Accessing the Data Accessing the Data LayerLayerLive DemoLive Demo
Master DetailMaster Detail
Master DetailMaster Detail What is Master Detail?What is Master Detail?
When selecting a CategoryWhen selecting a Category
Show category infoShow category info
Show products in this categoryShow products in this category<ListView Name="ListViewFamilies" <ListView Name="ListViewFamilies" ItemsSource="{Binding}" ItemsSource="{Binding}" DisplayMemberPath="FamilyName"/>DisplayMemberPath="FamilyName"/><ListView DisplayMemberPath="Name"<ListView DisplayMemberPath="Name" ItemsSource="{Binding ElementName=ListViewFamilies, ItemsSource="{Binding ElementName=ListViewFamilies, Path=SelectedItem}"/>Path=SelectedItem}"/>
Master Datail in MVVMMaster Datail in MVVM Consider we have the property Consider we have the property
CategoriesCategories in the ViewModel in the ViewModel Somehow it gets data from the Somehow it gets data from the
ModelModel
Since Categories is a Since Categories is a IEnumerable IEnumerable we can get its we can get its ICollectionViewICollectionView
Now we got the Now we got the CurrentItemCurrentItem
Can make property Can make property ProductsProducts that that returns the products of the returns the products of the CurrentItemCurrentItem
Master DetailMaster DetailLive DemoLive Demo
Data PersisterData PersisterThe way to collect informationThe way to collect information
What is a Data What is a Data Persister?Persister?
The Data Persister is the object that The Data Persister is the object that gives data to the Modelgives data to the Model Sometimes it is merged with the Sometimes it is merged with the
ModelModel
It could be an It could be an EntityDataModelEntityDataModel
It could be a It could be a Linq-to-XMLLinq-to-XML object object
It could just an object we createdIt could just an object we created How the Data Persister helps?How the Data Persister helps?
Easier for mocking (unit testing)Easier for mocking (unit testing)
Better reusabilityBetter reusability
The DataPersisterThe DataPersister The DataPersister is the class that The DataPersister is the class that
makes the connection with the makes the connection with the Data (RDB, XML or object)Data (RDB, XML or object) Responsible for the CRUDResponsible for the CRUD
May have validationMay have validation With this object the Model is pretty With this object the Model is pretty
cleanclean Almost no programming logicAlmost no programming logic
Loose couplingLoose coupling
Data PersisterData PersisterLive DemoLive Demo
PagingPagingWhat is paging and why we need it?What is paging and why we need it?
What is Paging?What is Paging? A memory-management scheme A memory-management scheme
A computer can store and retrieve data from A computer can store and retrieve data from secondary storage for use in main memorysecondary storage for use in main memory
With paging the application retrieves data With paging the application retrieves data from secondary storage in same-size from secondary storage in same-size blocks called pagesblocks called pages
Why use paging?Why use paging? Imagine a Database with over a million Imagine a Database with over a million
recordsrecords
Store them in the dynamic memory at once?Store them in the dynamic memory at once?
Or take them by pieces with 100 records?Or take them by pieces with 100 records?
PagingPaging TODOTODO
PagingPagingLive DemoLive Demo
WPF and DatabasesWPF and DatabasesWith MVVMWith MVVM
WPF and DatabasesWPF and Databases Consider the usual caseConsider the usual case
You have categories with products in a You have categories with products in a DatabaseDatabase
CRUD operations should be implementedCRUD operations should be implemented You have the following:You have the following:
When selecting the DataContext of the When selecting the DataContext of the GridGrid You get a Person objectYou get a Person object
<Grid DataContext="{Binding Person}"><Grid DataContext="{Binding Person}"> <TextBox Text="{Binding Name}"/><TextBox Text="{Binding Name}"/> <TextBox Text="{Binding Age}"/><TextBox Text="{Binding Age}"/></Grid></Grid>
Categories EditorCategories EditorLive DemoLive Demo
QuestionsQuestions??
WPF and DatabasesWPF and Databases
ExercisesExercises1.1. Create a database holding continents, Create a database holding continents,
countries and town. Countries have name, countries and town. Countries have name, language, population and continent. Towns language, population and continent. Towns have name, population and country. have name, population and country. Implement a WPF application that shows Implement a WPF application that shows the continents in a the continents in a ComboBoxComboBox, countries in a , countries in a ListViewListView and the towns in a and the towns in a ListViewListView and and allows master-detail navigation. Use Entity allows master-detail navigation. Use Entity Framework. Use paging and sorting for the Framework. Use paging and sorting for the long lists. long lists.
2.2. Implement add / edit / delete for the Implement add / edit / delete for the continents, countries, towns and continents, countries, towns and languages. Handle the possible errors languages. Handle the possible errors accordingly. Implement validation logic.accordingly. Implement validation logic.
26
Exercises (2)Exercises (2)3.3. Add a flag for each country which Add a flag for each country which
should be a PNG image, stored in the should be a PNG image, stored in the database, displayed along with the database, displayed along with the country data. Implement "change flag" country data. Implement "change flag" functionality by uploading a PNG functionality by uploading a PNG image.image.
27