简体   繁体   中英

Project structure of enterprise architecture with mvc and service (.NET)

I am new in enterprise architecture, but when I right understand, it is composed with 3 tiers. I working on school project at .net platform and I have that structure:

  • Data tier - Class library with data models which are mapping to relational databas
  • Business tier - Class library where I have some classes which provide functionality of application
  • Presentation tier - I guess in this tier I could mvc project, but I am not sure.

When I have this structure, where I could store some WEB API or WCF? Could by right in business tier? Or can you advice me, where I found real-word example of EA with service and mvc ? Thanks

You actually have four layers:

  • Presentation
  • Service Layer
  • Domain Layer
  • Data Layer

The naming convention can be a bit different, but the idea is Separation Of Principal's. An idea that allows the service layer to perform business logic, but not be aware of the method invocation of the data layer.

So you would reference like:

  • Presentation - (Reference to Service, Domain, Data)
  • Service Layer - (Reference Domain, Data)
  • Domain Layer - (No Reference)
  • Data Layer - (Reference Domain)

So your presentation layer will reference all, so when you build your Dependency Injection Containers, you can correctly reference throughout.

You can look at this project as a sample. Of how to interact between.

Presentation:

using Microsoft.AspNetCore.Mvc;
using Service_Layer;

namespace Address_Book.Controllers
{
    [Route("api/[controller]")]
    public class PeopleController : Controller
    {
        #region Dependencies:

        private readonly IPeopleService peopleService;

        #endregion

        #region Constructor:

        public PeopleController(IPeopleService peopleService)
        {
            this.peopleService = peopleService;
        }

        #endregion

        [HttpGet]
        public JsonResult Get()
        {
            var branches = peopleService.GetBranches();
            return Json(branches);
        }

        [HttpGet("{id}")]
        public JsonResult Get(int id)
        {
            var people = peopleService.GetEmployees(id);
            return Json(people);
        }
    }
}

Service Layer:

using Data_Layer.Factory;
using Domain_Layer.Entity;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace Service_Layer
{
    public class PeopleService : IPeopleService
    {
        private readonly IEmployeeFactory factory;

        private const string getBranches = "...";
        private const string getPeople = "..."
        #region Constructor:

        public PeopleService(IEmployeeFactory factory)
        {
            this.factory = factory;
        }

        #endregion

        public IEnumerable<BranchModel> GetBranches()
        {
            using (var context = factory.Create())
                return context.List<BranchModel>(getBranches, CommandType.Text);
        }

        public IEnumerable<EmployeeModel> GetEmployees(int branchId)
        {
            using (var context = factory.Create())
                return context.List<EmployeeModel>(getPeople, CommandType.Text, new SqlParameter() { ParameterName = "BranchNum", SqlDbType = SqlDbType.Int, Value = branchId });
        }
    }

    #region Declaration of Intent:

    public interface IPeopleService
    {
        IEnumerable<BranchModel> GetBranches();

        IEnumerable<EmployeeModel> GetEmployees(int branchId);
    }

    #endregion
}

Data Layer:

using Data_Layer.Repository;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Data_Layer.Helper;

namespace Data_Layer.Context
{
    public class EmployeeContext : DbCommand, IEmployeeRepository
    {
        private bool disposed = false;
        private string dbConnection;

        #region Constructor:

        public EmployeeContext(string dbConnection)
        {
            this.dbConnection = dbConnection;
        }

        #endregion

        public IEnumerable<TEntity> List<TEntity>(string query, CommandType commandType, params SqlParameter[] parameters) where TEntity : class, new()
        {
            using (var connection = new SqlConnection(dbConnection))
            using (var command = new SqlCommand(query, connection))
            {
                connection.Open();
                command.CommandType = commandType;

                foreach (var parameter in parameters)
                    command.Parameters.Add(parameter);

                return BuildEntity(command, new TEntity());
            }
        }

        #region Dispose:

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
                disposed = true;
        }

        ~EmployeeContext() { Dispose(false); }

        #endregion
    }
}

You will need to look at the project, the Data Layer and Service Layer are being called via Dependency Injection, which I created an extension method for the Startup.cs file, but that is how they interact. If you have any questions feel free to ask, I'm in the C# chat daily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM