简体   繁体   English

.Net中的N层体系结构的项目结构 - 交叉引用层

[英]Project Structure for N-Tier Architecture in .Net - Cross Referencing Layers

I'm implementing an N-Tier architecture in .Net, comprised of an Entity layer, a DAL layer, a Business layer, and a UI layer. 我正在.Net中实现N层体系结构,包括实体层,DAL层,业务层和UI层。

The entities, in addition to primitive fields, contain navigation properties. 除原始字段外,实体还包含导航属性。 An example of this would be an Invoice entity, which has an InvoiceLines property, which should return all InvoiceLine entities associated with the parent Invoice entity. 其中一个示例是Invoice实体,它具有InvoiceLines属性,该属性应返回与父Invoice实体关联的所有InvoiceLine实体。

Owing to the fact that there is business logic which needs to be applied to these InvoiceLines once they have been retrieved from the persistence medium, and not wanting to duplicate code, the InvoiceLines property on the Invoice entity populates itself via a call to a GetInvoiceLinesByInvoiceID method within the business logic, which returns IEnumerable. 由于存在需要应用于这些InvoiceLines的业务逻辑,一旦从持久性介质中检索它们并且不想复制代码,Invoice实体上的InvoiceLines属性通过调用GetInvoiceLinesByInvoiceID方法填充自身在业务逻辑中,返回IEnumerable。

My problem is this - I cannot separate out the layers into separate projects, since the Entities project depends on the Business project, and the Business project depends on the Entities project, thereby introducing a cross reference. 我的问题是这个 - 我不能将这些层分成单独的项目,因为实体项目依赖于业务项目,而业务项目依赖于实体项目,从而引入了交叉引用。 I currently have all 3 layers (Entities, DAL and Business) living in the same project, which works fine, but means I cannot share just the Entities amongst other solutions. 我目前拥有所有3层(实体,DAL和商业)生活在同一个项目中,工作正常,但意味着我不能在其他解决方案中分享实体。

I appreciate that by cross-referencing layers in this way I'm violating some principle, but it's extremely convenient to have navigation properties populated on my Entities with business logic applied to them. 我很欣赏通过这种方式交叉引用图层我违反了一些原则,但是在我的实体上填充导航属性并且应用了业务逻辑非常方便。

Can anyone please suggest a better approach, or some way I can do this whilst keeping my layers in separate projects? 任何人都可以建议一个更好的方法,或者某种方式我可以这样做,同时保持我的层在单独的项目?

Cheers 干杯

I've been in this situation myself a number of times and there is usually a simple way out. 我自己一直处于这种状况,通常有一个简单的出路。

I'll make a few assumptions here but hopefully they'll be right. 我会在这里做一些假设,但希望他们是对的。 If you're using an interface based approach your business logic will implement some interface and your entities will implement some interface. 如果您使用的是基于接口的方法,您的业务逻辑将实现一些接口,您的实体将实现一些接口。 Any dependancy from the entity to the business object should only be of the interface and vice versa from the business object to the entity. 从实体到业务对象的任何依赖性应该只是从业务对象到实体的接口,反之亦然。 This should make it quite easy to strip the interfaces in to a separate project (or maybe two), then split out your business logic and concrete entities to their own projects which will only need to reference the interface assemblies. 这应该可以很容易地将接口剥离到一个单独的项目(或者两个)中,然后将业务逻辑和具体实体拆分为他们自己的项目,这些项目只需要引用接口程序集。

Hopefully that makes sense. 希望这是有道理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM