Skip to content

Factory Interface Naming Conventions

Floyd May edited this page May 8, 2015 · 1 revision

Naming Conventions That Factory Interfaces Use

If you're wondering "Why don't my perfectly good bindings work?!" you've come to the right place.

Factory interfaces, in general, should use Create as the method name for factory methods. For instance, let's consider a class Foo:

public class Foo
{
    public Foo(int x){}
    public Foo(int x, int y){}
}

A factory interface that will create instances of Foo might look like this:

public interface IFooFactory
{
    public Foo Create(int x);
    public Foo Create(int x, int y);
}

However, the following interface will likely fail, and will fail with a complaint that it couldn't find a binding for Foo (a bit of a red herring):

public interface IFooFactory
{
    // note that the parameter name doesn't match the constructor param name
    public Foo Create(int someOtherName);

    // the factory extension will look for a named binding
    // with a name of "" (the empty string). If it isn't there,
    // you'll get an exception.
    public Foo Get(int x, int y);
}