Frage

I have a class ScreenParameter (implement IScreenParameter) that stores all form values in windows form. This class is initiated when user populate all the fields and click the button. The class in Bussiness Layer waits IScreenParameter in its constructor. I am using Structure map to inject the interface and it needs to be created on its context. I have a method(GetScreenParameters) that creates and populates the interface. I want to give method name instead of concreate class while configuring Structure map and tried this.

   ObjectFactory.Initialize(initialize =>
        {
            initialize.For<IScreenParameter>().Use(GetScreenParameters);

        });

But it is not working. Can you please help how to achieve this?

War es hilfreich?

Lösung

Assuming the code is called from an instance of the Form class that's containing the GetScreenParameters method you can use the following syntax:

initialize.For<IScreenParameter>().Use(() => GetScreenParameters());

This will cause the GetScreenParameters method to be called every time you want to get an instance of IScreenParameter. If you want the same instance every time you should be able to register it as a singleton with:

initialize.For<IScreenParameter>().Use(GetScreenParameters());
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top