0% found this document useful (0 votes)
94 views4 pages

PRN212 Summer 2024 Exam Instructions

Uploaded by

hieu nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views4 pages

PRN212 Summer 2024 Exam Instructions

Uploaded by

hieu nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PE_PRN212_SU24TrialTest - Note

SUMMER 2024
Subject: PRN212

INSTRUCTIONS
Please read the instructions carefully before doing the questions.
 You are NOT allowed to use any other materials. You are NOT allowed to use any device to share data
with others.
 You must use IDE as Visual Studio 2019 or later, MSSQL Server 2016 or later database for your
development tools.
IMPORTANT – Before you start doing your solution, MUST do the following steps:
1. To do your program, you must use Windows Presentation Foundation (WPF), apply 3-Layer
architecture. Note that you are not allowed to connect direct to database from WPF Windows/Pages,
every database connection must be used with Repository and Data Access Objects. The database
connection string must get from [Link] file. In the case your program connects directly to
database from WPF Windows/Pages or you hardcode connection string, you will get 0 point.
2. If there are syntax errors or compilation errors in your PE program, you will not pass the PE
requirements, the point will be 0.
3. Create Solution in Visual Studio 2019/2022 named PE_PRN212_SU24TrialTest_StudentName.
Inside the Solution, Project WPF named: AirConditionerShop_StudentName.
4. Create your MS SQL database named AirConditionerShop2024DB by running code in script
[Link].
5. Set the default user interface for your project as Login window/page.
6. Your work will be considered invalid (0 point) if your code inserts stuff that is unrelated to the test.

1
REFERENCES (this session just for reference, student can use the other approach to do Practical Exam)

Working with DB connection string from JSON file.


a. In the Presentation layer (WindowsForms Project), you create [Link] and add ConnectionString
same as the bellow to config the connection string to SQL Server Database.
{
"ConnectionStrings": {
"DefaultConnectionStringDB": "server =(local);
database=AirConditionerShop2024DB;uid=sa;pwd=1234567890; TrustServerCertificate=True"
}
}
You can change server, uid and pwd to suitable with your local machine.

b. Set property "Copy to output Directory" of [Link] file to "Copy if newer"

c. Using Manage Nuget packages to install packages

Package using for .NET:

[Link] [Link],
version [Link] version
.NET 5 5.0.17 5.0.0
.NET 6 6.0.27 6.0.1/6.0.0
.NET 7 7.0.16 7.0.0
.NET 8 8.0.2 8.0.0

- Install package using Tools  NuGet Package Manager  Package Manager Console

Install-Package [Link] -Version 5.0.17


Install-Package [Link] -Version 5.0.17
Install-Package [Link] -Version 5.0.17

- Install package using CLI or Power Shell


dotnet add package [Link] --version 5.0.17
dotnet add package [Link] --version 5.0.17
2
dotnet add package [Link] --version 5.0.17

d. Using ConfigurationBuilder to init Configuration object for reading [Link] file same as this code:
private string GetConnectionString()
{
IConfiguration config = new ConfigurationBuilder()
.SetBasePath([Link]())
.AddJsonFile("[Link]",true,true)
.Build();
var strConn = config["ConnectionStrings:DefaultConnectionStringDB"];

return strConn;
}

e. After that, durring development, student can bypass the ConnectionString (which read from
[Link]) to Data access layer by constructor or others
public partial class AirConditionerShop2024DBContext: DbContext
{
public AirConditionerShop2024DBContext (string connectionString)
{
[Link](connectionString);
}
}

Entity Framework Core


- Install dotnet-ef for CLI
dotnet tool install --global dotnet-ef --version 5.0.11

- Use Entity Framework Core to generate Object Model from existing database – CLI
dotnet ef dbcontext scaffold
"Server=(local);uid=sa;pwd=100104;database=AirConditionerShop2024DB;TrustServerCertificate=True"
[Link] --output-dir Entites

- Generate database from domain classes – CLI.


dotnet ef migrations add "InitialDB"
3
dotnet ef database update

Entity Framework Core


- Use Entity Framework Core to generate Object Model from existing database – Package Manager Console
Scaffold-DbContext
"Server=(local);uid=sa;pwd=100104;database=AirConditionerShop2024DB;TrustServerCertificate=True;"
[Link] -OutputDir Models Entites

