WPF and Databases
-
Upload
doncho-minkov -
Category
Technology
-
view
1.909 -
download
9
Transcript of WPF and Databases
![Page 1: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/1.jpg)
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
![Page 2: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/2.jpg)
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
![Page 3: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/3.jpg)
Data Access LayerData Access Layer
![Page 4: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/4.jpg)
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
![Page 5: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/5.jpg)
Data Access LayerData Access LayerLive DemoLive Demo
![Page 6: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/6.jpg)
Accessing the Data Accessing the Data LayerLayer
![Page 7: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/7.jpg)
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>
![Page 8: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/8.jpg)
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}">
![Page 9: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/9.jpg)
Accessing the Data Accessing the Data LayerLayerLive DemoLive Demo
![Page 10: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/10.jpg)
Master DetailMaster Detail
![Page 11: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/11.jpg)
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}"/>
![Page 12: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/12.jpg)
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
![Page 13: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/13.jpg)
Master DetailMaster DetailLive DemoLive Demo
![Page 14: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/14.jpg)
Data PersisterData PersisterThe way to collect informationThe way to collect information
![Page 15: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/15.jpg)
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
![Page 16: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/16.jpg)
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
![Page 17: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/17.jpg)
Data PersisterData PersisterLive DemoLive Demo
![Page 18: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/18.jpg)
PagingPagingWhat is paging and why we need it?What is paging and why we need it?
![Page 19: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/19.jpg)
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?
![Page 20: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/20.jpg)
PagingPaging TODOTODO
![Page 21: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/21.jpg)
PagingPagingLive DemoLive Demo
![Page 22: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/22.jpg)
WPF and DatabasesWPF and DatabasesWith MVVMWith MVVM
![Page 23: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/23.jpg)
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>
![Page 24: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/24.jpg)
Categories EditorCategories EditorLive DemoLive Demo
![Page 25: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/25.jpg)
QuestionsQuestions??
WPF and DatabasesWPF and Databases
![Page 26: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/26.jpg)
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
![Page 27: WPF and Databases](https://reader035.fdocuments.net/reader035/viewer/2022062319/554ebcd6b4c9053c4b8b4702/html5/thumbnails/27.jpg)
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