Infragistics JQuery controls

Dependency Injection with the Microsoft Unity Container: Injecting multiple ICommand implementations

Injecting multiple ICommand implementations with the Microsoft Unity container

Hello, and welcome to my second article on Dependency Injection and IoC with the Microsoft Unity Framework.  In my last post Dependency Injection and Inversion of Control with the Microsoft Unity Container I discussed the importance of coding to an interface and not a concrete implementation.  I also introduced the idea of dependencies, and dependency injection using Unity.

As I've been working a lot lately in XAML using the MVVM (Model-View-ViewModel) Pattern, I find myself implementing a lot of ICommand classes.  The ICommand interface is very simple, with a method for execution, a method to check if the command can execute, and an event handler to specify the state of the command execution.  The interface is very useful because of the way that XAML can bind to these commands to perform actions on button click's and other user interface actions.  

Dependency Injection with multiple ICommand implementations

So I had a lot of implementations of the ICommand interface that were used to call on the service layer of my application to complete application tasks. My past experience up until this point never required for me to register one type with multiple interfaces.  Luckily, I found it that Unity has a nice way to handle this issue.

So when I setup my container, I register the commands like this

_container.RegisterType<ICommand, LoadQuizesCommand>("LoadQuizes");
_container.RegisterType<ICommand, CreateQuizCommand>("CreateQuiz");

_container.RegisterType<IQuizService, QuizService>();
_container.RegisterType<IMainQuizMenuViewModel, MainQuizMenuViewModel>();

Then here is the ViewModel where the dependencies are injected in the constructor.

using Domain.QuizIt;
using Microsoft.Practices.Unity;
using QuizIt.ViewModels.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Service.QuizIt.Interfaces;

namespace QuizIt.ViewModels
{
    /// <summary>
    /// This represents the ViewModel that is bound to the Main Quiz user interface.
    /// </summary>
    public class MainQuizMenuViewModel : IMainQuizMenuViewModel
    {

        #region "public properties"

        public ICommand LoadQuizesCommand { get; set; }
        public ICommand CreateQuizCommand { get; set; }
        public IQuizService QuizServiceManager { get; set; }
        public List<Quiz> Quizes { get; set; }
        public string QuizName { get; set; }
        public string QuizDescription { get; set; }

        #endregion

        #region "constructors"

        public MainQuizMenuViewModel([Dependency("LoadQuizes")]ICommand loadQuizesCommand,
            [Dependency("CreateQuiz")]ICommand createQuizCommand, [Dependency]IQuizService quizServiceManager)
        {
            this.LoadQuizesCommand = loadQuizesCommand;
            this.CreateQuizCommand = createQuizCommand;
            this.QuizServiceManager = quizServiceManager;
        }

        #endregion
    }
}

Notice how I specify the same names in the Dependency attributes in the constructors as I did when registering the types.  This way I can specify the command by name and resolve different implementations for multiple ICommand interfaces.

I hope you enjoyed reading this post.  Please feel free to comment if you have any tricks of your own with injecting multiple interfaces.

 

kick it on DotNetKicks.com



Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About the author

My name is Buddy James.  I'm a Microsoft Certified Solutions Developer from the Nashville, TN area.  I'm a Software Engineer, an author, a blogger (http://www.refactorthis.net), a mentor, a thought leader, a technologist, a data scientist, and a husband.  I enjoy working with design patterns, data mining, c#, WPF, Silverlight, WinRT, XAML, ASP.NET, python, CouchDB, RavenDB, Hadoop, Android(MonoDroid), iOS (MonoTouch), and Machine Learning. I love technology and I love to develop software, collect data, analyze the data, and learn from the data.  When I'm not coding,  I'm determined to make a difference in the world by using data and machine learning techniques. (follow me at @budbjames).  

Related links

Month List

refactorthis.net | Windows 8 sales are below Microsoft's internal projections
Infragistics WPF controls

Windows 8 sales are below Microsoft's internal projections

Windows 8 sales aren't meeting expectations

I'd like to first start by saying that I am a fan of Windows 8.  I'm a software developer so naturally I purchased a tablet and installed Windows 8 professional so I would have something to test on.  I find that I use it quite often.  The fact remains that I can see where the average user who cares nothing about tablets or touch screen would have problems with the new OS.  The Windows 8 strategy by Microsoft has seemed awkward and rushed from the beginning.  WinRT has been a mystery to most users, and the lawsuits surrounding the Metro terminology doesn't help matters at all.  I personally think that it would have made sense for Microsoft to enhance Windows 7 to give the user's a reason to upgrade to a new OS and then release WinRT/Metro as a new tablet platform.  It seems Microsoft decided to combine the tablet and PC operating systems to use their weight to force a move toward touch screens and tablets.  

theverge.com has reported that a source close to Microsoft has reported that Windows 8 sales are "below the companies internal projections".  Will Microsoft listen to it's user's and create a new OS?  Basically a situation like we had with Vista and Windows 7?  Only time will tell.

Here is the article on theverge.com

Windows 8 PC sales reportedly 'well below Microsoft's internal projections' 

What are you thoughts on the subject?  What can Microsoft do to turn things around?  What do you think about Windows 8?  Share your comments!

kick it on DotNetKicks.com

 

 



Comments (1) -

pankaj90
pankaj90
11/24/2012 6:58:43 AM #

Yes, Windows 8 have complete new look then Windows 7... It didn't hope from this giant brand.

<a href="http://resultsweb.in">Result</a>;

Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About the author

My name is Buddy James.  I'm a Microsoft Certified Solutions Developer from the Nashville, TN area.  I'm a Software Engineer, an author, a blogger (http://www.refactorthis.net), a mentor, a thought leader, a technologist, a data scientist, and a husband.  I enjoy working with design patterns, data mining, c#, WPF, Silverlight, WinRT, XAML, ASP.NET, python, CouchDB, RavenDB, Hadoop, Android(MonoDroid), iOS (MonoTouch), and Machine Learning. I love technology and I love to develop software, collect data, analyze the data, and learn from the data.  When I'm not coding,  I'm determined to make a difference in the world by using data and machine learning techniques. (follow me at @budbjames).  

Related links

Month List