我是新的企业架构,但当我正确地理解,它是由3层组成。我在.net平台上从事学校项目,我有这样的结构:
当我有这个结构时,我可以在哪里存储一些WEB或WCF?在商业层面可以吗?或者你能给我建议一下,在哪里我找到了带有服务和mvc的EA的真实例子?谢谢
发布于 2016-11-09 23:53:39
你实际上有四个层次:
命名约定可能有点不同,但其思想是分离主体的,它允许服务层执行业务逻辑,但不知道数据层的方法调用。
所以你会说:
所以表示层将引用所有,所以当您构建依赖项注入容器时,您可以正确地引用整个过程。
您可以将此项目视为示例。如何相互作用。
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);
}
}
}服务层:
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#聊天。
https://stackoverflow.com/questions/40517781
复制相似问题