views:

605

answers:

3

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 separate 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 2

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

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.

+1  A: 

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.

Adrian K
If I have the Interfaces in a seperate assembly between the data and business layers how does the presentation layer access the properties of the Company Object that is returned from the Business layer.For example (from the presentation layer): Dim company As ICompany = MyCompany.Business.Companies.CreateCompany()For this to work in the Presentation Layer there needs to be a reference to the ICompany Interface class located in the Interface assembly.Is there another way to do this?
focus.nz
focus.nz - have a look at my answer as I think I explain it there. http://stackoverflow.com/questions/2466293/n-tier-architecture-structure-with-multiple-projects-in-vb-net/2467026#2467026
Enigmativity
@focus.nz - The definition of the interfaces doesn't have to be in the same place as the definition of entities/types it uses, you could have those in a different (central) location (and hence visibility). It also depends if you want to use the same types between the DAL and BL, and, between the BL and UI (for example).
Adrian K
Enigmativity
+1  A: 

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.)

Michael Rodrigues
+3  A: 

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.

Enigmativity
I'll give that a try, thanks.
focus.nz
+1 You forgot to mention that after a while half of a `Common` lib falls into disuse too and nobody ever untangles it. You might also be interested in the ideas (mainly in my comments IMNSHO) in http://stackoverflow.com/questions/925453/wcf-service-proxy-name-namespace-naming-strategy/2430056#2430056
Ruben Bartelink
+1 thanks! We just implemented something similar, good to see others who have thought it through and came to same structure like us.
Ralph Willgoss