Wednesday, May 30, 2018

Entity Framework .NET Core 2 for Enterprise Application

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:
  1. Entity Layer: Contains entities (POCOs)
  2. Data Layer: Contains all related code to database access
  3. Business Layer: Contains definitions and validations related to business
  4. External Services Layer (optional): Contains invocations for external services (ASMX, WCF, RESTful) Contains invocations for external services (
  5. Common: Contains common classes and interfaces for all layers (e.g. Loggers, Mappers, Extensions)
  6. Tests (QA): Contains automated tests for back-end code
  7. Presentation Layer: This is the UI
  8. 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

  1. Step 01 - Create Database
  2. Step 02 - Core Project
  3. Step 03 - Putting All Code Together
  4. Step 04 - Add Unit Tests
  5. Step 05 - Add Mocks
  6. Step 06 - Add Web API
  7. Step 07 - Add Unit Tests for Web API
  8. 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:


  1. Go to source code directory in SimpleEnterprise.Core.
  2. Create a directory with name SimpleEnterprise.Mocker.
  3. Change to SimpleEnterprise.Mocker directory
  4. Run this command: dotnet new
  5. Run this command: dotnet restore
  6. Later, add console project to current solution.
  7. 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: