C# EF4 和分层设计的程序集引用错误
我终于抽出时间来查看最新的 EF 版本,但我的测试遇到了一些麻烦。
因此,我的 DAL 层保存我的 .EDMX 文件,并且我正在使用 MS POCO 模板,因此我还拥有生成的 .TT 文件和类。
我在 TestProject.DAL 中有一个通用基类,我创建了这些类的派生源。 即
public class BaseEntity<T> {}
public class Customer : BaseEntity<Customer> {}
public class Product : BaseEntity<Product> {}
然后在我的 TestProject.BLL 层中我有一些派生类 即
public class TestProject.BLL.Customer : TestProject.DAL.Customer {}
public class TestProject.BLL.Product : TestProject.DAL.Product {}
然后,在我的 UI 层中,我调用我的 BLL.Customer 对象。 我收到一条错误消息,指出未添加对 DAL.Customer 对象的引用,等等。
我有一个来自 UI 项目的对 BLL 项目的引用,以及来自我的 BLL 项目的对 DAL 的引用。
为什么 UI 层抱怨它知道 DAL 层,而该项目中没有引用它?
另外,作为一个附带问题,这看起来像一个“好的”设计吗?
谢谢大家! 鹅
I'm finally getting around to checking out the latest EF release and I'm running into some troubles with my testing.
So, my DAL layer holds my .EDMX file and I'm using the MS POCO template so I also have the generated .TT files and classes.
I have a generic base class in the TestProject.DAL I've created that those classes derive from.
I.e.
public class BaseEntity<T> {}
public class Customer : BaseEntity<Customer> {}
public class Product : BaseEntity<Product> {}
Then in my TestProject.BLL layer I have some derived classes
I.e.
public class TestProject.BLL.Customer : TestProject.DAL.Customer {}
public class TestProject.BLL.Product : TestProject.DAL.Product {}
Then, in my UI layer I'm calling my BLL.Customer object.
I get an error saying that the reference to the DAL.Customer object is not added, etc.
I have a reference to the BLL project from my UI project and a reference to the DAL from my BLL project.
Why is the UI layer complaining that it knows of the DAL layer when it's not referenced in that project?
Also, as a side question, does this look like a "good" design?
Thanks all!
Goosey
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Craig 是正确的 - 您的 UI 正在引用 POCO 实体类型。但我会详细说明一下。
如果您的 UI 项目正在引用 BLL 程序集,而该程序集正在引用 DAL 程序集并且没有公开公开该 DAL 程序集中的任何成员,那么你说的就是正确的。但这里发生的情况并非如此。您正在引用 BLL 程序集,并且该程序集中的类型直接继承自 DAL 类型,因此 DAL 类型对您的 UI 是公开可见的。因此,编译器(正确地)告诉您必须从 UI 项目引用 DAL 程序集。
至于您的“良好设计”问题,这始终取决于上下文。在不了解您的任何背景的情况下,我会犹豫是否创建这样的继承树。 BLL 程序集中的子类的工作是什么?
Craig is correct - your UI is referencing the POCO entity types. But I'll elaborate a bit more.
If you were in a situation where your UI project was referencing the BLL assembly and that assembly was referencing the DAL assembly and not publicly exposing any members from that DAL assembly, then what you are saying would be correct. But that is not what is happening here. You are referencing the BLL assembly and the types in that assembly directly inherit from the DAL types and therefore the DAL types are publicly visible to your UI. Therefore, the compiler is (correctly) telling you that you must reference the DAL assembly from your UI project.
As to your "good design" question, that always depends on context. Without knowing any of your context, I would hesitate to create an inheritance tree like this. What is the job of your sub-classes in the BLL assembly?
您的 UI确实通过
BaseEntity
上的通用类型参数引用 POCO 实体类型。Your UI does reference the POCO entity types -- via the generic type parameter on
BaseEntity
.