Project structure
GitHub
Below is the project solution structure
- .gitattributes
- .gitignore
- .gitlab-ci.yml
- Batch.File.Executor.sln
- Batch.File.Executor.sln.DotSettings.user
- LICENSE.txt
- README.md
DirectoryBatch.File.Executor # WinForms project
- Batch.File.Executor.csproj
- Batch.File.Executor.csproj.user
- editor.html
DirectoryMainForm.cs
- MainForm.Designer.cs
- MainForm.resx
- NLog.config
- Program.cs
DirectoryControllers
- AppControllerBase.cs
- BatchFileAppController.cs
- ExecuteFileController.cs
- LogController.cs
- Lib
DirectoryMonaco
- … # library comes here
DirectoryModels
- Datamodel.cs
DirectoryProperties
DirectoryPublishProfiles
- FolderProfile.pubxml
- FolderProfile.pubxml.user
- Resources
- $this.Icon.ico
DirectoryService
- AppLogger.cs
DirectoryUserControls
DirectoryMainView.cs
- MainView.Designer.cs
- MainView.resx
DirectoryViews
DirectoryDialogBase.cs
- DialogBase.Designer.cs
- DialogBase.resx
DirectoryDialogNewFile.cs
- DialogNewFile.Designer.cs
- DialogNewFile.resx
DirectoryDialogUpload.cs
- DialogUpload.Designer.cs
- DialogUpload.resx
DirectoryUnit.Tests # Tests
- Database_Reset.cs
- NLog.config
- Test_EF_Database.cs
- Test_Execution.cs
- Test_NLog.cs
- Unit.Tests.csproj
DirectoryModels
- Datamodel.cs
Application setup
- Create a Windows Forms C# project in visual studio
- Create also a Unit Test project
Nuget Packages
-
Packages for
Windows Forms C#
project- FontAwesome.Sharp (6.6.0)
- Microsoft.EntityFrameworkCore (8.0.8)
- Microsoft.EntityFrameworkCore.Sqlite (8.0.8)
- Microsoft.EntityFrameworkCore.Tools (8.0.8)
- Microsoft.Web.WebView2 (1.0.2739.15)
- NLog (5.3.3)
- NLog.Databases (5.3.3)
- NLog.Extensions.Logging (5.3.12)
- System.Data.SQLite (1.0.118)
-
Packages for
Unit Test
project- coverlet.collector (6.0.2)
- Microsoft.EntityFrameworkCore (8.0.8)
- Microsoft.EntityFrameworkCore.InMemory (8.0.8)
- Microsoft.EntityFrameworkCore.Tools (8.0.8)
- Microsoft.Web.WebView2 (1.0.2739.15)
- NLog (5.3.3)
- NLog.Databases (5.3.3)
- NLog.Extensions.Logging (5.3.12)
- System.Data.SQLite (1.0.118)
- xunit (2.9.0)
- xunit.runner.visualstudio (2.8.2)
Log configuration
In this project, the logging of system and user actions are saved to SQLite
database and alternatively as files. To achieve this, we will be using NLog
framework.
-
NLog internal configuration - for the logging framework itself
internalLogLevel
- level of logging required for viewinginternalLogFile
- location of theNLog
Logging
-
Targets
in NLog define where the logs are to be sent. This could be console, file or even database. For this project, we are going to save the logs in file (maximum 4 files) and to SQLite database itself- For the file target,
layout
attribute defines how each line is formatted. - For the database target,
commandText
element defines how each entry is saved to the database.parameter
are defined in the command text, which then can have an element describing thelayout
- For the file target,
-
Rules
define which logger is allowed to write to the target. Logging granularity can be set with the attributeminlevel
Refer the entire configuration of the NLog
below.
Important code snippets
UI development
MainView
- consist of the batch file along with start and delete buttonsSidebar
- actions for create and uploading files, lists available batch files for ExecuteFileControllerLogger
- displays the user and system action
Mainview
The main view consists of filename, start button and delete button.
Check the tabs for the integration of Monaco
editor which powers the VSCode.
DialogNewFile
DialogUpload
- Upload multiple files
- Upload single file
LoggerView
UI - titbits
Below I will show you essential code bits required to reproduce the Modern UI in Windows Forms.
Modern UI
In the MainForm.cs
file,
Apply this for the rest of the containers - panelLogs
panelSidebar
and panelMainView
App - Windows buttons
Entity framework (source)
What is Entity Framework?
TLDR;
It is a framework to interact with the database within the C# Project.
Prior to .NET 3.5, we (developers) often used to write ADO.NET code or Enterprise Data Access Block to save or retrieve application data from the underlying database. We used to open a connection to the database, create a DataSet to fetch or submit the data to the database, convert data from the DataSet to .NET objects or vice-versa to apply business rules. This was a cumbersome and error prone process. Microsoft has provided a framework called “Entity Framework” to automate all these database related activities for your application.
Entity Framework is an open-source ORM framework for .NET applications supported by Microsoft. It enables developers to work with data using objects of domain specific classes without focusing on the underlying database tables and columns where this data is stored. With the Entity Framework, developers can work at a higher level of abstraction when they deal with data, and can create and maintain data-oriented applications with less code compared with traditional applications.
Official Definition: “Entity Framework is an object-relational mapper (O/RM) that enables .NET developers to work with a database using .NET objects. It eliminates the need for most of the data-access code that developers usually need to write.”
The following figure illustrates where the Entity Framework fits into your application.
As per the above figure, Entity Framework fits between the business entities (domain classes) and the database. It saves data stored in the properties of business entities and also retrieves data from the database and converts it to business entities objects automatically.
Entity Framework Features
- Cross-platform: EF Core is a cross-platform framework which can run on Windows, Linux and Mac.
- Modelling: EF (Entity Framework) creates an EDM (Entity Data Model) based on POCO (Plain Old CLR Object) entities with get/set properties of different data types. It uses this model when querying or saving entity data to the underlying database.
- Querying: EF allows us to use LINQ queries (C#/VB.NET) to retrieve data from the underlying database. The database provider will translate this LINQ queries to the database-specific query language (e.g. SQL for a relational database). EF also allows us to execute raw SQL queries directly to the database.
- Change Tracking: EF keeps track of changes occurred to instances of your entities (Property values) which need to be submitted to the database.
- Saving: EF executes INSERT, UPDATE, and DELETE commands to the database based on the changes occurred to your entities when you call the SaveChanges() method. EF also provides the asynchronous SaveChangesAsync() method.
- Concurrency: EF uses Optimistic Concurrency by default to protect overwriting changes made by another user since data was fetched from the database.
- Transactions: EF performs automatic transaction management while querying or saving data. It also provides options to customize transaction management.
- Caching: EF includes first level of caching out of the box. So, repeated querying will return data from the cache instead of hitting the database.
- Built-in Conventions: EF follows conventions over the configuration programming pattern, and includes a set of default rules which automatically configure the EF model.
- Configurations: EF allows us to configure the EF model by using data annotation attributes or Fluent API to override default conventions.
- Migrations: EF provides a set of migration commands that can be executed on the NuGet Package Manager Console or the Command Line Interface to create or manage underlying database Schema.
EF Versions and overview
Implementation in project
For a basic implementation, we need a class
for an entity and a DbContext
to map the entity to the table
- Create and dispose context
- Add and update an Entity - BatchFile
Testing project - Unit.Tests
With unit-tests, each line of production code is tested. Atleast that is the idea for it.
xUnit
homepage
xUnit.net is a free, open source, community-focused unit testing tool for the .NET Framework. Written by the original inventor of NUnit v2, xUnit.net is the latest technology for unit testing C#, F#, VB.NET and other .NET languages. xUnit.net works with ReSharper/Rider, CodeRush, TestDriven.NET and Xamarin. It is part of the .NET Foundation, and operates under their code of conduct. It is licensed under Apache 2 (an OSI approved license).
I used xUnit
testing framework. It is simple yet powerful to use features made easy it.
3 Basic blocks of each test.
Arrange
- Setup the environment and conditions for the test. Eg. mock values, demo data and so on.Act
- Here the actual code for testingAssert
- Check if the desired condition is reached
Data models for testing
Below are few tests for the this project