PRN212Lab 02 ProductManagement Using EntityFramework and WPF
PRN212Lab 02 ProductManagement Using EntityFramework and WPF
PRN212Lab 02 ProductManagement Using EntityFramework and WPF
1. Introduction
Imagine you're an employee of a store named ProductStore. Your manager has asked you to
develop a WPF application for product management. The application has to support adding,
viewing, modifying, and removing products—a standardized usage action verbs better known as
Create, Read, Update, Delete (CRUD).
This lab explores creating an application using WPF with .NET Core, and C#. An SQL Server
Database will be created to persist the car's data that will be used for reading and managing
product data by Entity Framework Core
2. Lab Objectives
In this lab, you will:
Use the Visual Studio.NET to create WPF application and Class Library (.dll) project.
Create a SQL Server database named MyStoreDB that has a Product, Category,
AccountMember tables.
Apply Repository pattern in a project.
Add CRUD action methods to WPF application.
Run the project and test the WPF application actions.
1|Page
3. Database Design (MyStore)
Table AccountMember
Table Categories
Table Products
2|Page
Activity 01: Build a solution by Visual Studio.NET
Create a Blank Solution named ProductManagementDemo then add new a Class Library
project named BusinessObjects, DataAccessObjects, Repositories, Services and a WPF
project named ProductManagement
Note:
Data Source in this case is the SQL Server Database
Services Project – This project represents a layer or component responsible for
implementing the business logic of an application.
3|Page
Repository Project – This project provides an abstraction layer between the
application’s business logic and the underlying data source.
Data Access Layer Project – This project used to abstract and encapsulate the logic for
accessing data from a data source, such as a database.
4|Page
Activity 02: Write codes for the BusinessObjects project
Step 01. Install the following packages from NuGet:
Microsoft.EntityFrameworkCore.SqlServer --version 8.0.2
Microsoft.EntityFrameworkCore.Tools --version 8.0.2
5|Page
Microsoft.Extensions.Configuration.Json --version 8.0.0
Check the tool for EFCore (install/uninstall tool if needed) (dotnet SDK 8.0.202)
Step 02. Right-click on project , select Open In Terminal. On Developer PowerShell dialog
execute the following commands to generate model:
Implement ORM
using System.IO;
using Microsoft.Extensions.Configuration.Json;
6|Page
Activity 03: Write codes for the DataAccessLayer project
Step 01. On the DataAccessLayer project, add a class named CategoryDAO.cs and write codes
as follows:
7|Page
Step 02. On the DataAccessLayer project, add a class named ProductDAO.cs and write codes
as follows:
8|Page
9|Page
Step 03. Write codes for AccountDAO.cs as follows:
10 | P a g e
The details for GetConnectionString() and OnConfiguring() functions
11 | P a g e
Activity 04: Write codes for the Repositories project
Step 01. On the Repositories project, add an interface named ICatergoryRepository.cs and
write codes as follows:
12 | P a g e
Step 02. On the Repositories project, add an interface named IProductRepository.cs and write
codes as follows:
Step 03. On the Repositories project, add an interface named IAccountRepository.cs and write
codes as follows:
13 | P a g e
Step 05. Write codes for class ProductRepository.cs as follows:
14 | P a g e
Activity 05: Write codes for the Services project
Step 01. On the Services project, add an interface named ICatergoryService.cs and write codes
as follows:
15 | P a g e
Step 02. On the Services project, add an interface named IProductService.cs and write codes as
follows:
Step 03. On the Services project, add an interface named IAccountService.cs and write codes as
follows:
16 | P a g e
Step 05. Write codes for class ProductService.cs as follows:
17 | P a g e
Step 06. Write codes for class AccountService.cs as follows:
18 | P a g e
19 | P a g e
Activity 04: Design UI and write codes for WPF project
20 | P a g e
Step 01. On the WPF project, design UI as follows:
<Window x:Class="WPFApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFApp"
mc:Ignorable="d"
Loaded="Window_Loaded"
WindowStartupLocation="CenterScreen"
21 | P a g e
Title="Product Management" Height="670" Width="710">
<Grid>
<Grid>
<Grid.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="#0099FF" Offset="0"/>
<GradientStop Color="#FF347BDA" Offset="0.794"/>
<GradientStop Color="#FF60B1E7" Offset="1"/>
<GradientStop Color="#FF596FDD" Offset="0.309"/>
<GradientStop Color="#FF472FDE" Offset="0.484"/>
</LinearGradientBrush>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="119.415"/>
<ColumnDefinition Width="30.585"/>
<ColumnDefinition Width="47*"/>
<ColumnDefinition Width="513*"/>
</Grid.ColumnDefinitions>
22 | P a g e
<TextBox x:Name="txtUnitsInStock" Margin ="4,4,4,4" Grid.Column="2"
Grid.Row="4" Text="" TextWrapping="Wrap" Grid.ColumnSpan="2" />
</Grid>
</Grid>
</Window>
23 | P a g e
<Window x:Class="WPFApp.LoginWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFApp"
mc:Ignorable="d"
Title="LoginWindow" Height="450" Width="800"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
Background="Transparent"
AllowsTransparency="True">
<Grid>
<Border CornerRadius="10"
BorderThickness="2"
Opacity="0.95">
<Border.BorderBrush>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="#0099FF" Offset="0"/>
<GradientStop Color="#DA34AE" Offset="0.75"/>
<GradientStop Color="#FF60B1E7" Offset="1"/>
<GradientStop Color="#FF596FDD" Offset="0.309"/>
<GradientStop Color="#FF8C57CA" Offset="0.484"/>
</LinearGradientBrush>
</Border.BorderBrush>
<Border.Background>
<LinearGradientBrush StartPoint="0,1" EndPoint="1,0">
<GradientStop Color="#060531" Offset="0"/>
<GradientStop Color="#FF472FDE" Offset="1"/>
</LinearGradientBrush>
24 | P a g e
</Border.Background>
<Grid>
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center"
Height="82" VerticalAlignment="Top" Width="632">
<TextBlock Text="LOGIN WINDOW"
Foreground="White"
FontSize="28"
FontWeight="Medium"
FontFamily="Montserrat"
Cursor="Hand"
Margin="180,30,0,0" Width="377"
/>
</StackPanel>
<StackPanel
Orientation="Vertical"
Margin="82,102,82,68">
<TextBlock Text="Username"
Foreground="DarkGray"
FontSize="12"
FontWeight="Medium"
FontFamily="Montserrat"
Margin="0,35,0,0"/>
<TextBox x:Name="txtUser"
FontSize="13"
FontWeight="Medium"
FontFamily="Montserrat"
Foreground="White"
CaretBrush="LightGray"
BorderBrush="DarkGray"
BorderThickness="0,0,0,2"
Height="28"
VerticalContentAlignment="Center"
Margin="0,5,0,0"
>
<TextBox.Background>
<LinearGradientBrush></LinearGradientBrush>
</TextBox.Background>
</TextBox>
<TextBlock Text="Password"
Foreground="DarkGray"
FontSize="12"
FontWeight="Medium"
FontFamily="Montserrat"
Margin="0,15,0,0"/>
<PasswordBox x:Name="txtPass"
FontSize="13"
FontWeight="Medium"
FontFamily="Montserrat"
Foreground="White"
25 | P a g e
CaretBrush="LightGray"
BorderBrush="DarkGray"
BorderThickness="0,0,0,2"
Height="28"
VerticalContentAlignment="Center"
Margin="0,5,0,0">
<PasswordBox.Background>
<LinearGradientBrush></LinearGradientBrush>
</PasswordBox.Background>
</PasswordBox>
<Button x:Name="btnLogin"
BorderThickness="0"
Content="LOG IN"
Foreground="White"
FontSize="12"
FontFamily="Montserrat"
Cursor="Hand"
Margin="0,50,0,0"
Click="btnLogin_Click">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Background" Value="#0099FF"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#28AEED"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
<Button.Template>
<ControlTemplate TargetType="Button">
<Border Width="150" Height="40"
CornerRadius="20"
Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
<StackPanel>
<Button x:Name="btnCancel"
BorderThickness="0"
Content="CANCEL"
Foreground="White"
FontSize="12"
FontFamily="Montserrat"
26 | P a g e
Cursor="Hand"
Margin="20,350,20,0"
Click="btnCancel_Click">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Background" Value="LightSeaGreen"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="SeaGreen"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
<Button.Template>
<ControlTemplate TargetType="Button">
<Border Width="150" Height="40"
CornerRadius="20"
Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
</Grid>
</Border>
</Grid>
</Window>
Step 02. Right-click on the project | Add | New Item, select JavaScript JSON Configuration
File then rename to appsettings.json, click Add and write contents as follows:
{
"ConnectionStrings": {
"MyStockDB": "Server=(local);uid=sa;pwd=1234567890;database=MyStore;"
}
}
27 | P a g e
Step 04. Write codes for LoginWindow.xaml.cs:
28 | P a g e
Step 05. Write codes for MainWindow.xaml.cs:
29 | P a g e
30 | P a g e
31 | P a g e
32 | P a g e
33 | P a g e
Step 06. Open App.xaml and then update XAML code as follows:
<Application x:Class="WPFApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFApp"
StartupUri="LoginWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
Activity 05: Run the WPFApp project and test all actions
34 | P a g e