[英]Project structure of enterprise architecture with mvc and service (.NET)
我是企業架構的新手,但是當我正確理解時,它由3層組成。 我在.net平台上的學校項目工作,我有這樣的結構:
當我有這個結構,我可以存儲一些WEB API或WCF? 可以通過商業層嗎? 或者你可以建議我,在那里我找到EA與服務和mvc的真實單詞示例? 謝謝
你實際上有四層:
命名約定可能有點不同,但想法是Separation Of Principal。 一種想法,允許服務層執行業務邏輯,但不知道數據層的方法調用。
所以你會參考:
因此,您的表示層將引用所有內容,因此在構建依賴注入容器時,您可以在整個過程中正確引用。
您可以將此項目視為示例。 如何互動。
介紹:
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#聊天。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.