DotNetBrightener.WebApi.GenericCRUD.Generator
2024.0.1-alpha-1
See the version list below for details.
dotnet add package DotNetBrightener.WebApi.GenericCRUD.Generator --version 2024.0.1-alpha-1
NuGet\Install-Package DotNetBrightener.WebApi.GenericCRUD.Generator -Version 2024.0.1-alpha-1
<PackageReference Include="DotNetBrightener.WebApi.GenericCRUD.Generator" Version="2024.0.1-alpha-1" />
paket add DotNetBrightener.WebApi.GenericCRUD.Generator --version 2024.0.1-alpha-1
#r "nuget: DotNetBrightener.WebApi.GenericCRUD.Generator, 2024.0.1-alpha-1"
// Install DotNetBrightener.WebApi.GenericCRUD.Generator as a Cake Addin #addin nuget:?package=DotNetBrightener.WebApi.GenericCRUD.Generator&version=2024.0.1-alpha-1&prerelease // Install DotNetBrightener.WebApi.GenericCRUD.Generator as a Cake Tool #tool nuget:?package=DotNetBrightener.WebApi.GenericCRUD.Generator&version=2024.0.1-alpha-1&prerelease
Code Generator for Centralized CRUD WebAPI in ASP.NET Core Applications
© 2024 DotNet Brightener
Getting Started
Create new project
You can create the project in Visual Studio for your convenience. You can put everything into one single WebAPI project, but it's recommended not to do that, instead, you should separate the project into different libraries. The suggestion is as the following table
Project Name | Project Type | Purpose |
---|---|---|
{YourProject}.WebAPI | WebAPI | The entry point of the application |
{YourProject}.Core or {YourProject}.Entities | Class Library | The library that contains only the entities needed for your application |
{YourProject}.Services | Class Library | The library that contains business logics and CRUD auto generated classes |
{YourProject}.Database | Class Library | The library that holds the database context to communicate with the database |
If you like CLI, you can follow the instruction below.
- Create a new .NET 8 WebAPI project:
dotnet new webapi -n {your-project-name} -f net8.0
dotnet new classlib -n {your-project-name}.Database -f net8.0
dotnet new sln --name {your-project-name}
dotnet sln {your-project-name}.sln add {your-project-name}\\{your-project-name}.csproj
dotnet sln {your-project-name}.sln add {your-project-name}.Database\\{your-project-name}.Database.csproj
- If needed to separate the entity definitions to a different library, create new class library project
dotnet new classlib -n {your-project-name}.Core -f net8.0
dotnet sln {your-project-name}.sln add {your-project-name}.Core\\{your-project-name}.Core.csproj
- If needed to separate the service layer, create new class library project
dotnet new classlib -n {your-project-name}.Services -f net8.0
dotnet sln {your-project-name}.sln add {your-project-name}.Services\\{your-project-name}.Services.csproj
Now you can open the solution in Visual Studio.
Add Generic CRUD and its Generator Library
1. Add CRUD libraries
Add the following packages to the projects, following the instruction below
| Package Name | Project |
| -- | -- |
| DotNetBrightener.WebApi.GenericCRUD | Web API |
| DotNetBrightener.WebApi.GenericCRUD.Generator | Web API, Service |
| DotNetBrightener.DataAccess.Abstractions | Core (Entity) |
| DotNetBrightener.DataAccess.EF | Database |
2. Update the settings to enable code generator
Open the csproj
files and update the <PackageReference>
of the Generator
library with the following
<ItemGroup>
<PackageReference Include="DotNetBrightener.WebApi.GenericCRUD.Generator"
Version="{current-version-of-the-library}"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
The change is to add
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"
to the XML tag, that enables the auto generate code for the projects.
Create first entity
The entities must inherit from BaseEntity
or BaseEntityWithAuditInfo
from the DotNetBrightener.DataAccess.Abstractions
library. Create any entity into the Core or Entity project.
Example:
using DotNetBrightener.DataAccess.Models;
namespace CRUDWebApiWithGeneratorDemo.Core.Entities;
public class Product: BaseEntity
{
[MaxLength(255)]
public string Name { get; set; }
// omitted code
}
The Entity/Core project should only contain the entities without any other logics
Configure Code Generator for Service Project
At the root of Service project, create a class CRUDDataServiceGeneratorRegistration.cs
with the following content
using CRUDWebApiWithGeneratorDemo.Core.Entities;
namespace CRUDWebApiWithGeneratorDemo.Services;
public class CRUDDataServiceGeneratorRegistration
{
public List<Type> Entities =
[
// provide all entities that need to generate CRUD data service for
typeof(Product),
typeof(ProductCategory)
];
}
The name CRUDDataServiceGeneratorRegistration
is strictly required as the generator library will look for that file in order to understand what to generate.
In the Entities
list in the class, provide all the entities that needed to generate the CRUD data service interfaces and classes.
Configure Code Generator for WebAPI Project
At the root of WebAPI project, create a class CRUDWebApiGeneratorRegistration.cs
with the following content
public class CRUDWebApiGeneratorRegistration
{
// reference the CRUDDataServiceGeneratorRegistration from Service project
Type DataServiceRegistrationType = typeof(CRUDDataServiceGeneratorRegistration);
public List<Type> Entities =
[
// provide all entities that need to generate CRUD API Controllers for
// Some entities related to Authorization / Authentication / Security should be ignored
typeof(Product),
typeof(ProductCategory)
];
}
The name CRUDWebApiGeneratorRegistration
is strictly required as the generator library will look for that file in order to understand what to generate.
In the Entities
list in the class, provide all the entities that needed to generate the CRUD Web API Controllers for.
Build and Run Project
As you followed the instruction so far, upon building and running the project, the needed classes will be automatically generated. By default, .NET WebAPI project includes the OpenAPI library, which will detect the available APIs and you can see the following when the project is launch:
![[swagger_screenshot.png]]
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.8.0)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.8.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.