1

Jason Dolinger in his video located here (hot available right now) www.lab49.com/files/videos/Jason%20Dolinger%20MVVM.wmv (from 0.59 to 1.04) uses such code:

public partial App: Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        IUnityContainer container = new UnityContainer();
        RandomQuoteSource source = new RandomQuoteSource();
        container.RegisterInstance<IQuoteSource>(source);

        WatchList window = container.Resolve<WatchList>();
        window.Show();
    }
}

He uses class IUnityContainer which I can not found. As I understand here we just create a window (so container.Resolve call can be replaced with new WatchList(..., also somehow we associate RandomQouteSource as an implementation for IQouteSource, however I don't have clear understanding how this should be used later.

The questions are:

  • how do you create main Windows in your MVVM application, do you also use IUnityContainer for that?
  • is it good technics in general? is it well-known? is it default way to do these things? what alternativies do I have?
  • where can I find Microsoft.Practicies.Unity.dll?
Steven
  • 166,672
  • 24
  • 332
  • 435
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305

4 Answers4

7

Should you?

That's up to you. It can be complicated. If you use it correctly, it can be worth it, both for your code, and for your knowledge of how your code works.

You will be able to identify the parts of your application that should only touch other parts at arms length. You will be more free to make changes to your code without impacting other portions of your code. You will also have an easier time creating unit tests that use mock objects, but that's just a side benefit.

You'll have to read some articles on this topics and see if it makes sense to you.

(to be fair, it really isn't complicated - it just seems that way while you're learning it, or while you're trying to explain it to someone who is new to the concepts)

Unity and Dependency Injection

IUnityContainer is part of Unity, which is a Dependency Injection container library.

It can be coupled with the PRISM framework for use in WPF/Silverlight.

Dependency Injection has a lot of rules you'll want to follow to get the maximum benefit. I don't see an easy or effective "getting started" guide on Unity's site, and Mark Seemann's book on Dependency Injection in .Net isn't free.

So instead I suggest you check out an intro tutorial on Dependency Injection on a site that has a good tutorial:

This is not the Unity framework, so the code won't directly compile...

...but it should teach you the basics of what Dependency Injection is, and why you'd want to use it. Then you should be able to follow the sample code and videos on the Unity page.

If you skip these steps, you're going to get confused very quickly, and will probably shoot yourself in the foot at least a few dozen times.

Creating Windows

You don't use the container except in that one function. Use it anywhere else, and you're not using the DI container correctly. You'll just use the container to register your views, view models, and models, resolve the main window you previously registered, and dispose the container when you're done.

This process is called the "Three Calls Pattern". Unfortunately I don't have any generic examples for Unity, but here is an article on the three calls pattern for yet-another DI container library.

You might also see this mentioned in that Ninject tutorial that I linked above.

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • thanks, and where can I find Microsoft.Practicies.Unity.dll? should i download PRISM just for that? is it true that any MVVM well-designed project should use Microsoft.Practicies.Unity.dll? what alternatives do I have? – Oleg Vazhnev Nov 20 '11 at 17:06
  • @javapowered: "...is part of [Unity](http://unity.codeplex.com/)". There are download links on that page. There are boat-loads of alternatives to Unity, just like there are boat loads of DI libraries in Java :) This list is a bit older - http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx - Right now, I like Ninject, and that isn't directly on the list. – Merlyn Morgan-Graham Nov 20 '11 at 17:10
  • as I understand default "dependency injection" library in java is "spring framework". why c# doesn't have something so popular and so default? – Oleg Vazhnev Nov 20 '11 at 17:13
  • @javapowered: Short answer - it isn't that popular yet :) My long answer - http://programmers.stackexchange.com/questions/63557/how-could-dependency-injection-be-integrated-into-the-language/120262#120262 – Merlyn Morgan-Graham Nov 20 '11 at 17:16
  • i don't want to use `Microsoft.Practicies.Unity.dll` because it seems it is not popular and I don't want to add something not popular to dependencies in my project. what else methods do I have to introduce similar dependencies? – Oleg Vazhnev Nov 20 '11 at 17:17
  • @javapowered: Okay maybe I used the wrong word. It isn't unpopular, it is just not known by the majority of the .Net community yet. Anyone who knows what Dependency Injection is will probably know that library though. And Unity has been extremely well received where it has been applied. I know Microsoft has shipped products using it, with good success. It's just that not everyone knows about it yet. On that article I linked you to, I also detail why I don't think a DI container should *ever* be part of the standard. Though that's just my sole opinion. – Merlyn Morgan-Graham Nov 20 '11 at 17:24
  • @javapowered: If you still would rather use something else (sorry to have swayed you - it works well), then I already gave you a link with a list of other DI container libraries. Here it is again - http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx – Merlyn Morgan-Graham Nov 20 '11 at 17:26
  • ok, another question.. if application is small (developed by just myself), do I need dependency injection in it? probably it would be kind of "overkill"? and what other technics can be used to "substitute" dependency injection? – Oleg Vazhnev Nov 20 '11 at 17:27
  • @javapowered: Even if the application is small you can still get benefits from dependency injection. I would only do this if you plan to maintain the product with bug fixes, or new features, or you evolve its requirements while developing it, or you write unit tests for it. If you don't plan to do any of these things than you might not get a lot of benefit out of DI. You don't have to use libraries to do DI, as that Ninject tutorial I linked shows. But there isn't really any substitute that will give you the same effect as the DI technique itself. – Merlyn Morgan-Graham Nov 20 '11 at 17:32
0

It's a well known technic called Dependency Injection.

An alternativ is to create the needed dependency by hand.

You can download the unity assemblies at patterns & practices - Unity from codeplex.

Take a look at codeproject article for a tutorial.

I havent worked with wpf but i would go the same way to minimize the dependencies and get a better testability.

Edit

Here is another example from codeplex.

But read this article from here stack first, cause it seems to be a pain

Community
  • 1
  • 1
Khh
  • 2,521
  • 1
  • 25
  • 42
0

using dependency injection is a good practice in general. it lets your classes worry about their own concerns and leave the framework to worry about managing dependencies. this leads to more focused, more maintainable, and more testable code in your classes. unity is just one of many such frameworks and it could be argued there are others that are better, such as structuremap and castle windsor. using a container basically means that in one place you set up a registry from which you resolve your classes with their dependencies and you classes specify in their constructor or with public properties those things on which they depend. if you resolve a class from the container, it will automatically resolve its dependencies according to the type of the dependency based on how that type is registered with a container.

the easiest way to include unity in your project is to use nuget. just issue: install-package unity. you can also download binaries and source and get a lot more information at the codeplex project for unity: http://unity.codeplex.com/.

Dave Rael
  • 1,759
  • 2
  • 16
  • 21
0

One suggestion is Ninject - a lightweight, easy to use DI-tool.

Mattias
  • 684
  • 3
  • 7
  • 16
  • 1
    I should probably have read the question abit slower before answering, but a downvote.. some people are jerks – Mattias Nov 20 '11 at 20:44
  • +1 because in trying to discover how to port some code that references Unity, this answer made it clear to me that I can use Ninject (which is alredy being used in my project). – vegemite4me Mar 12 '14 at 16:06