你实际上有四层:
命名约定可能有点不同,但其理念是分离主体。一种允许服务层执行业务逻辑,但不知道数据层的方法调用的想法。
所以你会像这样引用:
- 演示文稿 -(对服务、域、数据的引用)
- 服务层 -(参考域,数据)
- 域层 - (无参考)
- 数据层 -(参考域)
因此,您的表示层将引用所有内容,因此当您构建依赖注入容器时,您可以正确引用整个内容。
您可以查看此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# 聊天中。