首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于mvc和服务(.NET)的企业体系结构项目结构

基于mvc和服务(.NET)的企业体系结构项目结构
EN

Stack Overflow用户
提问于 2016-11-09 23:35:12
回答 1查看 2K关注 0票数 1

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

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

当我有这个结构时,我可以在哪里存储一些WEB或WCF?在商业层面可以吗?或者你能给我建议一下,在哪里我找到了带有服务和mvc的EA的真实例子?谢谢

EN

回答 1

Stack Overflow用户

发布于 2016-11-09 23:53:39

你实际上有四个层次:

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

命名约定可能有点不同,但其思想是分离主体的,它允许服务层执行业务逻辑,但不知道数据层的方法调用。

所以你会说:

  • 表示-(参考服务、域、数据)
  • 服务层-(参考域,数据)
  • 域层-(无引用)
  • 数据层-(参考域)

所以表示层将引用所有,所以当您构建依赖项注入容器时,您可以正确地引用整个过程。

您可以将此项目视为示例。如何相互作用。

Presentation:

代码语言:javascript
复制
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);
        }
    }
}

服务层:

代码语言:javascript
复制
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
}

数据层:

代码语言:javascript
复制
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#聊天。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40517781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档