Simple package which allow to use power of source generators to handle factories pattern.
Add "com.blacbone.factories": "https://github.com/blackbone/factories.git"
to manifest.json
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.
Feel free to create issues and send PR's. There's no strict requirements.