Advertisement
Guest User

Untitled

a guest
Jun 15th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. Just a quick recapitualtion:
  2. We have
  3. public interface IModalDialog
  4. - implemented by two Windows-derived classes: SimpleDialogWindow, SimpleDialogWindow2
  5. public interface IModalDialogService
  6. - implemented by: public class ModalDialogService : IModalDialogService
  7.  
  8. Variant 1.
  9. <code>
  10. IUnityContainer container = new UnityContainer();
  11.  
  12. container.RegisterType<IModalDialogService, ModalDialogService>(new ContainerControlledLifetimeManager());
  13. container.RegisterType<Func<string, IModalDialog>>
  14. (
  15. new InjectionFactory(
  16. c => new Func<string, IModalDialog>(name =>
  17. {
  18. switch (name)
  19. {
  20. case "SimpleDialogWindow2":
  21. return new SimpleDialogWindow2();
  22. case "SimpleDialogWindow":
  23. default:
  24. return new SimpleDialogWindow();
  25. }
  26. })
  27. )
  28. );
  29. </code>
  30.  
  31. Variant 2.
  32. <code>
  33. IUnityContainer container = new UnityContainer();
  34.  
  35. container.RegisterType<IModalDialogService, ModalDialogService>(new ContainerControlledLifetimeManager());
  36. container.RegisterType<IModalDialog, SimpleDialogWindow>("SimpleDialogWindow");
  37. container.RegisterType<IModalDialog, SimpleDialogWindow2>("SimpleDialogWindow2");
  38.  
  39. container.RegisterType<Func<string, IModalDialog>>
  40. (
  41. new InjectionFactory(
  42. c => new Func<string, IModalDialog>(name => {
  43. return container.Resolve<IModalDialog>(name);
  44. }
  45. )
  46. ));
  47. </code>
  48.  
  49. The first variant is nice because there are no dependencies.
  50.  
  51. The second variant is nice because if IModalDialog implementation had further dependencies, Unity would resolve it for us.
  52. Question: Could there be any problem with the fact that we are using a Unity reference inside the factory that is used inside the ViewModel?
  53. Question2: I know that using the local variable 'container' inside lambda function for reference is bad. Would storing the reference inside some static class member be better that using new operator
  54. to create the container?
  55.  
  56. Main Question: I am no sure I am doing the auto-factory type correctly with Variant1/2? Is there a better/another way to have Unity implement the auto-factory in this particular case?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement