N层架构 - 在VB.NET多个项目结构 [英] N-Tier Architecture - Structure with multiple projects in VB.NET
问题描述
我想在最好的办法一些建议在以下情况下使用...
I would like some advice on the best approach to use in the following situation...
我将有一个Windows应用程序和Web应用程序(presentation层),这些都将进入一个共同的业务层。业务层将看一个配置文件来查找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?).
的原因,用于创建到所述数据访问层在运行时的基准是因为应用程序将与这取决于客户端使用不同的第三方计费系统对接。所以,我想有一个单独的数据访问层支持的每个会计制度。这些可能是单独设置的项目,每个客户端将使用一个或另一个,他们不会需要在两者之间进行切换。
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.dll 和 MyCompany.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
任何意见/建议将不胜AP preciated。
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
是preferable到 MyCompany.Business.dll
。它是那么容易分裂成应用程序的子部件,并从多个应用程序中重用code。
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
从你的描述,似乎在 AccountingSys1
和 AccountingSys2
组件都有一个共同的合同,因此只有一个合同大会这两个实施组件。
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.
合同组件应该重新present你的设计,不是你的实施,不仅是因为设计的改变而改变。你应该避免任何显著code(避免错误),你应该约束code到接口,枚举,异常,属性,事件参数和结构 - 所有没有显著$ C $角
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.
这种结构的另一个好处是,单元测试更简单,可以根据合同,而不是实施,帮助你写干净,可测试code。
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.
这可能看起来像很多组件,但你保持你的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屋!