Giving method name instead of concrete class name in Structure map

153 views Asked by At

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?

1

There are 1 answers

3
PHeiberg On BEST ANSWER

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());