9. XAML & WPF - WPF and Databases


Published on

WPF and Databases
Telerik Software Academy:
The website and all video materials are in Bulgarian.

Data Access Layer
Accessing the Data Layer
Master Detail
Data Persister
WPF and Databases

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

9. XAML & WPF - WPF and Databases

  1. 1. WPF and Databases LINQ-to-Entities, Data Persisters, Master DatailDoncho MinkovTechnical Trainerhttp://www.minkov.itTelerik Software Academyacademy.telerik.com
  2. 2. 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
  3. 3. Data Access Layer
  4. 4. 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
  5. 5. Data Access Layer Live Demo
  6. 6. Accessing the Data Layer
  7. 7. 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>
  8. 8. Accessing DAL After instantiating the EntityModel we can simply bind some of the entities to a ItemCollection  ListBox, ListView, TreeView, etc. <TreeView x:Name="TreeViewCategories" ItemsSource="{Binding Categories}"> In MVVM it is not possible to access the DAL directly  Done through the ViewModel  In MVVM we need a DataPersister object
  9. 9. Accessing the Data Layer Live Demo
  10. 10. Master Detail
  11. 11. 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}"/>
  12. 12. 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
  13. 13. Master Detail Live Demo
  14. 14. Data PersisterThe way to collect information
  15. 15. 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
  16. 16. The DataPersister The DataPersisteris 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
  17. 17. Data Persister Live Demo
  18. 18. PagingWhat is paging and why we need it?
  19. 19. 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 applicationretrieves 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?
  20. 20. Paging TODO
  21. 21. PagingLive Demo
  22. 22. WPF and Databases With MVVM
  23. 23. WPF and Databases Consider the usual case  You have categories with products in a Database  CRUD operations should be implemented You have the following:<Grid DataContext="{Binding Person}"> <TextBox Text="{Binding Name}"/> <TextBox Text="{Binding Age}"/></Grid> When selecting the DataContext of the Grid  You get a Person object
  24. 24. Categories Editor Live Demo
  25. 25. WPF and Databases курсове и уроци по програмиране, уеб дизайн – безплатно BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране – Телерик академия форум програмиране, форум уеб дизайн уроци по програмиране и уеб дизайн за ученици ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET http://academy.telerik.com програмиране за деца – безплатни курсове и уроци ASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC безплатен SEO курс - оптимизация за търсачки алго академия – състезателно програмиране, състезаниякурсове и уроци по програмиране, книги – безплатно от Наков курс мобилни приложения с iPhone, Android, WP7, PhoneGap уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop Дончо Минков - сайт за програмиране free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране безплатен курс "Качествен програмен код" безплатен курс "Разработка на софтуер в cloud среда" C# курс, програмиране, безплатно
  26. 26. 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
  27. 27. 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
  28. 28. Free Trainings @ Telerik Academy Desktop Applications with C# and WPF  academy.telerik.com/ Telerik Software Academy  academy.telerik.com Telerik Academy @ Facebook  facebook.com/TelerikAcademy Telerik Software Academy Forums  forums.academy.telerik.com