Is there a clever way to use transient object in a singleton?
I have a builder class which is stateful.
interface IFilterBuilder
{
AddFilter1();
AddFilter2();
//etc
IEnumerable<Filter> FilterList { get; }
}
class FilterBuilder : IFilterBuilder { //implementation }
For the moment I'm creating its instances with new keyword because it's
short lived and it's used inside a method of a singleton object.
I know I can inject a factory type like Func<IFilterBuilder> to singleton
and resolve it with the container but I really don't want to have a
filterBuilderFactory.
Is there a way to do some kind of magic with interception like this:
class SingletonClass
{
[TransientDependency]
public IFilterBuilder FilterBuilder
get
{
//magic.gif
}
set
{
//some stuff for testability
}
}
So when I need to use it in a method, I get a new instance.
IFilter filterBuilder = FilterBuilder;
filterBuilder.AddFilter1();
return filterBuilder.FilterList;
It will have to release the object when its out of scope and I should have
a way to replace it with a mock in my tests.
No comments:
Post a Comment