- Generate database from domain classes – Package Manager Console


Add-Migration "InitialDB"
Update-Database -verbose

Common questions

Powered by AI

The PRN212 SUMMER 2024 trial test requires the use of a 3-Layer architecture for developing applications. This pattern is generally organized into the Presentation Layer, Business Logic Layer, and Data Access Layer. Direct database connections from the Presentation layer are discouraged because it violates the separation of concerns principle, which is a fundamental benefit of the 3-Layer architecture. This separation ensures that the Presentation layer does not include any database interaction code, which improves maintainability, scalability, and testability of the application .

The GetConnectionString method facilitates dynamic configuration of database connectivity by using the ConfigurationBuilder to load settings from the appsettings.json file. This method initializes an IConfiguration object that reads the database connection string specified under the "ConnectionStrings" section. By dynamically retrieving the connection string at runtime, this approach supports flexible and environment-specific configurations, allowing the application to adapt to different setups without modifying the code .

Hardcoding connection strings directly in the code can lead to security vulnerabilities, as sensitive information could be inadvertently exposed in version control systems. It can also decrease flexibility and maintainability, as changing connection details would require code modifications and recompilations. Using appsettings.json mitigates these issues by externalizing configuration data, allowing secure management of connection strings and simplifying changes without altering the codebase, thus preventing exposure and enhancing environment-specific configurations .

Handling connection strings and database configurations outside the main source code, such as in the appsettings.json file, is crucial for enhancing security, flexibility, and maintainability. It prevents sensitive information from being exposed in the source code, reducing the risk of accidental leakage through version control systems. It also allows for easier configuration changes, as updates to connection settings do not require recompiling the application, thus supporting different environments setups, like development, testing, and production, with minimal hassle .

The appsettings.json file in the WPF application is used to store the connection string required for database connections. This setup allows the application to retrieve the database connection string at runtime using a ConfigurationBuilder, which reads from the appsettings.json file. This approach enhances security by removing hardcoded database credentials from the codebase and improves flexibility by allowing the configuration to be changed without modifying the application code directly .

Separating the database connection logic into a dedicated class like AirConditionerShop2024DBContext adheres to several software development principles such as Single Responsibility Principle, Separation of Concerns, and Encapsulation. The Single Responsibility Principle is upheld as this class is solely responsible for database interactions, making the codebase easier to maintain and test. Separation of Concerns is achieved by isolating database logic from other application logic, promoting modularity. Encapsulation ensures that changes to database connection settings or logic do not impact other parts of the application, thus enhancing robustness and flexibility .

Entity Framework Core's migration feature is used to keep the database schema in sync with the application's domain models. This is achieved by generating migration files that include the necessary SQL commands to update the database schema. Developers can add a new migration using commands like 'dotnet ef migrations add "MigrationName"' and then apply these migrations to the database using 'dotnet ef database update'. This process allows for incremental updates to the database schema, reflecting changes in the domain models over time, and ensuring that the database structure aligns with the application's data model .

Entity Framework Core plays a crucial role in abstracting the database communication through the use of DbContext classes like AirConditionerShop2024DBContext. It supports reverse engineering of the database using commands such as 'dotnet ef dbcontext scaffold' and 'Scaffold-DbContext'. These commands allow developers to generate object models from existing databases, which facilitate interactions with the database through LINQ queries rather than raw SQL, simplifying development and maintenance .

Using the NuGet Package Manager to manage dependencies in a WPF project that employs Entity Framework Core is significant because it simplifies the process of adding or updating packages necessary for the project. It resolves package dependencies automatically and ensures compatibility across different versions of the software. Utilizing NuGet helps maintain a clean and easily reproducible development environment by managing libraries and tools required for data access and database interactions in a systematic way, thereby reducing errors and enhancing productivity .

Tools like Entity Framework for database schema migration are important because they enable the automation of database schema changes, which is critical for agile development practices that require frequent updates and deployments. They support the practice of making schema changes incrementally, reducing the risk associated with large-scale updates. By generating and applying migrations programmatically, these tools ensure that the database remains in sync with the evolving application models, facilitating continuous integration and deployment processes typical in agile environments .

You might also like