Note that there are some explanatory texts on larger screens.

plurals
  1. POApply DI by using unity in WPF application
    primarykey
    data
    text
    <p>I got confused after reading the documentation of Unity framework. <a href="http://msdn.microsoft.com/en-us/library/ff648512.aspx" rel="nofollow">link</a> </p> <p>I'am writing a WPF application which will search for some devices.<br> Below my code from my main Window. As you can see, now i'am still declaring UnitOfWork and DeviceService inside my main Window. I want to replace this code by applying Dependency Injection. At the same time i would also inject my viewmodel inside my main window. </p> <pre><code> public Window1() { InitializeComponent(); UnitOfWork myUnitOfWork = new UnitOfWork(); DeviceService dService = new DeviceService(myUnitOfWork); _vm = new DeviceViewModel(dService); this.DataContext = _vm; _vm.SearchAll(); } </code></pre> <p>I gave a try in below code but i failed in setting the container. The real question is how should i start? Do i need to completely change the stucture of my program?</p> <pre><code>public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); IUnityContainer container = new UnityContainer(); UnitOfWork myUnitOfWork = new UnitOfWork(); container.RegisterInstance&lt;UnitOfWork&gt;(myUnitOfWork); Window1 w1 = new Window1(); w1.Show(); } } </code></pre> <hr> <p>I went trough the suggested tutorial. It is still not clear for me on how i should configure the property injection.</p> <p>My viewmodel should be injected in the Window 1 class, so i assume that i have to create a dependency property.</p> <pre><code> private DeviceViewModel viewModel; [Dependency] public DeviceViewModel ViewModel { get { return viewModel; } set { this.DataContext = value; } } </code></pre> <p>How can i inject my viewmodel into window 1, knowing that DeviceViewModel has dependency on DeviceService and again on UnitOfWork ?</p> <pre><code>//CONSTRUCTOR public DeviceViewModel(DeviceService service) { Service = service; SearchCommand = new SearchCommand(this); } private UnitOfWork myUnit; public DeviceService(UnitOfWork unit) { myUnit = unit; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload