C# 具有mvc和服务(.NET)的企业架构的项目结构
我在企业架构方面是新手,但当我正确理解时,它由3层组成。我在.net平台上从事学校项目,我有这样的结构:C# 具有mvc和服务(.NET)的企业架构的项目结构,c#,.net,asp.net-mvc,wcf,enterprise-architecture,C#,.net,Asp.net Mvc,Wcf,Enterprise Architecture,我在企业架构方面是新手,但当我正确理解时,它由3层组成。我在.net平台上从事学校项目,我有这样的结构: 数据层-具有映射到关系数据库的数据模型的类库 业务层-类库,其中有一些类提供应用程序的功能 表示层——我想在这一层我可以完成mvc项目,但我不确定 当我有了这个结构,我可以在哪里存储一些WEB API或WCF?在业务层中,您是否可以正确地使用它?或者你能告诉我,我在哪里找到了EA与服务和mvc的真实示例?谢谢您实际上有四层: 介绍 服务层 域层 数据层 命名约定可能有点不同,但其思想
- 数据层-具有映射到关系数据库的数据模型的类库
- 业务层-类库,其中有一些类提供应用程序的功能
- 表示层——我想在这一层我可以完成mvc项目,但我不确定
当我有了这个结构,我可以在哪里存储一些WEB API或WCF?在业务层中,您是否可以正确地使用它?或者你能告诉我,我在哪里找到了EA与服务和mvc的真实示例?谢谢您实际上有四层:
- 介绍
- 服务层
- 域层
- 数据层
- 演示文稿-(对服务、域、数据的引用)
- 服务层-(参考域、数据)
- 域层-(无引用)
- 数据层-(参考域)
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
}
}
服务层:
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
}
}
使用数据层工厂;
使用Domain_Layer.Entity;
使用System.Collections.Generic;
使用系统数据;
使用System.Data.SqlClient;
命名空间服务层
{
公共类PeopleService:IPeopleService
{
私人只读的IEmployeeFactory;
私有常量字符串GetBranchs=“…”;
private const string getPeople=“…”
#区域构造函数:
公众服务(雇员工厂)
{
这个工厂=工厂;
}
#端区
公共IEnumerable getBranchs()
{
使用(var context=factory.Create())
返回context.List(getBranchs,CommandType.Text);
}
公共IEnumerable GetEmployees(int branchId)
{
使用(var context=factory.Create())
返回context.List(getPeople,CommandType.Text,new SqlParameter(){ParameterName=“BranchNum”,SqlDbType=SqlDbType.Int,Value=branchhid});
}
}
#区域意向声明:
公共接口IPeopReservice
{
IEnumerable getBranchs();
IEnumerable GetEmployees(int branchId);
}
#端区
}
数据层:
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
}
}
使用数据\u Layer.Repository;
使用制度;
使用System.Collections.Generic;
使用系统数据;
使用System.Data.SqlClient;
使用Data_Layer.Helper;
命名空间数据\u Layer.Context
{
公共类EmployeeContext:DbCommand,IEEmployeeRepository
{
私有布尔=假;
私有字符串连接;
#区域构造函数:
公共EmployeeContext(字符串dbConnection)
{
this.dbConnection=dbConnection;
}
#端区
公共IEnumerable列表(字符串查询、CommandType CommandType、params SqlParameter[]参数),其中tenty:class、new()
{
使用(var连接=新的SqlConnection(dbConnection))
使用(var命令=新的SqlCommand(查询、连接))
{
connection.Open();
command.CommandType=CommandType;
foreach(参数中的var参数)
command.Parameters.Add(参数);
返回BuildEntity(命令,newtenty());
}
}
#区域处置:
公共空间处置()
{
处置(真实);
GC.1(真);
}
受保护的虚拟void Dispose(bool disposing)
{
如果(!已处置)
这是真的;
}
~EmployeeContext(){Dispose(false);}
#端区
}
}
您需要看看这个项目,数据层和服务层是通过依赖项注入调用的,我为
Startup.cs
文件创建了一个扩展方法,但这就是它们的交互方式。如果您有任何问题,请随时提问,我每天都会与您聊天。您实际上有四个层次:
- 介绍
- 服务层
- 域层
- 数据层
- 演示文稿-(对服务、域、数据的引用)
- 服务层-(参考域、数据)
- 域层-(无引用)
- 数据层-(参考域)
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
}
}
服务层:
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
}
}
使用数据层工厂;
使用Domain_Layer.Entity;
使用System.Collections.Generic;
使用系统数据;
使用System.Data.SqlClient;
命名空间服务层
{
公共类PeopleService:IPeopleService
{
私人只读的IEmployeeFactory;
私有常量字符串GetBranchs=“…”;
private const string getPeople=“…”
#区域构造函数:
公众服务(雇员工厂)
{
这个工厂=工厂;