Giter Club home page Giter Club logo

Comments (3)

YairHalberstadt avatar YairHalberstadt commented on May 28, 2024 1

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.

YairHalberstadt avatar YairHalberstadt commented on May 28, 2024

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.

YairHalberstadt avatar YairHalberstadt commented on May 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.