user804401
user804401

Reputation: 1994

StructureMap pass arguments in constructor

I have a constructor with some artuments

public class AppEngine:IAppEngine
{
    private IGreeter _greeter;
    private string _str;
    public AppEngine(IGreeter greeter,string str)
    {
        _greeter = greeter;
        _str = str;
    }
    public string Run()
    {
        return _greeter.Greeting()+":"+_str;
    }
}

Here from the factory i want to get instance

 var obj = ObjectFactory.GetInstance<IAppEngine>();

Here I want to pass Arguments that the constructor is accepting. how could i do this.

Regards

Upvotes: 2

Views: 2587

Answers (2)

user3064969
user3064969

Reputation: 7

//Arguments Constructor    
Example(int id,String Name){    //Define    }//main method  
//Object Creation    
//Object  Creation for Default Constructor
Example e1=new Example();
//Object Creation for Arguments Constructor
Example e2=new Example(101,"kathik");}

Upvotes: -1

Rob West
Rob West

Reputation: 5241

If you want to specify the argument when you call ObjectFactory you can do it like this:

ObjectFactory.With("str").EqualTo(someValue).GetInstance<IAppEngine>();

If you need to do multiple arguments you can just chain these together. Note that you can also define a constructor value for all instances when inializing like this:

ForRequestedType<IAppEngine>().Use<AppEngine>().WithCtorArg("str").EqualTo(someValue);

Upvotes: 8

Related Questions