I have the following class which exposes a public factory method instead of a public constructor:
public class SomeDependency
{
private readonly string _someValue;
private SomeDependency(string someValue)
{
_someValue = someValue;
}
public static SomeDependency CreateSomeDependency(string someValue)
{
return new SomeDependency(someValue);
}
}
I configure it in the container to have a transient lifestyle and to be created via the static method:
Container.Register(Component.For<SomeDependency>()
.UsingFactoryMethod(k => SomeDependency.CreateSomeDependency("a value"))
.LifestyleTransient());
All components that this dependency is injected into are configured to be transient as well.
Will the container dispose this dependency when objects that depend on it fall out of scope or will this dependency fail to be disposed and make everything stick around?
It will be disposed if:
IDisposable
, which it currently does not, andContainer.Release()
for the component that caused SomeDependency to be instantiated.You cannot simply rely on the component going out of scope for disposal to be triggered.
If you are referring to garbage collection rather than disposal, then your current implementation may work ok with the object becoming available for GC when it goes out of scope. In most cases, transient components that do not implement IDisposable will not be tracked by the container but it can depend on what other facilities you are using in your container. Therefore, it is always best to ensure Release
is called.