如果您使用跨项目的分部类? [英] Should you use a partial class across projects?
问题描述
我有一个类库,用我所有的数据库逻辑。我DAL / BLL。
I have a class library with all my database logic. My DAL/BLL.
我将使用相同的数据库和类的几个web项目,所以我认为这是一个好主意,抽象数据层到自己的项目。
I have a few web projects which will use the same database and classes, so I thought it was a good idea to abstract the data layer into its own project.
然而,当涉及到添加功能的类我想方法添加到某些类型的某些项目。
However, when it comes to adding functionality to classes for certain projects I want to add methods to certain classes.
例如,我的数据层产品和SomeItem对象:
For example, my data layer has Product and SomeItem objects:
// Data Access Layer project
namespace DAL {
public class Product {
//implementation here
}
public class SomeItem {
//implementation here
}
}
在一个项目我想补充一点,被不同的内容项的接口,所以我有一个叫做类:
In one project I want to add an interface that is used by different content items, so I have a class called:
// This is in Web Project
namespace DAL {
public partial class Product : ICustomBehaviour {
#region ICustomBehaviour Implementation
TheSharedMethod();
#endregion
}
}
这是个好主意写的部分类一个单独的项目使用的相同的命名空间(创建依赖性)?如果它是一个坏主意,我怎么能得到这个类型的功能工作?
Is it a good idea to write a partial class in a separate project (creating a dependency) using the same namespace? If it's a bad idea, how can I get this type of functionality to work?
这似乎并不想在编译时将它们合并,所以我不知道我做错了。
It doesn't seem to want to merge them at compile time, so I'm not sure what I'm doing wrong.
推荐答案
您不能写跨项目的分部类。一个分部类是一个编译时间只有一块语法糖 - 全类型单一组装结束,即一个项目
You can't write a partial class across projects. A partial class is a compile-time-only piece of syntactic sugar - the whole type ends up in a single assembly, i.e. one project.
(您的原始文件DAL必须声明类是部分的为好,顺便说一句。)
(Your original DAL file would have to declare the class to be partial as well, by the way.)
这篇关于如果您使用跨项目的分部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!