You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Once a lot of a codebase has necessarily taken a dependency on Owned<T> so that StrongInject can wire everything up, many places remain that need to create their own Owned instances. This is handy for many tests and smaller executables in a project that don't really need StrongInject and can do their own wiring with a few calls.
Instead of: new Owned<SomePotentiallyLongTypeName>(value, () => { })
Friendly: Owned.WithoutDisposal(value)
Instead of: new Owned<SomePotentiallyLongTypeName>(value, value.Dispose)
Friendly: Owned.WithDisposal(value)
Instead of: new Owned<T>(value, () => (value as IDisposable)?.Dispose())
Friendly: Owned.WithDisposal(value)
Here's a rough draft for what I've already found useful, and I'm happy to do the implementation:
namespaceStrongInject;publicstaticclassOwned{/// <summary>/// Nothing happens when the <see cref="Owned{T}"/> is disposed, even if <paramref name="value"/>/// is disposable./// </summary>publicstaticOwned<T>WithoutDisposal<T>(Tvalue){returnnew(value,dispose:null);}/// <summary>/// <para>/// When the <see cref="Owned{T}"/> is disposed, <paramref name="value"/> is disposed. If/// <paramref name="value"/> does not implement <see cref="IDisposable"/>, the behavior is/// the same as <see cref="WithoutDisposal{T}(T)"/>./// </para>/// </summary>publicstaticOwned<T>WithDisposal<T>(Tvalue){// T is not constrained to IDisposable. If it was, call sites using generic types would be penalized.returnnew(value,dispose:valueisIDisposabledisposable?disposable.Dispose:null);}/// <summary>/// <para>/// When the <see cref="Owned{T}"/> is disposed, <paramref name="value"/> is disposed first and then/// the items in <paramref name="alsoDispose"/> are disposed in the order they appear./// </para>/// <para>/// If <paramref name="value"/> does not implement <see cref="IDisposable"/>, the items in/// <paramref name="alsoDispose"/> will still be disposed./// </para>/// </summary>publicstaticOwned<T>WithDisposal<T>(Tvalue,paramsIDisposable[]alsoDispose){returnnew(value,dispose:()=>{(valueasIDisposable)?.Dispose();foreach(vardisposableinalsoDispose)disposable.Dispose();});}}
An AsyncOwned static class would be similar.
The text was updated successfully, but these errors were encountered:
I'm not sure about this. At the moment StrongInject only contains types that help with DI itself. Do we want to start adding in types which aren't actually helpful for DI, but for testing code which uses it, especially when it's super simple to write this yourself?
I would look at this as having to do with the definition of Owned<T> as a library type and as being completely separate from the source generator. What would it look like if the BCL maintained this type, or someone else?
On the other hand it's not a huge deal if I copy and paste this file as needed.
Once a lot of a codebase has necessarily taken a dependency on
Owned<T>
so that StrongInject can wire everything up, many places remain that need to create their own Owned instances. This is handy for many tests and smaller executables in a project that don't really need StrongInject and can do their own wiring with a few calls.Instead of:
new Owned<SomePotentiallyLongTypeName>(value, () => { })
Friendly:
Owned.WithoutDisposal(value)
Instead of:
new Owned<SomePotentiallyLongTypeName>(value, value.Dispose)
Friendly:
Owned.WithDisposal(value)
Instead of:
new Owned<T>(value, () => (value as IDisposable)?.Dispose())
Friendly:
Owned.WithDisposal(value)
Instead of:
new Owned<SomeType>(value, () => { value.Dispose(); dependency1.Dispose(); dependency2.Dispose(); })
Friendly:
Owned.WithDisposal(value, dependency1, dependency2)
orFriendly:
Owned.WithDisposal(value, alsoDispose: new[] { dependency1, dependency2 })
Here's a rough draft for what I've already found useful, and I'm happy to do the implementation:
An AsyncOwned static class would be similar.
The text was updated successfully, but these errors were encountered: