C#C
C#3y ago
78 replies
ero

❔ Creating a "Lazy" Factory with a Fluent API

I'm looking to create a kind of factory, which creates commands instead of the actual results. The results are then conditionally returned upon a call to the factory's
Run()
or
Execute()
method. The reason for the "laziness" of the factory is that executing one of these commands may fail (
TryExecute()
). This command is then skipped (the failure may be logged) and tried again later, at which point it may no longer fail.

Assume these result objects;
interface IFoo { }
abstract class FooBase : IFoo { }

class Foo<T> : FooBase { }
class Bar<T> : FooBase { }
class Qux    : FooBase { }


These objects are all "created" differently (as in, they have different constructor parameters), meaning the factory must also have
MakeFoo<T>
,
MakeBar<T>
, and
MakeQux
.
But unfortunately, that's not it. All implementations of
FooBase
take an instance of
IFoo
as a sort-of parent. This can be nested infinitely. Let's assume something like this;
Foo<int> foo1 = new(/* */);
Foo<long> foo2 = new(foo1, /* */);
Foo<byte> foo3 = new(foo2, /* */);


This means that the factory needs some way to create a parent which then has at least 1 or more children.
I also want to modify some of the objects' properties fluently;
factory.MakeFoo<T>(/* */).Prop1(/* */).Prop2(/* */);


Potential implementations I've considered use a setup such as this;
interface IMakeFooCommand { }
abstract class MakeFooCommandBase : IMakeFooCommand { }

class MakeFooCommand<T> : MakeFooCommandBase { }
class MakeBarCommand<T> : MakeFooCommandBase { }
class MakeQuxCommand    : MakeFooCommandBase { }

interface ILazyFactory { }
class LazyFactory : ILazyFactory { }
Was this page helpful?