Factory Requires template consultation

I am working on wrapping my head around a factory pattern using it in a simple data storage project in my spare time. The idea is to take simple data and store it in a database using a simple factory pattern in VB.NET. I think I have a basic understanding of the pattern itself, however what I am struggling with is how easy it is to fit factory classes into the architecture. I have a standard 3-tier architecture for this project that looks something like this:

Representation

 -Presentation.Common
 -Presentation.DataStorageWebAppName

      

Business

 -BusinessLayer.Common
 -BusinessLayer.DataStorageAppName

      

Data

 -DataLayer.Common
 -DataLayer.DataStorageAppName

      

General

 -Common
 -Common.DataStorageAppName

      

Interfaces

 -Interfaces.Common
 -Interfaces.DataStorageAppName

      

To highlight a specific scenario when I have an application architecture issue, let me give you an example. Let's say that in the business layer, I create a class in the BusinessLayer.DataStorageAppName DLL called Foo. It has an IFoo interface, which is in the Interfaces.DataStorageAppName DLL. To instantiate the Foo class via the IFoo interface using a simple factory pattern, I am now creating a factory class in BusinessLayer.DataStorageAppName and writing a generic / static method to give me an instance via the IFoo interface. Later, as I understand it, I could decide to swap the object that this factory class returns without doing more others (in theory).

To figure it out, it works, but what seems a little scary is that now I am forced to create multiple factory classes: one per DLL, so that I can avoid circular references. Is there a cleaner way to implement these factory classes without resorting to using a third party solution like windsor on a padlock, etc. Etc. There seems to be a fundamental concept missing here. It seems that in the architecture that is responsible for passing object instances, it should be possible to have one "repository".

Thank you in advance!

+1


source to share


3 answers


I used a simpler approach. I have defined an Entity class and I have a factory that creates a list of objects (List <Entity>). I tell the factory what types I want to return, it assumes the type will be a table, properties that will be columns, generate sql, get data, populate a list of new instances of the type.

The factory can also get an Entity and use reflection to update its values ​​in the database.



Now the only thing I need to do is create a set of Entity classes for a given database, which BTW are also data transfer objects.

+1


source


Is there a reason your implementation of the IFoo method and the factory that returns it cannot live in a separate assembly from the BusinessLayer (and therefore you can reference this new assembly from all client assemblies)? If there is a reason, is it possible that IFoo should also be defined in the BusinessLayer (and therefore does not require interface assembly references)?

If your interface really needs to be accessible from more than the Business Layer, and the implementation really relies on the business layer, you need to look at something a little more than a factory method. You can use some of the Inversion of Control / Dependency Injection principles for better separation of concerns.

[Edit in response to subsequent answer from OP] The factory method is not IMO sufficient to be considered an IoC approach. Your best bet is to encapsulate the construction of a specific implementation, either reuse in multiple locations, or just sweep it under the carpet . Dependency breaking is missing for me: since your factory method lives in the same assembly as the callee, in order to change the return specific type, you will need to recompile. Consider the difference between the following two:

public class FooConsumer1
{
    public void DoStuff()
    {
        IFoo myFoo = new Foo();
        myFoo.Bar();
    }
}

      



and

public class FooConsumer2
{
    public void DoStuff()
    {
        IFoo myFoo = FooFactory.getFoo();
        myFoo.Bar();
    }
}

public class FooFactory
{
    public static IFoo GetFoo()
    {
        return new Foo();
    }
}

      

The advantage of the second is that if you create an IFoo in multiple places, you only have one place to change it when you create a SuperFoo class to replace Foo. Also, there is only one place to put the logic if it decides in some way between the two implementations. Another benefit is that if the constructor is complex / ugly or requires some extra code to find configuration settings or so on, it hides the whole thing from the method that uses it.

However, none of them actually (at least in my opinion) help you to break the dependencies between this method and its specific implementation.

+1


source


It doesn't have to be funny to have multiple factory classes if they handle different problems. I would advise against using static factory methods, but rather instantiating the factories you use. Then the factories themselves can implement the interfaces, and maybe some of them go away.

0


source







All Articles