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 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 s开发者_开发知识库eparate setup projects, each client would use one or the other, they wouldn't need to switch between the two.
Projects:
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 2The 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
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
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.
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.
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.
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.)
精彩评论