Pros and cons of having a static repository factory? RRS feed

  • Question

  • Hi,

    I have a pretty simple layer struture:
    DataRepository (facade layer)

    I'm using a factorty pattern for the data repository layer and are always returning interfaces. The factory class only has static methods. To handle unit testing the factory class has properties for setter injection.

    A simple example:

        public static class RepositoryFactory
            public static ICustomerRepo CustomerRepo { get; set; }
            public static ICustomerRepo CreateCustomerRepository()
                if (CustomerRepo != null)
                    return CustomerRepo;
                return new CustomerRepo();

    The pros of this design I think is that there's one less class to mock, the factory class since you inject what it should return during testing. And less object creation.

    Is this a good approach to have a static factory class with setting injection for unit testing, or should I refactor the factory class as an instance class and mock the factory class as well?

    Pros and cons?


    MCTS | MCPD Web
    Monday, August 24, 2009 8:01 AM

All replies

  • http://www.coderanch.com/t/100289/OO-Patterns-UML-Refactoring/difference-between-factory-pattern-abstract
    Sunday, August 30, 2009 7:05 AM
  • Sterne
    One con that comes to my mind is if you have to subclass your factory to support family of object creators, you will not be able to subclass your static classes or override static methods. 
    However in simple use cases, I think it does not make much difference whether you define static factory or instance factory. The performance overhead of having a instance factory in most situations is negligible if that is what you are worried about.
    Sunday, August 30, 2009 1:28 PM