N 层架构 - 在 VB.NET 中具有多个项目的结构 [英] N-Tier Architecture - Structure with multiple projects in VB.NET

查看:31
本文介绍了N 层架构 - 在 VB.NET 中具有多个项目的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想就在以下情况下使用的最佳方法提供一些建议...

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

我将有一个 Windows 应用程序和一个 Web 应用程序(表示层),它们都将访问一个公共业务层.业务层将查看配置文件以查找 dll(数据层)的名称,它将在运行时创建引用(这是最好的方法吗?).

I will have a Windows Application and a Web Application (presentation layers), these will both access a common business layer. The business layer will look at a configuration file to find the name of the dll (data layer) which it will create a reference to at runtime (is this the best approach?).

在运行时创建对数据访问层的引用的原因是,应用程序将根据客户端使用的内容与不同的 3rd 方会计系统进行交互.所以我会有一个单独的数据访问层来支持每个会计系统.这些可以是单独的设置项目,每个客户都会使用一个或另一个,他们不需要在两者之间切换.

The reason for creating the reference at runtime to the data access layer is because the application will interface with a different 3rd party accounting system depending on what the client is using. So I would have a separate data access layer to support each accounting system. These could be separate setup projects, each client would use one or the other, they wouldn't need to switch between the two.

项目:

MyCompany.Common.dll - 包含接口,所有其他项目都引用了这个.
MyCompany.Windows.dll - Windows 窗体项目,引用 MyCompany.Business.dll
MyCompany.Web.dll - 网站项目,引用 MyCompany.Business.dll
MyCompany.Busniess.dll - 业务层,引用 MyCompany.Data.*(在运行时)
MyCompany.Data.AccountingSys1.dll - 会计系统 1 的数据层MyCompany.Data.AccountingSys2.dll - 会计系统 2 的数据层

MyCompany.Common.dll - Contains interfaces, all other projects have a reference to this one.
MyCompany.Windows.dll - Windows Forms Project, references MyCompany.Business.dll
MyCompany.Web.dll - Website project, references MyCompany.Business.dll
MyCompany.Busniess.dll - Business Layer, references MyCompany.Data.* (at runtime)
MyCompany.Data.AccountingSys1.dll - Data layer for accounting system 1 MyCompany.Data.AccountingSys2.dll - Data layer for accounting system 2

项目 MyCompany.Common.dll 将包含所有接口,每个其他项目都会引用这个接口.

The project MyCompany.Common.dll would contain all the interfaces, each other project would have a reference to this one.

Public Interface ICompany
    ReadOnly Property Id() as Integer
    Property Name() as String
    Sub Save()
End Interface

Public Interface ICompanyFactory
    Function CreateCompany() as ICompany
End Interface

项目 MyCompany.Data.AccountingSys1.dllMyCompany.Data.AccountingSys2.dll 将包含如下类:

The project MyCompany.Data.AccountingSys1.dll and MyCompany.Data.AccountingSys2.dll would contain the classes like the following:

Public Class Company
    Implements ICompany

    Protected _id As Integer
    Protected _name As String

    Public ReadOnly Property Id As Integer Implements MyCompany.Common.ICompany.Id
        Get
            Return _id
        End Get
    End Property

    Public Property Name As String Implements MyCompany.Common.ICompany.Name
        Get
            Return _name
        End Get
        Set(ByVal value as String)
            _name = value
        End Set
    End Property

    Public Sub Save() Implements MyCompany.Common.ICompany.Save
        Throw New NotImplementedException()
    End Sub

End Class

Public Class CompanyFactory
    Implements ICompanyFactory

    Public Function CreateCompany() As ICompany Implements MyCompany.Common.ICompanyFactory.CreateCompany
        Return New Company()
    End Function

End Class

项目 MyCompany.Business.dll 将提供业务规则并从数据层检索数据:

The project MyCompany.Business.dll would provide the business rules and retrieve data form the data layer:

Public Class Companies

    Public Shared Function CreateCompany() As ICompany
        Dim factory as New MyCompany.Data.CompanyFactory
        Return factory.CreateCompany()
    End Function    

End Class

任何意见/建议将不胜感激.

Any opinions/suggestions would be greatly appreciated.

推荐答案

一些评论.

我会避免使用 MyCompany.Common.dll 程序集.这些通常最终会被各种不相关的东西填满,然后经常需要重新构建所有程序集.

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.

我会用应用程序名称和公司名称命名您的程序集.MyCompany.MyApplication.Business.dll 优于 MyCompany.Business.dll.这样就可以更轻松地将应用程序拆分为子部分并重用来自多个应用程序的代码.

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

从您的描述看来,AccountingSys1AccountingSys2 程序集共享一个公共合同,因此两个实现程序集只有一个合同程序集.

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.

此规则的例外情况是在创建继承层次结构时.例如,您可以创建一个 *.Data.AccountingSys.dll 来定义两个特定会计系统程序集的基类.

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.

如果您可以遵循上述所有内容,那么您将需要实现某种依赖注入方法,以便能够从合约程序集中的接口创建对象的实例.您可以使用现有的 DI 框架或创建第三组 *-Factory.dll 程序集,其中包含您的工厂方法.

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.

这篇关于N 层架构 - 在 VB.NET 中具有多个项目的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