【发布时间】:2017-01-04 17:42:22
【问题描述】:
我对 n 层架构有点陌生,通过实现一个简单的控制台应用程序来学习它。
我有 3 个项目:
具有域实体和 DbContext 类的 DAL。
带有存储库类的 BLL。
控制台应用程序只是为了运行它。
由于我在 DAL 中定义的所有实体,BLL 层都引用了 DAL,如下所示:
public class DefaultRepository
{
private DefaultDbContext _repository;
private void SaveChanges()
{
try
{
_repository.SaveChanges();
}
catch (Exception e)
{
Console.WriteLine("Exception were caught");
Console.WriteLine(e.Message);
}
}
public void AddPatient(Patient patient)
{
_repository.Patients.Add(patient);
SaveChanges();
}
public Patient GetPatientById(int id)
=> _repository.Patients.Find(id) ?? null;
public void AddVisit(int patientId, Visit visit)
{
GetPatientById(patientId)?.Visits.Add(visit);
SaveChanges();
}
public DefaultRepository()
{
_repository = new DefaultDbContext();
}
}
明显的问题是我不能在我的控制台应用程序项目中使用存储库,因为控制台应用程序没有对 DAL 级别的引用。以下代码发生编译时异常。
DefaultRepository repository = new DefaultRepository();
repository.AddPatient(new Patient());
当然,我可以通过在 ConsoleApplication 项目中添加对 DAL 的引用来解决它。但是,据我了解,这绝对破坏了 n 层概念。 那么,我应该如何处理这个问题呢?我用谷歌搜索了一些关于使用自动映射器的信息......
【问题讨论】:
-
将(poco)域实体放在他们自己的项目中,并从控制台应用程序中引用它
标签: c# entity-framework