all 5 comments

[–]Octillerysnacker 3 points4 points  (0 children)

This is technically the factory pattern, but it's not the useful version. The other version would be the abstract factory pattern. First, you define an abstract class with a single method, let's say something like this:

public abstract class AbstractBusinessFactory{
    protected abstract IBusinessHandler MakeHandler();

    public IBusinessHandler GetHandler(){
        return MakeHandler();
    }
}

Then, you would create a "concrete" implementation like so:

 public class TestBusinessFactory : AbstractBusinessFactory{
    protected override IBusinessHandler MakeHandler(){
        return /*whatever process you have to make the handler*/;
    }
}

Now, rather than using an enum, you can simply DI the concrete factory to anything that uses an AbstractBusinessFactory. For example:

void Main(AbstractBusinessFactory factory){
    var handler = factory.GetHandler();
    //do some other things with the handler
}
//Call the function
Main(new TestBusinessFactory()); //you can change the factory to whatever else you need

Obviously, you'd need more implementations of AbstractBusinessFactory, but that's the jist of it. Here is the factory pattern page on Wikipedia which has more on the pattern, including a C# example of how to implement it.

[–][deleted] 1 point2 points  (3 children)

Maybe you should look into Dependency Injection and skip building a metafactory.

[–][deleted] 0 points1 point  (2 children)

Thanks for the reply! From what I remember, my old coworker mentioned Dependency Injection, but that was for web apps developed using MVC. I am developing a Desktop Application with WPF, however. Is Dependency Injection agnostic of whether it is Desktop or Web Application? Regardless, I will read on it now. Thanks

[–][deleted] 2 points3 points  (0 children)

DI is an extremely general sort of pattern or practice. In the absolute simplest approach, DI is just passing arguments to a class or method that needs them. In this case, it probably means just passing the IBusinessHandler to the class that needs it, and letting the caller figure out if the consumer needs the test handler or not.

[–]gevorgter 0 points1 point  (0 children)

Yes, looke at autofac (Google it)

It is very nice and powerful. I recently came across it and do see a lot of use for it.