LernenderCorpDotNetMicroserviceGenerator 1.0.10
dotnet new install LernenderCorpDotNetMicroserviceGenerator::1.0.10
Lernender Corp .NET Microservice Generator
Version: 1.0.10
Email me for comments and/or suggestions on how I might improve this template.
- Email: slovelidge@lernendercorp.com
- LinkedIn: https://www.linkedin.com/in/lovelidge/
Overview
This project is intended for Software Engineers to help facilitate scaffolding of C# DotNet Core Microservices. The project utilizes 'dotnet ef dbcontext scaffold' to build out models that are reflective on table entities defined within a Microsoft SQL Server Database.
The project is intended as a starter project ONLY.
Moreover, using Microsoft's T4 Templating Engine (Microsoft.VisualStudio.TextTemplating) a complete set of Controllers, Services, and Repositories will be automatically created.
Additionally, a Dockerfile is created to create a Docker image. The images will be pushed to Elastic Container Registry and can subsequently be used by Elastic Container Service(s) (ECS) and/or Kubernetes Cluster Service.
Author
Author: Lovelidge, Shawn
License
Prerequisites
The following prerequisites are required to use this package!!!!
- Microsoft Windows Pro
- Installed .NET SDK 7.0
- Install Powershell 7.3 for Windows
- Microsoft SQL Server Management Studio (latest)
- SQL Server Database
- Database DDL created and validated for all entities and constraints
- SQL Server User Account & Password (i.e. sa account)
- Visual Studio Professional 2017 (15.9.57)
- Visual Studio Build Tools 2017 (15.9.57)
- AWS Account (used in YAML deployment scripts)
- AWS ECR Repository (used in YAML deployment scripts)
Install "LernenderCorpDotNetMicroserviceGenerator" Nuget Package
Using Package Manager...
Search for PackageId: "LernenderCorpDotNetMicroserviceGenerator" and install this package.
Once installed follow the remaining steps.
1. Verify Installed Template
To see all installed dotnet templates:
-----------------------------------------------------
c:> dotnet new list
Specific Template (i.e. 'Lernender Corp Microservice Generator AspNetCore'):
-----------------------------------------------------
C:> dotnet new list | findstr -i Lernender
2. Create a Database
In order for this Microservice Generator to work properly. It must have a valid SQL Server Database. Effectively, the Microservice Generator uses a database as a driver to know how to construct the various layers of the Microservice Api (i..e Controller, Service and Repository).
WARNING:
Without a valid database this Microservice Generator will not work!!! Before moving forward make sure to define a database with valid login administrative credentials. This database and corresponding credentials will be used in the followig steps.
3. Change to Root Development Folder
Once your template has been installed and verified then change directory to your development folder (example c:\dev)
4. Create MicroService Project
Now you can being using the template to create your first Microservice project. Execute the dotnet CLI statement...
SYNTAX:
-----------------------------------------------------------------
dotnet new lnmsapi
--namespace <namespace>
--output <namespace>
--sqlServer <sqlServer>
--sqlDatabase <sqlDatabase>
--usr <usr>
--passwd <passwd>
--dbContext <dbContext>
--awsAccountId <awsAccountId>
--awsECRRepository <awsECRRepository>
--azureDevOpsServiceConnection <service connection name>
--dry-run
Example:
-----------------------------------------------------------------
C:> dotnet new lnmsapi
--namespace "{namespace}"
--output "{namespace}"
--sqlServer "{sqlServer}"
--sqlDatabase "{sqlDatabase}"
--usr "{usr}"
--passwd "{passwd}"
--dbContext "{dbContext}"
--awsAccountId "{awsAccountId}"
--awsECRRepository "{awsECRRepository}"
--azureDevOpsServiceConnection "{azureDevOpsServiceConnection}"
--dry-run
Option | Definition |
---|---|
namespace | Give a namespace for your microservice. Typically this is your company name followed by the name of your project (i.e. Ln.Microservice) |
output | specify the directory where the project will be created. (note: omit back-slash or forward-slash) |
sqlServer | Database Server |
sqlDatabase | Database Name |
usr | Database User |
passwd | Database Password |
dbcontext | Give a name for the dbContext file that will be created (i.e. LnContext) |
awsAccountId | AWS AccountId (with administrator policies set) |
awsECRRepository | AWS Elastic Container Registry (ECR) Name |
azureDevOpsServiceConnection | Using Microsoft Dev/Ops → {Project} → Project Settings → Service Connection. Specify the name given for the AWS Connection |
NOTE:
The project should have been created after executing step 4.
Change directory to the new project folder ([Development Folder] > {namespace}).
Following the instructions in the next step:
5. Run "Database Context" Powershell Script (GenerateContext.ps1)
This will generate the models and DbContext used by Entity Framework Core (EF Core).
Change directory to {namespace}.Microservice.Repository folder
[Development Folder] >cd {namespace}.Microservice\{namespace}.Microservice.Repository
- Execute Powershell Script (GenerateContext.ps1)
Example: .\GenerateContext.ps1
6. Run Project Powershell Script (Generate.ps1)
This will generate the Controllers, Services, and Repository Layers...
Change directory to {namespace}.Microservice folder
[Development Folder] >cd {namespace}.Microservice
- Execute Powershell Script (GenerateContext.ps1)
Example: .\Generate.ps1
7. Build the Project in Visual Studio 2023
If everything went well, your Microservice project has been scaffold out and your entire rest API has been created. Open the solution file in your project folder and take time to examine the code in the Repository, Service, and Controller layers.
WARNING:
The project should work out of the box with one small caveat namely, the Select() and RowCount() calls for all repository APIs must be implemented by the developer.
Docker Build & Run
When attempting to run docker command you need to be in the root directory of the project.
The following commands are listed here for you to use:
> docker build -t {namespace} .
> docker run --env-file ./.env -p 8000:80 -p 443:443 -p 1433:1433 -t {namespace}
NOTE: The docker images is intended to ruin in Production mode (i.e. ASPNET_ENVIRONMENT=Production). If you wish to run your docker container in development mode append an environment variable.
Example:
> docker build -t {namespace} .
> docker run --env-file ./.env -e "ASPNET_ENVIRONMENT=Development" -p 8000:80 -p 443:443 -p 1433:1433 -t {namespace}
Microsoft Azure DevOps
The template also comes with a YAML file for both windows and linux platforms.
When setting up CI/CD build in Microsoft DevOps chooses the appropriate YAML file (i.e. windows or linux). The image will be built and deployed to Elastic Container Registry (ECR) provided correct AWS account and Registry names are specified.
-
net7.0
- Microsoft.VisualStudio.TextTemplating (>= 17.5.33428.366)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.