Skip to content

Simple source generation based factories for Unity

License

Notifications You must be signed in to change notification settings

blackbone/factories

Repository files navigation

FACTORIES

Simple package which allow to use power of source generators to handle factories pattern.

Installation

Add "com.blacbone.factories": "https://github.com/blackbone/factories.git" to manifest.json

Usage

Use FactoryAttribute on types which will be created from factories like so:

[Factory(typeof(MyBaseClass), "key"]
[Factory(typeof(MyBaseClass), 123]
[Factory(typeof(MyBaseClass), Enum.EnumValue]
class MyClass : MyBaseClass
{
    // .. 
}

Each use of attribute with different combination of base type and key type will lead to registration in different factory.

To create instance call Factory.Create() where needed like so:

void Foo()
{
    var instance1 = Factory<MyBaseClass, string>.Create("key");
    var instance2 = Factory<MyBaseClass, int>.Create(123);
    var instance3 = Factory<MyBaseClass, string>.Create(Enum.EnumValue);
}

In all three cases above instance will be of type MyClass.

Issues and contribution

Feel free to create issues and send PR's. There's no strict requirements.

About

Simple source generation based factories for Unity

Resources

License

Stars

Watchers

Forks

Languages