首页 / C# / c#-在n层应用程序中引用的正确方法
c#-在n层应用程序中引用的正确方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-在n层应用程序中引用的正确方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2647字,纯文字阅读大概需要4分钟。
内容图文
![c#-在n层应用程序中引用的正确方法](/upload/InfoBanner/zyjiaocheng/665/72df0b6be5e4451fb0cc95c6a606e690.jpg)
我对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层概念.
那么,我该如何解决这个问题?我在Google上搜索了有关使用自动映射器的信息…
解决方法:
这里我没有直接为您的用例提供解决方案,但是我想为实现复杂的NLayer Architecture应用程序提供正确的途径.
什么是NLayer架构
Layering of an application’s codebase is a widely accepted technique
to help reduce complexity and improve code reusability. To achieve
layered architecture, we can follows the principles of
Domain Driven Design. In Domain Driven Design there are four
fundamental layers:Presentation Layer: Provides an interface to the user. Uses the
Application Layer to achieve user interactions.Application Layer: Mediates between the Presentation and Domain
Layers. Orchestrates business objects to perform specific application
tasks.Domain Layer: Includes business objects and their rules. This is heart
of the application.Infrastructure Layer: Provides generic technical capabilities that
support higher layers. An example of the Infrastructure Layer can be a
Repository implementation used to interact with a database through an
ORM framework, or an implementation for an email provider to send
emails.
必要时可以添加其他层.一个例子是:
Distributed Services Layer: Used to expose application features to
remote clients. There are tools like ASP.NET Web API and WCF that can
provide this layer. These are all common layers of a domain-centric
architecture. There may be minor differences based on implementation.
图层和结构概述如下所示:
这是一个包含五个项目的解决方案,用于一个简单的分层应用程序:
如果您想了解更多有关此的内容,我强烈建议您研究以下提到的项目.它是免费的开源程序.
内容总结
以上是互联网集市为您收集整理的c#-在n层应用程序中引用的正确方法全部内容,希望文章能够帮你解决c#-在n层应用程序中引用的正确方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。