Framework
A framework is a structure of libraries and rules that sets how an application is built. Unlike a library, the framework calls your code. ASP.NET Core, Blazor, and EF Core are examples from .NET.
A framework is a structure of libraries and rules that sets how an application is built and takes over standard tasks. You fill the structure with your business code.
Framework or library?
The difference lies in the direction of the call. You call a library when you need it. A framework calls you: it starts the application, accepts requests, and hands them to your code. This is called inversion of control. In ASP.NET Core, it looks like this:
var app = WebApplication.Create(args);
app.MapGet("/hello", () => "Hello!");
app.Run();
You only describe what is to happen on a request to /hello. The request pipeline, web server, configuration, and logging come from the framework.
Frameworks in everyday .NET work
- ASP.NET Core: Microsoft describes it as a cross-platform, open-source framework for modern web applications, with Kestrel as the web server, built-in dependency injection, configuration, logging, minimal APIs, SignalR, gRPC, and security features.
- Blazor (part of ASP.NET Core): user interfaces made of components, written in C# instead of JavaScript.
- Entity Framework Core: a mapper between objects and the database.
- .NET itself is also called a platform or a framework; older applications run on the .NET Framework, which should not be confused with today's .NET.
- Test frameworks such as xUnit call your tests (unit test).
What a framework costs
A framework saves work, but it also binds you: you learn its rules, follow its release planning, and find it hard to get away from it again. Good practice is to keep business rules independent of the framework, so that they can be tested without a web server.
An example from the Kladde
The business rules of the Kladde, such as calculating free appointment slots, live in Kladde.Core as a pure function without any reference to ASP.NET Core. The framework provides endpoints, pages, and dependency injection; the rule itself can be checked with a unit test and could be carried over to another framework.
Common misconceptions
- A framework is not a language. C# is the language, ASP.NET Core the framework.
- Not every "framework" calls your code. Some collections carry the name but are libraries.
- More framework is not better. A small problem does not need the heaviest solution.
- An SDK is a toolkit, not a framework.
In the course
You learn the web framework in K2, module 2.3, starting with The first web API, and the user interface in module 2.5 with The Blazor Web App. Why business rules should stay independent is shown in K2, module 2.14, in the lessons on SOLID, for example L: substitutability that tests prove.
Further reading on the blog
Sources
- Microsoft Learn, Overview of ASP.NET Core: https://learn.microsoft.com/aspnet/core/overview?view=aspnetcore-10.0
- Microsoft Learn, Entity Framework Core: https://learn.microsoft.com/ef/core/