N-Tier Architecture - Structure with multiple projects in VB.NET

后端 未结 3 1942
粉色の甜心
粉色の甜心 2021-02-04 18:07

I would like some advice on the best approach to use in the following situation...

I will have a Windows Application and a Web Application (presentation layers), these w

相关标签:
3条回答
  • 2021-02-04 18:23

    A few comments.

    I would avoid having a MyCompany.Common.dll assembly. These typically end up getting filled with all sorts of unrelated things which then get changed often requiring a rebuild of all of your assemblies.

    I would name your assemblies with the application name as well as the company name. MyCompany.MyApplication.Business.dll is preferable to MyCompany.Business.dll. It is then easier to split applications into sub parts and to reuse code from multiple applications.

    It's best to have separate contract assemblies for each type of implementation assembly you're going to have. In your case I would suggest the following:

    MyCompany.MyApplication.Windows-Contract.dll
    MyCompany.MyApplication.Windows.dll
    
    MyCompany.MyApplication.Web-Contract.dll
    MyCompany.MyApplication.Web.dll
    
    MyCompany.MyApplication.Business-Contract.dll
    MyCompany.MyApplication.Business.dll
    
    MyCompany.MyApplication.Data-Contract.dll
    MyCompany.MyApplication.Data.AccountingSys1.dll
    MyCompany.MyApplication.Data.AccountingSys2.dll
    

    From your description it appears that the AccountingSys1 and AccountingSys2 assemblies share a common contract hence only one contract assembly for the two implementation assemblies.

    Contract assemblies should represent your design, not your implementation, and only change because of design changes. You should avoid having any "significant" code (to avoid bugs) and you should constrain the code to interfaces, enums, exceptions, attributes, event args, and structs - all with no "significant" code.

    When setting up assembly references you should ensure that assemblies only ever reference contract assemblies, like so:

    Data.AccountingSys1
        Data-Contract
    
    Data.AccountingSys2
        Data-Contract
    
    Business
        Business-Contract
        Data-Contract
    
    Windows
        Windows-Contract
        Business-Contract
        Data-Contract (maybe)
    
    Web
        Web-Contract
        Business-Contract
        Data-Contract (maybe)
    

    As a result implementation assemblies never have a dependency on other implementation assemblies. When an implementation changes you only have one assembly to rebuild.

    The exception to this rule is when creating inheritance hierarchies. For example, you may create a *.Data.AccountingSys.dll to define base classes for the two specific accounting system assemblies.

    If you can follow all of the above then you will need to implement some sort of dependency injection approach to be able to create instances of objects from the interfaces in the contract assemblies. You could use an existing DI framework or create a third set of *-Factory.dll assemblies that contain your factory methods.

    A further benefit of this kind of structure is that unit testing is much simpler and can be based on the contracts rather than the implementation, helping you to write clean, testable code.

    This may seem like a lot of assemblies, but the benefits you get from keeping your code from creating nasty dependencies will significantly reduce the chance that your project will become too complex and will help drive good quality as you go. A little pain now will eliminate so much pain later.

    0 讨论(0)
  • 2021-02-04 18:30

    Your general approach is sound :)

    You could consider putting all the interfaces in a seperate assembly (dll) (strictly speaking the interface sits between the business logic and the data access implementation - they should be the only things that have access to the interface), but in the grand scheme of things that might not be such a big deal.

    Personally I'd have one shared factory method that returned an object, and just cast it appropriately when used.

    0 讨论(0)
  • 2021-02-04 18:42

    It's an excellent approach! I've use it in one of our systems at work, and it has proved reliable, east to maintain and allows us to quickly add extra interfaces when needed (e.g. when we need to interface with another accounting system from a company that we acquired.)

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