Introduction
The design for enterprise applications is a common question in software development and how we can solve this issue in the best way following best practices according to technology we have choose for our job.
In this guide, we'll take a look at the common requirements for design of enterprise architect.
We'll use EntityFramework for this guide, but this concepts apply for Dapper or another ORM
Background
The architecture for enterprise application should have at least the following levels:
- Entity Layer: Contains entities (POCOs)
- Data Layer: Contains all related code to database access
- Business Layer: Contains definitions and validations related to business
- External Services Layer (optional): Contains invocations for external services (ASMX, WCF, RESTful) Contains invocations for external services (
- Common: Contains common classes and interfaces for all layers (e.g. Loggers, Mappers, Extensions)
- Tests (QA): Contains automated tests for back-end code
- Presentation Layer: This is the UI
- UI Tests (QA): Contains automated tests for front-end code
Skills Prerequisites
- OOP (Object Oriented Programming)
- AOP (Aspect Oriented Programming)
- ORM (Object Relational Mapping)
- Design Patterns: Domain Driven Design, Repository & Unit of Work and IoC
Table of Contents
- Using the Code
- Step 01 - Create Database
- Step 02 - Core Project
- Step 03 - Putting All Code Together
- Step 04 - Add Unit Tests
- Step 05 - Add Mocks
- Step 06 - Add Web API
- Step 07 - Add Unit Tests for Web API
- Step 08 - Add Integration Tests for Web API
- Code improvements
- Points of Interest
- Related Links
Using the Code
Step 01 - Create Database
In this guide, we'll use a sample database to understand each component in our architecture. This is the script for database:
Create Database
We are using AdventureWorks2017 database
Core Project
Architecture project
DATABASE | SQL Server | DATABASE |
ENTITY LAYER | POCOs | BACK-END |
DATA LAYER | DbContext, Configurations, Contracts, Data Contracts and Repositories | BACK-END |
BUSINESS LAYER | Services, Contracts, DataContracts, Exceptions and Loggers | BACK-END |
EXTERNAL SERVICES LAYER | ASMX, WCF, RESTful | BACK-END |
COMMON | Loggers, Mappers, Extensions | BACK-END |
PRESENTATION LAYER | UI Frameworks (AngularJS | ReactJS | VueJS | Others) | FRONT-END |
Entity Layer
Please take a look at POCOs, we're using nullable types instead of native types because nullable are easy to evaluate if property has value or not, that's more similar to database model.
In Entity Layer there are two interfaces: IEntity and IAuditEntity
- IEntity represents all entities in our application
- IAuditEntity represents all entities that allows to save audit information: create and last update; as special point if we have mapping for views, those classes do not implement IAuditEntity because a view doesn't allow insert, update and elete operations.
Data Layer
For this source code, the implementation for repositories are by feature instead of generic repositories; the generic repositories require to create derived repositories in case we need to implement specific operations. I prefer repositories by features because do not require to create derived objects (interfaces and classes) but a repository by feature will contains a lot of operations because is a placeholder for all operations in feature.
The sample database for this article contains 4 schemas in database, so we'll have 4 repositories, this implementation provides a separation of concepts.
We are working with EF Core in this guide, so we need to have a DbContext and objects that allow mapping classes and database objects (tables and views).
Business Layer
Putting All Code Together
Add Unit Tests
Open a terminal window in your working directory and follow these steps to create unit tests for current project:
Create a directory in SimpleEnterprise.Core with name test.
Change to test directory.
Create a directory with name SimpleEnterprise.Core.Tests.
Change to SimpleEnterprise.Core.Tests directory
Run this command: dotnet new -t xunittest
Run this command: dotnet restore
Later, add tests project to current solution, creating a new solution item with name test and inside of that solution item, add an existing project.
Add reference to SimpleEnterprise.Core project and save changes to rebuild.
Add Mocks
Open a terminal window in your working directory and follow these steps to create unit tests for current project:
- Go to source code directory in SimpleEnterprise.Core.
- Create a directory with name SimpleEnterprise.Mocker.
- Change to SimpleEnterprise.Mocker directory
- Run this command: dotnet new
- Run this command: dotnet restore
- Later, add console project to current solution.
- Add reference to SimpleEnterprise.Core project and save changes to rebuild.
Web API
Web API project with name SimpleEnterprise.API and add references to SimpleEnterprise.Core project, add a controller with name Sales and add this code:
0 comments: