Developing a Console-Based Car Rental Management System in C# with EF Core with N-Layer Architecture

Maintaining clean code, separation of concerns, and scalability become more crucial as software programs expand. N-Layer Architecture, which divides an application into discrete layers, each in charge of a particular set of duties, is one of the most popular architectural patterns in enterprise.NET applications.

Using C#, Entity Framework Core, Repository Pattern, and Service Layer Architecture, we will construct a Vehicle Rental Management System in this post. The project’s organized N-Layer methodology enhances code organization, testability, and maintainability.

The implementation includes:

  • Entity Framework Core with SQL Server
  • Repository Pattern
  • Service Layer
  • Session Management
  • Authentication and Registration
  • Email Notifications
  • Vehicle Rental Operations
  • Console-Based User Interface
  • Result Wrapper Pattern

The project demonstrates how enterprise-level architecture can be implemented even in a console application.

What Is N-Layer Architecture?

N-Layer Architecture divides an application into multiple logical layers.

Presentation Layer

Responsible for user interaction.

Examples:

  • Console UI
  • ASP.NET MVC
  • Blazor
  • WinForms

Business Logic Layer (BLL)

Contains business rules and validations.

Examples:

  • Authentication rules
  • Vehicle rental policies
  • Customer validations

Data Access Layer (DAL)

Handles database operations.

Examples:

  • Entity Framework Core
  • Repository classes
  • Database Context

Shared Layer

Contains reusable components.

Examples:

  • Enums
  • DTOs
  • Utility classes
  • Result wrappers

Project Structure

Plain text

Required NuGet Packages

Install the following packages before starting the project:

PowerShell

For reading configuration from appsettings.json:

PowerShell

Additional console UI packages:

PowerShell

These packages are used throughout the application setup.

Configuring Entity Framework Core

After installing packages, create migrations and update the database.

PowerShell

Creating the Database Context

The AppDbContext acts as the bridge between Entity Framework Core and SQL Server.

DbSets

The application contains the following entities:

C#

The context loads the connection string from appsettings.json and configures SQL Server using Entity Framework Core. It also defines relationships between:

  • User and Customer
  • Customer and Rental
  • Vehicle and Rental

The project additionally uses seed data to insert default users and vehicles during database creation.

Database Configuration

The SQL Server connection string is stored in appsettings.json.

JSON

This approach keeps database configuration separate from application code.

Implementing the Repository Pattern

The Repository Pattern acts as an abstraction layer between the Business Layer and the database.

Benefits include:

  • Cleaner code
  • Easier testing
  • Better maintainability
  • Reduced database coupling

Instead of accessing Entity Framework directly inside services, repositories handle all CRUD operations.

Typical repositories include:

  • UserRepository
  • CustomerRepository
  • VehicleRepository
  • RentalRepository

Implementing the Service Layer

The Service Layer contains all business logic.

Examples include:

  • Login validation
  • User registration
  • Vehicle rental rules
  • Rental return logic
  • Email notification handling

Services interact with repositories rather than directly communicating with the database.

Authentication Service

The AuthServices class handles:

  • User login
  • User registration
  • Email validation
  • Password validation
  • Session creation

The service uses dependency-based communication with repositories to perform user-related operations.

Session Management

The application maintains the logged-in user using a static Session Manager.

Features

  • Login
  • Logout
  • Current User Tracking
  • Role Checking
C#

This allows role-based navigation throughout the application.

Input Validation

Validation is centralized inside a utility class.

Email Validation

The application validates email addresses using regular expressions.

Password Validation

Passwords are checked before registration and login.

Benefits:

  • Reusable validation logic
  • Cleaner service classes
  • Consistent validation rules

 

Implementing Email Notifications

The system includes an Email Sender service using SMTP.

Features

  • Registration notifications
  • OTP notifications
  • Rental confirmations
  • Future email integrations
C#

The implementation uses Gmail SMTP for sending emails.

Result Wrapper Pattern

Instead of returning primitive values, the application uses a generic Result Wrapper.

C#

Benefits:

  • Consistent API responses
  • Better error handling
  • Easier debugging
  • Strongly typed responses

 

Vehicle Rental Business Rules

The Vehicle Rental Service contains business validations before creating rentals.

Rules Implemented

  • Rental period must be between 1 and 30 days
  • Customer must exist
  • Vehicle must exist
  • Vehicle must be available
  • Maximum 2 active rentals per customer
  • Same vehicle cannot be rented twice simultaneously

These validations ensure business consistency before a rental is created.

Vehicle Return Process

When a customer returns a vehicle:

  1. Rental status is verified.
  2. Return date is recorded.
  3. Rental status changes to Returned.
  4. Vehicle availability is increased.
  5. Database records are updated.

This logic is encapsulated inside the User Service layer.

Console User Interface

The application uses Spectre.Console to create an interactive console experience.

Public Menu

Available before login:

  • Login
  • Register
  • Quit

Administrator Menu

Available after admin login:

  • View Active Subscriptions
  • View Paused Subscriptions
  • Logout

Customer Menu

Available after customer login:

  • View Available Plans
  • Subscribe to a Plan
  • Pause Subscription
  • Resume Subscription
  • Cancel Subscription
  • Logout

The menu-driven structure provides a clean user experience inside the console environment.

Advantages of Repository and Service Pattern

Separation of Concerns

Each layer handles a specific responsibility.

Maintainability

Business rules remain isolated from database logic.

Scalability

Additional features can be added without modifying existing layers.

Testability

Repositories and services can be mocked during unit testing.

Reusability

Business logic can be reused across multiple UI layers.

Best Practices Followed

  • N-Layer Architecture
  • Repository Pattern
  • Service Layer Pattern
  • Entity Framework Core
  • Configuration Management
  • Centralized Validation
  • Session Management
  • Generic Result Wrapper
  • Dependency-Based Design
  • Database Seeding

Conclusion

One of the best approaches for structuring medium- and large-scale C# applications is still N-Layer Architecture. Developers may build applications that are simpler to manage, expand, and test by integrating Entity Framework Core, Repository Pattern, Service Layer, and Utility Components.

This article’s Vehicle Rental Management System demonstrates how these architectural concepts interact in a practical setting. Despite the console-based nature of the application, the same architecture may be readily expanded to Blazor projects, MVC apps, ASP.NET Core Web APIs, or corporate solutions.

Recommendation for ASP.NET 10.0 Hosting

A solid base for developing online services and applications is ASP.NET. Before creating an ASP.NET web application, you must be proficient in JavaScript, HTML, CSS, and C#. There are thousands of web hosting providers offering ASP.NET hosting on the market. However, there are relatively few web hosting providers that offer top-notch ASP.NET hosting.

ASP.NET is the best development language in Windows platform, which is released by Microsoft and widely used to build all types of dynamic Web sites and XML Web services. With this article, we’re going to help you to find the best ASP.NET Hosting solution in Europe based on reliability, features, price, performance and technical support. After we reviewed about 30+ ASP.NET hosting providers in Europe, our Best ASP.NET Hosting Award in Europe goes to HostForLIFE.eu, one of the fastest growing private companies and one of the most reliable hosting providers in Europe.

You may also like...

Popular Posts

Skip to toolbar Log Out