Should you use a partial class across projects?

后端 未结 8 482
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 16:42

I have a class library with all my database logic. My DAL/BLL.

I have a few web projects which will use the same database and classes, so I thought it was a good

相关标签:
8条回答
  • 2020-12-03 17:05

    No.You cant write partial classes in different projects.Because at a time compiler gets a single project for compilation and so scans for list of classes,methods,fields etc in that project only.So if you have some parts of the partial class in other projects,compiler cant find those.

    0 讨论(0)
  • 2020-12-03 17:16

    I can't answer your question about the best way to organize your layers, but I can try to answer your question about how best to emulate partial classes.

    Here are a few thoughts:

    • The first thing that springs to mind is inheritance. It's not necessarily the best solution always, but you may not have a choice since you may need to be able to have your objects be able to be treated like the base class.
    • Composition is also a good choice (that is, wrapping the class in another class). This gives you a little bit nicer decoupling from your DAL, but can be tedious to implement.
    • If you really just need to add a method or two onto an existing class, you might also consider using an extension method, but these can quickly create spaghetti code if you use them too much.
    0 讨论(0)
  • 2020-12-03 17:20

    Partial classes have to exist in the same assembly. Otherwise, how would the compiler decide where to merge the partial classes to?

    0 讨论(0)
  • 2020-12-03 17:23

    I agree with Jon Skeet's answer.

    I don't think it would be a good choice to approach an issue like this anyway. There are good design patterns out there already that demonstrate the best way to split your tiers/layers of code, and this is just a little syntactic sugar so that Microsoft could make the WinForms/WebForms designer files separate and prevent people from breaking them.

    0 讨论(0)
  • 2020-12-03 17:25

    While I agree with you Neil when it comes to pre-linq development, I also wish I could be able to do this in order to split up bussiness logic from partial classes generated by Linq2SQL designer. For example:

    Northind.DAL (prj)
    -NorthindDataContext (EntityNamespace set to "Northwind.BLL")
    --Product() (Entity, partial class auto-generated)
    --Category() (Entity, partial class auto-generated)
    --Supplier() (Entity, partial class auto-generated)
    
    Northind.BLL (prj)
    -Product() : IMyCustomEnityInterface, BaseEntity (override OnValidate(), etc)
    -Category() : IMyCustomEnityInterface, BaseEntity (override OnValidate(), etc)
    -Supplier() : IMyCustomEnityInterface, BaseEntity (override OnValidate(), etc)
    

    Unfortunately, we can not do this... actually I'd love to know what the recommended way of splitting up layers/tiers when using LINQ.

    0 讨论(0)
  • 2020-12-03 17:26

    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.

    (Your original DAL file would have to declare the class to be partial as well, by the way.)

    0 讨论(0)
提交回复
热议问题