2

If I have a collection which contains a list of models and want to create corresponding views for each model, what is the best way to do this using IOC (inversion of control)?

I see two ways:

  1. Use a factory pattern. I would only pass in my model collection and build views as I need them with a factory that is passed in via IOC.

  2. Create a another class which would have a list of all the views I need, pre-built by the IOC. Then I just pick the view I want from the list.

Which way do you think is better?

Nick Lang
  • 859
  • 2
  • 9
  • 15

1 Answers1

2

If you can build the Views in advance the best solution might be to simply inject a list (IEnumerable) of all the Views into each consumer and perhaps use the Specification Pattern to select the appropriate View from that list.

If, on the other hand, you need run-time values before you can instantiate each View, a Factory is the correct solution.

Community
  • 1
  • 1
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736