具有mvc和服务的企业架构的项目结构(.NET)

时间:2016-11-09 23:35:13

标签: c# .net asp.net-mvc wcf enterprise-architecture

我是企业架构的新手,但是当我正确理解时,它由3层组成。我在.net平台上的学校项目工作,我有这样的结构:

  • 数据层 - 具有映射到关系数据库的数据模型的类库
  • 业务层 - 类库,其中我有一些提供应用程序功能的类
  • 表示层 - 我想在这层我可以mvc项目,但我不确定。

当我有这个结构时,我可以在哪里存储一些WEB API或WCF?可以通过商业层吗?或者你可以建议我,在那里我找到EA与服务和mvc的真实单词示例?感谢

1 个答案:

答案 0 :(得分:3)

你实际上有四层:

  • 演示
  • 服务层
  • 域层
  • 数据层

命名约定可能有点不同,但这个想法是Separation Of Principal's。允许服务层执行业务逻辑但不知道数据层的方法调用的想法。

所以你可以参考:

  • 演示文稿 - (参考服务,域名,数据)
  • 服务层 - (参考域,数据)
  • 域层 - (无参考)
  • 数据层 - (参考域)

因此,您的表示层将引用所有内容,因此在构建依赖注入容器时,您可以在整个过程中正确引用。

您可以将此project视为示例。如何互动。

<强>演示:

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);
        }
    }
}

服务层:

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
}

数据层:

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
    }
}

您将需要查看项目,通过依赖注入调用数据层和服务层,我为Startup.cs文件创建了一个扩展方法,但这就是它们的交互方式。如果您有任何问题可以随意提问,我每天都在C#聊天。