views:

25

answers:

1

Hi,

Here is the c# code:

var server = ******* some internal logic to determine server name ****
var username = ******* some internal logic to determine user name ****
var password = ******* some internal logic to determine password ****

ObjectFactory.Initialize(x =>
{                                            
 x.For<IService<bool>>().Add<Service>()
.Ctor<string>("server").Is(server)
.Ctor<string>("username").Is(username)
.Ctor<string>("password").Is(password)
}

This works great but I would like to move this to configuration file as I don't want to reference to concerete type directly in my code.

There isn't much documentation about 2.6. I couldn't find how to handle this in configuration.

Ideally I would like to inject the server, username, and password paramenters in to ObjectFactory and some how use these parameters in the configuration.

(PS: It doesn't necessary need to be with StructureMap any IAC container that can support this scenario will be welcome.)

Thanks

A: 

The simplest way to handle this type of case would be to introduce a new type to provide the primitive constructor args, and register that in code. Then you can register the service, and that new type can be auto wired by the container. This also allows you to move the logic to get the server, username and password into this new object and out of the container configuration.

Robin Clowers
Hi Robin, thanks for your reply. I've just implemented the same way you described and yes it looks much cleaner. But at the end I've moved to CastleWindsor as its documentation is much more complete. I really struggled with StructureMap's documentation and there is not much help aspecially for xml configuration, looks like lots of people are configuring it by code (which makes sense if you don't have specific requirements). Thanks Again.
Zerdush