0

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?

PHeiberg
  • 29,411
  • 6
  • 59
  • 81
londondev
  • 231
  • 2
  • 13
  • Where is GetScreenParameters located (in which class) and is it a member method or a class (static) method? Does GetScreenParameters expect any parameters? – PHeiberg Jan 27 '12 at 13:39
  • No, it is not static. GetScreenParameters is in the Form.cs and returns IScreenParameter, the code above is run on Form.cs Ctor. – londondev Jan 27 '12 at 14:52

1 Answers1

0

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());
PHeiberg
  • 29,411
  • 6
  • 59
  • 81
  • Unfortunately it didn't work. Yeah the code is called in Form constructor that also contains GetScreenParameter class. – londondev Jan 28 '12 at 08:43
  • I think you need to give a bit more information on what you mean by "didn't work". Does it give a compilation or run time error? If so what kind of error do you get? I tested a bare bones version of this and it worked on my machine. :-) – PHeiberg Jan 28 '12 at 09:54
  • Yes, it throws exception when I called "ObjectFactory.BuildUp(this)" in form Ctor. It says, No Default Instance defined for PluginFamily .IHYSDataBL. When I commented your code, IHYSDataBL is created in Structure Map container. – londondev Jan 28 '12 at 16:58