A simple injector passes hard-coded values ​​to the constructor

In Simple Injector, I can do the following:

container.RegisterSingle<IAuctionContext>(() => new AuctionContext( new Uri("http://localhost:60001/AuctionDataService.svc/"))); 

What I'm doing here says that when an IAuctionContext found, replace it with the new AuctionContext . The problem is that when calling RegisterSingle , only one instance of AuctionContext will be used. What I would like for it to be able to pass the Uri parameter as above, but not have a single instance, but allow a new instance each time.

How is this possible?

+8
c # dependency-injection ioc-container simple-injector
source share
1 answer

The value you are trying to enter is a simple hard coded value. For constant values, such as hardcoded values ​​and configuration values, simply use the Register method:

 var uri = new Uri("http://localhost:60001/AuctionDataService.svc/"); container.Register<IAuctionContext>(() => new AuctionContext(uri)); 

The Register method ensures that every instance is returned every time.

When it comes to values ​​that can change throughout the application, read this article on entering runtime data .

+18
source share

All Articles