When working with .NET, setting up a new solution and managing project files and references is often a time-consuming task, especially if you're setting up a multi-layered architecture. To streamline this, we’ve created a powerful batch script that automates the project setup process, from solution creation to package installation, folder structuring, and even launching the application on an available port.
Purpose of the Script
This batch script automates the creation of a .NET project structure in a solution with common layers, specifically targeting an ASP.NET MVC application setup. It also handles the tedious aspects, like configuring folder structures and adding essential project references. The script even finds an available port in a specified range, ensuring your project starts up without interference from other applications.
Steps and Key Functions
Step 1: Getting User Input for Project Name
The script prompts you to input a project name:
set /p ProjectName="Enter your project name: "
This `ProjectName` is then used throughout the script to name the solution and related projects, ensuring consistency.
Step 2: Finding an Available Port
In this section, the script checks for an available port between 7000 and 7100 using `netstat`. If it finds a free port, it sets it as `Port`:
set Port=
for /l %%i in (7000,1,7100) do (
netstat -an | find "%%i" >nul 2>&1
if errorlevel 1 (
set Port=%%i
echo Found available port: !Port!
goto :port_found
)
)
if not defined Port (
echo No available port found in the range 7000-7100
exit /b 1
)
This loop ensures that the script allocates a free port dynamically, avoiding conflicts with other running applications.
Step 3: Creating the Solution and Projects
The script uses `dotnet new sln` to create a solution with the name you provided. It then creates four projects:
- MVC Web Project: The main ASP.NET MVC project.
- Application Library: Contains application logic and service layers.
- Domain Library: Houses domain models and core business logic.
- Infrastructure Library: Manages data access and external integrations.
Each project is generated using `dotnet new`, and then added to the solution:
dotnet new sln -n %ProjectName%
dotnet new mvc -n %ProjectName%.Web
dotnet new classlib -n %ProjectName%.Application
dotnet new classlib -n %ProjectName%.Domain
dotnet new classlib -n %ProjectName%.Infrastructure
dotnet sln %ProjectName%.sln add %ProjectName%.Web\%ProjectName%.Web.csproj
dotnet sln %ProjectName%.sln add %ProjectName%.Application\%ProjectName%.Application.csproj
dotnet sln %ProjectName%.sln add %ProjectName%.Domain\%ProjectName%.Domain.csproj
dotnet sln %ProjectName%.sln add %ProjectName%.Infrastructure\%ProjectName%.Infrastructure.csproj
Step 4: Adding Project References
To ensure dependencies are correctly mapped, the script sets up references among projects:
- The Web project references the Application layer.
- The Application project references the Domain layer.
- The Infrastructure project references the Domain layer.
dotnet add %ProjectName%.Web\%ProjectName%.Web.csproj reference %ProjectName%.Application\%ProjectName%.Application.csproj
dotnet add %ProjectName%.Application\%ProjectName%.Application.csproj reference %ProjectName%.Domain\%ProjectName%.Domain.csproj
dotnet add %ProjectName%.Infrastructure\%ProjectName%.Infrastructure.csproj reference %ProjectName%.Domain\%ProjectName%.Domain.csproj
Step 5: Creating Folders
To maintain a clean architecture, we create folders for each layer and the MVC structure in the Web project:
mkdir %ProjectName%.Web\Controllers
mkdir %ProjectName%.Web\Views\Products
mkdir %ProjectName%.Web\wwwroot
mkdir %ProjectName%.Application\Interfaces
mkdir %ProjectName%.Application\Services
mkdir %ProjectName%.Application\DTOs
mkdir %ProjectName%.Domain\Entities
mkdir %ProjectName%.Domain\ValueObjects
mkdir %ProjectName%.Domain\Exceptions
mkdir %ProjectName%.Infrastructure\Data
mkdir %ProjectName%.Infrastructure\Repositories
mkdir %ProjectName%.Infrastructure\Services
Step 6: Adding External Packages
This script installs the `Dapper` package for data access in the Infrastructure project:
dotnet add %ProjectName%.Infrastructure\%ProjectName%.Infrastructure.csproj package Dapper
Dapper is a lightweight, fast ORM that works well with a layered architecture.
Step 7: Building the Solution
After setting up everything, the script builds the solution to confirm there are no errors in project references or folder structures:
dotnet build
Step 8: Running the Project on the Dynamic Port
Finally, the script starts the application on the found port, launching the browser automatically:
start "" "http://localhost:!Port!"
dotnet run --project %ProjectName%.Web --urls "http://localhost:!Port!"
Here’s the full batch script:
@echo off
setlocal enabledelayedexpansion
:: Get project name from user input
set /p ProjectName="Enter your project name: "
:: Function to find an available port starting from 7000
set Port=
for /l %%i in (7000,1,7100) do (
netstat -an | find "%%i" >nul 2>&1
if errorlevel 1 (
set Port=%%i
echo Found available port: !Port!
goto :port_found
)
)
if not defined Port (
echo No available port found in the range 7000-7100
exit /b 1
)
:port_found
:: Create solution and projects
dotnet new sln -n %ProjectName%
dotnet new mvc -n %ProjectName%.Web
dotnet new classlib -n %ProjectName%.Application
dotnet new classlib -n %ProjectName%.Domain
dotnet new classlib -n %ProjectName%.Infrastructure
:: Add projects to solution
dotnet sln %ProjectName%.sln add %ProjectName%.Web\%ProjectName%.Web.csproj
dotnet sln %ProjectName%.sln add %ProjectName%.Application\%ProjectName%.Application.csproj
dotnet sln %ProjectName%.sln add %ProjectName%.Domain\%ProjectName%.Domain.csproj
dotnet sln %ProjectName%.sln add %ProjectName%.Infrastructure\%ProjectName%.Infrastructure.csproj
:: Add project references
dotnet add %ProjectName%.Web\%ProjectName%.Web.csproj reference %ProjectName%.Application\%ProjectName%.Application.csproj
dotnet add %ProjectName%.Application\%ProjectName%.Application.csproj reference %ProjectName%.Domain\%ProjectName%.Domain.csproj
dotnet add %ProjectName%.Infrastructure\%ProjectName%.Infrastructure.csproj reference %ProjectName%.Domain\%ProjectName%.Domain.csproj
:: Create folders
mkdir %ProjectName%.Web\Controllers
mkdir %ProjectName%.Web\Views\Products
mkdir %ProjectName%.Web\wwwroot
mkdir %ProjectName%.Application\Interfaces
mkdir %ProjectName%.Application\Services
mkdir %ProjectName%.Application\DTOs
mkdir %ProjectName%.Domain\Entities
mkdir %ProjectName%.Domain\ValueObjects
mkdir %ProjectName%.Domain\Exceptions
mkdir %ProjectName%.Infrastructure\Data
mkdir %ProjectName%.Infrastructure\Repositories
mkdir %ProjectName%.Infrastructure\Services
:: Add Dapper package
dotnet add %ProjectName%.Infrastructure\%ProjectName%.Infrastructure.csproj package Dapper
:: Build the project
dotnet build
:: Run the project on the dynamic port and launch the browser
start "" "http://localhost:!Port!"
dotnet run --project %ProjectName%.Web --urls "http://localhost:!Port!"
Final Thoughts
This script provides a streamlined approach to initializing a .NET solution and its projects, saving time and reducing setup errors. It’s ideal for those working on complex .NET MVC applications who need a quick way to scaffold a multi-layer architecture.
By automating these foundational tasks, you can focus on building features rather than configuring solutions, enabling faster and more efficient development.
0 Comments
if you have any doubts , please let me know