문제

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?

도움이 되었습니까?

해결책

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());
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top