Comments (3)
The following is a possible solution which can be used without adding any new features:
Define your own interface e..g INeedsInitialization
, and a module that calls it using a decorator:
public interface INeedsInitialization
{
/// Must be safe to call multiple times
public ValueTask InitializeAsync();
}
public class InitializationModule
{
[DecoratorFactory] public static async ValueTask<T> InitializeAsync(T t) where T : INeedsInitialization
{
await t.InitializeAsync();
return t;
}
}
Then if you import that module, you can automatically initialize anything that implements that interface.
If there's any type which you don't control and need to initialize you can make a custom decorator specifically for it:
public class SomeClass
{
public ValueTask PrepareAsync();
}
[Register(typeof(SomeClass))]
public class SomeClassModule
{
[DecoratorFactory] public static async ValueTask<SomeClass> InitializeAsync(SomeClass someClass)
{
await someClass.PrepareAsync();
return someClass;
}
}
This also allows you to do much more complex initialization of a type if necessary, but adds a bit of boilerplate.
Would you consider this sufficient?
from stronginject.
I think I would like to do this at some point, but it's not highest priority for me.
Hopefully I'll make a product roadmap soon so people can see what I plan to work on and in which order.
from stronginject.
What I think I will do is as follows:
In the [RegisterAttribute]
I will add an optional parameter initializeMethod
. This must match the name of a public method on the registered type. The method must return void
, Task
, or ValueTask
.
This means you can do this:
public class A
{
public void SetUp(){}
}
[Register(typeof(A), initializeMethod: nameof(A.SetUp))]
public class Container : IContainer<A>{}
I will keep the IRequiresInitialization
interfaces, for cases where you want to make sure you don't forget to register it with an initialize method.
from stronginject.
Related Issues (20)
- Optionally generated service location API HOT 2
- Inject Func<Owned<T>> parameters HOT 1
- [Feature/Idea] Support for primary constructor generator HOT 2
- Innermost hasAwaitStarted catch block is a no-op and could be omitted HOT 3
- Update xamarin sample to maui
- 'registerAs' parameter for FactoryAttribute HOT 9
- IOwned<out T> HOT 4
- Convenience methods for creating Owned<T> instances HOT 3
- Permitting null instead of empty delegate for Owned<T> and AsyncOwned<T> HOT 2
- Add diagnostic if any registered method return types are by ref.
- Circular dependency check is too restrictive for delegates HOT 23
- SI1103 warning location forces suppression to be all or nothing HOT 6
- Separate Microsoft DI from Asp.Net Core HOT 2
- Convert to IIncrementalGenerator HOT 18
- Retrieving design-time info about registrations HOT 5
- StrongInject Source Generator API HOT 1
- Confusing use of the term 'delegate parameters' in the readme HOT 2
- Support decorator factories that aren't gratuitously generic HOT 4
- Misleading info in README HOT 2
- Generic interface registration HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stronginject.