9

In Simple Injector I can do the following:

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

What I am doing here is saying that when IAuctionContext is found, replace it with this new AuctionContext. The problem is that with the call to RegisterSingle, only a single instance of AuctionContext will be used. What I'd like it to be able to pass in a Uri parameter as above but not have the single instance but allow a new instance each time.

How is this possible?

Steven
  • 166,672
  • 24
  • 332
  • 435
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304
  • I've edited your question and removed your signature per http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts. Please refrain from using signatures. =) – J. Steen Dec 06 '12 at 15:38
  • I will keep this in mind for next time, thanks. – Sachin Kainth Dec 06 '12 at 16:10

1 Answers1

18

The value you are trying to inject is a simple hard-coded value. For constant values like hard-coded values and configuration values, just use the Register method:

var uri = new Uri("http://localhost:60001/AuctionDataService.svc/");

container.Register<IAuctionContext>(() => new AuctionContext(uri));

The Register method ensures a new instance is returned each time.

When it comes to injecting values that could change during the course of the application, please read this article about injecting runtime data.

Steven
  • 166,672
  • 24
  • 332
  • 435