WPF and Databases
description
Transcript of WPF and Databases
![Page 1: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/1.jpg)
WPF and DatabasesLinq-to-Entities, Data Persisters, Master Datail
Doncho Minkov
Telerik School Academyhttp://schoolacademy.telerik.com
Technical Trainerhttp://www.minkov.it
http://schoolacademy.telerik.com
![Page 2: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/2.jpg)
Table of Contents Data Access Layer
Linq-to-Entities Linq-to-XML
Accessing the Data Layer DataContext
Master Detail Data Persister Paging WPF and Databases
![Page 3: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/3.jpg)
Data Access Layer
![Page 4: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/4.jpg)
Data Access Layer The Data Access Layer is a essential Used for access to the database
Class Library with single EntityModel class
This way we can use the same DAL with many applications
![Page 5: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/5.jpg)
Data Access LayerLive Demo
![Page 6: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/6.jpg)
Accessing the Data Layer
![Page 7: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/7.jpg)
The DataContext When adding a reference to the DAL
project We can instantiate an entity model
in the XAML And set it for DataContext<Window … xmlns:datalayer=" clr-namespace:NorthwindDataLayer;assembly= NorthwindDataLayer"> <Window.DataContext> <datalayer:NorthwindEntities /> </Window.DataContext>
![Page 8: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/8.jpg)
Accessing DAL After instantiating the EntityModel
we can simply bind some of the entities to a ItemCollection ListBox, ListView, TreeView, etc.
In MVVM it is not possible to access the DAL directly Done through the ViewModel In MVVM we need a DataPersister
object
<TreeView x:Name="TreeViewCategories" ItemsSource="{Binding Categories}">
![Page 9: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/9.jpg)
Accessing the Data LayerLive Demo
![Page 10: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/10.jpg)
Master Detail
![Page 11: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/11.jpg)
Master Detail What is Master Detail?
When selecting a Category Show category info Show products in this category
<ListView Name="ListViewFamilies" ItemsSource="{Binding}" DisplayMemberPath="FamilyName"/><ListView DisplayMemberPath="Name" ItemsSource="{Binding ElementName=ListViewFamilies, Path=SelectedItem}"/>
![Page 12: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/12.jpg)
Master Datail in MVVM Consider we have the property Categories in the ViewModel Somehow it gets data from the
Model Since Categories is a IEnumerable
we can get its ICollectionView Now we got the CurrentItem Can make property Products that
returns the products of the CurrentItem
![Page 13: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/13.jpg)
Master DetailLive Demo
![Page 14: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/14.jpg)
Data PersisterThe way to collect information
![Page 15: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/15.jpg)
What is a Data Persister?
The Data Persister is the object that gives data to the Model Sometimes it is merged with the
Model It could be an EntityDataModel It could be a Linq-to-XML object It could just an object we created
How the Data Persister helps? Easier for mocking (unit testing) Better reusability
![Page 16: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/16.jpg)
The DataPersister The DataPersister is the class that makes the connection with the Data (RDB, XML or object) Responsible for the CRUD May have validation
With this object the Model is pretty clean Almost no programming logic Loose coupling
![Page 17: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/17.jpg)
Data PersisterLive Demo
![Page 18: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/18.jpg)
PagingWhat is paging and why we need it?
![Page 19: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/19.jpg)
What is Paging? A memory-management scheme
A computer can store and retrieve data from secondary storage for use in main memory
With paging the application retrieves data from secondary storage in same-size blocks called pages
Why use paging? Imagine a Database with over a
million records Store them in the dynamic memory
at once? Or take them by pieces with 100
records?
![Page 20: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/20.jpg)
Paging TODO
![Page 21: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/21.jpg)
PagingLive Demo
![Page 22: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/22.jpg)
WPF and DatabasesWith MVVM
![Page 23: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/23.jpg)
WPF and Databases Consider the usual case
You have categories with products in a Database
CRUD operations should be implemented
You have the following:
When selecting the DataContext of the Grid You get a Person object
<Grid DataContext="{Binding Person}"> <TextBox Text="{Binding Name}"/> <TextBox Text="{Binding Age}"/></Grid>
![Page 24: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/24.jpg)
Categories EditorLive Demo
![Page 25: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/25.jpg)
Questions?
WPF and Databases
![Page 26: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/26.jpg)
Exercises1. Create a database holding continents,
countries and town. Countries have name, language, population and continent. Towns have name, population and country. Implement a WPF application that shows the continents in a ComboBox, countries in a ListView and the towns in a ListView and allows master-detail navigation. Use Entity Framework. Use paging and sorting for the long lists.
2. Implement add / edit / delete for the continents, countries, towns and languages. Handle the possible errors accordingly. Implement validation logic.
26
![Page 27: WPF and Databases](https://reader036.fdocuments.us/reader036/viewer/2022062319/568160b2550346895dcfd381/html5/thumbnails/27.jpg)
Exercises (2)3. Add a flag for each country which
should be a PNG image, stored in the database, displayed along with the country data. Implement "change flag" functionality by uploading a PNG image.
27