C# is a object-oriented and type-safe programming language. C# enables developers to build many types of applications that run in .NET.
C# programs run on .NET, a virtual execution system called the common language runtime (CLR) and a set of class libraries. The CLR is the implementation by Microsoft of the common language infrastructure (CLI), an international standard.
What is .NET? .NET is a free, open-source development platform for building many kinds of apps, such as:
Types and variables
A type defines the structure and behavior of any data in C#. The declaration of a type may include its members, base type, interfaces it implements, and operations permitted for that type. A variable is a label that refers to an instance of a specific type.
There are two kinds of types in C#: value types and reference types. Variables of value types directly contain their data. Variables of reference types store references to their data, the latter being known as objects. With reference types, it’s possible for two variables to reference the same object and possible for operations on one variable to affect the object referenced by the other variable. With value types, the variables each have their own copy of the data, and it isn’t possible for operations on one to affect the other (except for ref and out parameter variables).
An identifier is a variable name. An identifier is a sequence of unicode characters without any whitespace. An identifier may be a C# reserved word, if it’s prefixed by @. Using a reserved word as an identifier can be useful when interacting with other languages.
C#’s value types are further divided into simple types, enum types, struct types, nullable value types, and tuple value types. C#’s reference types are further divided into class types, interface types, array types, and delegate types.
C# programs use type declarations to create new types. A type declaration specifies the name and the members of the new type. Six of C#’s categories of types are user-definable: class types, struct types, interface types, enum types, delegate types, and tuple value types. You can also declare record types, either record struct, or record class. Record types have compiler-synthesized members. You use records primarily for storing values, with minimal associated behavior.
A class type defines a data structure that contains data members (fields) and function members (methods, properties, and others). Class types support single inheritance and polymorphism, mechanisms whereby derived classes can extend and specialize base classes. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/class
A struct type is similar to a class type in that it represents a structure with data members and function members. However, unlike classes, structs are value types and don’t typically require heap allocation. Struct types don’t support user-specified inheritance, and all struct types implicitly inherit from type object. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct
An interface type defines a contract as a named set of public members. A class or struct that implements an interface must provide implementations of the interface’s members. An interface may inherit from multiple base interfaces, and a class or struct may implement multiple interfaces. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/interface
A delegate type represents references to methods with a particular parameter list and return type. Delegates make it possible to treat methods as entities that can be assigned to variables and passed as parameters. Delegates are analogous to function types provided by functional languages. They’re also similar to the concept of function pointers found in some other languages. Unlike function pointers, delegates are object-oriented and type-safe. https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/using-delegates
Object Oriented programming
Classes, structs, and records Definition of a class, struct, or record—is like a blueprint that specifies what the type can do. An object is basically a block of memory that has been allocated and configured according to the blueprint. This article provides an overview of these blueprints and their features. The next article in this series introduces objects. https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/tutorials/oop
Object is basically a block of memory that has been allocated and configured according to the blueprint. https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/object-oriented/objects
Inheritance enables you to create new classes that reuse, extend, and modify the behavior defined in other classes. https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/object-oriented/inheritance
Polymorphism is the ability of objects of different types to provide a unique interface for different implementations of methods https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/object-oriented/polymorphism
The road-map consists of several steps. In each step, a set of theoretical concepts are explored, supported by reference documentation, book chapters, tutorials and videos. In parallel, a simple application will be built with the learned concepts: the Online Shop application.
After the learning material for a given step was sufficiently explored, either some new functionality will be added to this application or old functionality will be refactored.
The application will have little-to-no user interface. Developers are expected to perform developer tests with Postman once the REST APIs are implemented OR Swagger
All the code written must be published on GitHub. Commits must be pushed when each individual chapter is finished. In order to request a code review from the trainers, you must open a pull request from the develop
to the master
branch.
You can work using your local environment:
The application will deal with the management and daily functioning of a small online shop. Business processes:
Goal: Getting familiar with the ecosystem around c#. You can skip this chapter if you have already worked with C# and Git.
Required Reading:
Online Shop: nothing to do.
Further Resources:
During the course, we will create an application for online shopping. Each chapter will cover different aspects of the C# language. The final application will be a client-server application.
For the first chapter please create a simple Web API using .Net Core.
To create the application open Visual Studio, choose a Create a New Project, Select ASP.NET Core Web API project and follow the steps. Please check the following the images to select the right options:
and
After you create the project please inspect all the classes that were made. In the project create the Folder Model where you will put your data Model. As a start-up sample, you have in Sources/Chapter1/Startup_Chapter the project created with one class implemented.
You should create the rest of the data models and controls. The classes are shown below diagram:
Location, ProductCategory collection should be implemented as a generic “HashTable”.
Location, ProductCategory will have a controller.
Stock will not have a controller.
Online Shop:
Register an account on GitHub and accept the training GitHub Classroom Assignment. This will create a new GitHub repository for you. Clone this repository locally and checkout the
develop
branch. During the course of the training, you will commit and push your work on this branch.Go to “Visual Studio” and generate a new project ASP.NET Core Web API:
- Project name:
shop
,- Solution:
ro.msg.learning
,
Goal: Debug and testing your aplication.
The online shop application built on Cpater 1 has included the Kestrel server inside. The Kestrel web server is a new web server as part of ASP.NET Core. It is now the preferred web server for all new ASP.NET applications. By default, Kestrel will generate a test page for your application. This is a good starting point to do manual testing of your application.
Look at the screenshots from folder /Sources/Chapter2 to see how you can test your application.
Online Shop:
complete the solution with the relation from the diagram.
run, debug and manual test the application with help page
(Optional) For other type of testing, you can use different tools like:
Goal: Understand IO Operations and JSON Serialization
Required Reading:
Serialize JSON using System.Text.Json
-it is the main JSON Serialization library in .Net 6 and recommended for new projects.
OnlineShop: Save product information and order information on local json files on disk.
Every time a product is created a file must be created on disk with information about the product.
When a product is modified or deleted, this must reflect in the files saved on disk.
Every time an order is created a file must be created containing the order information.
You will have to come up with an apropiate folder structure for this task
Further Reading:
An alternative for serialization is Newtonsoft.Json
-it is important to know about Newtonsoft.Json, as it was the main library used for JSON serialization for C#, and it can be found in older projects
Goal: Understand how logging libraries work and how to log the necessary information from your application
Required Reading:
Online Shop: Logging important actions (Order Product, Product create/edit/delete and so on) will greatly assist in the support and development of your application.
you need to identify and log all the important actions in your application
it is your decision how you structure and what information goes into those logs to be as comprehensive as possible
Future Reading:
Goal:
Required Reading:
Online Shop:
Goals: a) Understanding the need and advantages of databases compared to the file system. b) Principles of databases. c) Understanding, comparing, contrasting, using and integrating different database frameworks into a web application. e) Choosing and using relational and non-relational databases.
Why databases?
Required reading: File System vs. Database
Online Shop: Ask yourself the following questions: what happens if you want to update a numeric quantity into a very large products/orders data file that stores online shop information? What happens if during the time spent with reading/searching a large file, other simultaneous requests come for writing or modifying or deleting the same quantities or entries and what happens if during that exact time there is a power shortage? Can databases address some of these problems?
Further reading: Why use a database instead of saving data to disk?
Goal: Understanding how and why Entity Framework can be used to automate all types of SQL database-related activities for an application, and how using the frameowrk developers can work at a higher level of abstraction when they deal with data and can create and maintain the data-oriented application with less code and database-related knowledege.
Required Reading: .NET 6.0 - Connect to SQL Server with Entity Framework Core
Online Shop: Automatically create/update an SQL Server database from code using EF Core migrations that mirrors the model from Chapter 1 based upon the example given in the previous tutorial and adapt given example to connect, control & manage and perform CRUD operations on the SQL DB.
Further Reading:
Goal: Understanding how ADO.NET provides consistent access to data sources such as SQL Server and how data-sharing consumer applications can use ADO.NET to connect to these data sources and retrieve, handle, and update the data that they contain.
Required Reading: ADO.NET by building CRUD features in ASP.NET Core Application
Online Shop: Create an SQL database that mirrors the model from Chapter 1 based upon the example given in the previous tutorials and adapt and test given example to connect, control & manage and perform CRUD operations on the SQL DB.
Further Reading:
Goal: Understanding the difference between a relational and a non-relational database and adapting the OnlineShop Web Api to have persistence into a NoSql model database such as MongoDb.
Required Reading:
Online Shop: Create a MongoDB NoSQL database that mirrors the model from Chapter 1 based upon the example given in the previous tutorial and adapt and test given example to connect, control & manage and perform CRUD operations on the DB.
Further Reading:
Below are described 3 methods on how to access the database using c#, depending on the implementation, you can choose to use desktop or web applications to access the database.
There are three frameworks for building web applications that every frontend developer has heard about: React, Vue, and Angular. React is a UI library, Angular is a fully-fledged front-end framework, while Vue.js is a progressive framework. They can be used almost interchangeably to build front-end applications, but they’re not 100 percent the same, so it makes sense to compare them and understand their differences. Each framework is component-based and allows the rapid creation of UI features.
Angular AngularJS, the original framework, is an MVC (Model-View-Controller)) framework. But in Angular 2, there’s no strict association with MV*-patterns as it is also component-based. Projects in Angular are structured into Modules, Components, and Services. Each Angular application has at least one root component and one root module. Each component in Angular contains a Template, a Class that defines the application logic, and MetaData (Decorators). The metadata for a component tells Angular where to find the building blocks that it needs to create and present its view.
React is based on JavaScript, but it’s mostly combined with JSX (JavaScript XML), a syntax extension that allows you to create elements that contain HTML and JavaScript at the same time. Anything you create with JSX could also be created with the React JavaScript API, but most developers prefer JSX because it’s more intuitive.
WinForms Windows Form Application : WinForms, as the name suggests, is basically a GUI-based approach introduced to the .NET framework. Prior to WPF and Silverlight, it was the main API for .NET that is being used to build GUI. It does not require any type of support other than runtime and OS for developing a standalone application. One can develop apps that are easy to deploy, update, manage, and work offline while connected to the Internet. The development of WinForms is very simple as it is only based on the drag and drop placement of UI controls on canvas. It is the old platform for developing desktop applications.
Razor is a templating engine that combines C# with HTML to build dynamic web content.
Blazor is a component-based, single-page app framework for building client-side web apps using .NET that works well with all modern browsers via WebAssembly for client-side Blazor. Server-side Blazor ASP.NET Core app runs on server and makes use of SignalR connection to communicate with the client (browser). It works with all modern browsers. In other words, Blazor is a hosting model for Razor components.
Code access security is a mechanism that grants/denies access to resources within a method call. For example, code written by a person may be allowed to write to the disk while code from another one may be forbidden from accessing the disk. This control can be enforced even if the code written by both of them is used within a single application.
C# security vulnerabilities includes topics such as :
Goal: Group business logic into service classes and expose this logic through REST interfaces.
Required Reading:
Online Shop:
Create a simple API for exposing the products and product categories:
- Define a DTO (data transfer object) POJO which combines the properties from a product and its respective category.
- Create a service class which uses the repositories in order to: create, update, delete, read by ID and read all the product (as DTO instances).
- Build a REST Controller which uses this service.
Create a service class that handles the creation of orders. The following constraints apply:
- You get a single C# object as input. This object will contain the order timestamp, the delivery address and a list of products (product ID and quantity) contained in the order.
- You return an Order entity if the operation was successful. If not, you throw an exception.
- The service has to select a strategy for finding from which locations should the products be taken. The strategy should be selected based on a
@Configuration
. The following initial strategies should be created:
- Single location - find a single location that has all the required products (with the required quantities) in stock. If there are more such locations, simply take the first one based on the ID.
- Most abundant - take each product from the location which has the largest stock for that particular product.
- The service then runs the strategy, obtaining a list of objects with the following structure: location, product, quantity (= how many items of the given product are taken from the given location). If the strategy is unable to find a suitable set of locations, it should throw an exception.
- The stocks are be updated by subtracting the shipped goods.
- Afterwards the order is persisted in the database and returned.
Create a Rest Controller for the “Create order” operation, which should have a
POST
mapping accepting a JSON request body and producing a JSON response body.
Further Resources:
Goal: Store unstructured data in a NoSQL database.
Required Reading:
Further Resources:
Goal: Asynchronously communicate with a background worker application.
Required Reading:
Further Resources:
Goal: Publish events though WebSocket to allow potential user interfaces to automatically update their displayed data.
Required Reading:
Online Shop:
Further Resources: