Preface

This post assumes you are familiar a bit with the MVVM concepts. Otherwise, here is a 5 minute overview from John Papa

How do you wire up your ViewModels to your Views in your WPF and Silverlight apps? Of course, the simplest approach is assigning your View Model’s instance to the View’s data context, in the code behind :).

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainViewModel();
        }
    

imageUnderstanding The ViewModelLocator Concept

An alternate way to do the above task is via a ViewModelLocator implementation, mainly so that you can preserve the Blendability - to enable designers to see see the correct preview of the View. Laurent Bugnion has a pretty neat implementation of ViewModelLocator in his MVVM Light toolkit . A simple approach to use ViewModelLoator in MVVMLight is - you can create an instance of ViewModelLocator and keep it in App.xaml as a global resource, so that you can bind your views to that – As shown below. Assume we have a MainWindow.xaml view and a MainViewModel class as our view model

1 - Create a global resource in your App.xaml
    <Application.Resources>
        <!--Global View Model Locator-->
        <vm:ViewModelLocator x:Key="Locator"
                             d:IsDataSource="True" />
    </Application.Resources>
2 - You can bind to your locator from your Views.
<Window x:Class="MvvmLightApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="300"
        Width="300"
        DataContext="{Binding Main, Source={StaticResource Locator}}">
		
		<!-- More Xaml -->
		
</Window>	

If you are interested in the actual implementation of ViewModelLocator in MVVMLight, here is a ‘trimmed down’ version (The whole code of ViewModelLocator.cs is here in codeplex, under MVVMLight repository)

  public class ViewModelLocator
    {
        private static MainViewModel _main;

        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            CreateMain();
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        public static MainViewModel MainStatic
        {
            get
            {
                if (_main == null)
                {
                    CreateMain();
                }

                return _main;
            }
        }

        /// Gets the Main property.      
        public MainViewModel Main
        {
            get
            {
                return MainStatic;
            }
        }


        /// Provides a deterministic way to create the Main property.
        public static void CreateMain()
        {
            if (_main == null)
            {
                _main = new MainViewModel();
            }
        }
		
		//Clean up code omitted..

    }	

As you can see, you are expected to expose a property per ViewModel that returns a singleton instance of the same - so that we can bind to the same from the View.  Of course, the MVVMLight implementation is pretty clean and neatly blendable.

Locating ViewModels by Convention – AutoViewModelLocator

I was a bit lazy to create a property per ViewModel in the ViewModelLocator, and thought it is nice if I get my ViewModels resolved by convention. Thus born the AutoViewModelLocator. Have a look at the source code first.

 public class AutoViewModelLocator
    {
        private static List<object> _viewModels = new List<object>();


        /// <summary>
        /// Return the view model based
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public object this[string name]
        {
            get
            {
                //Need to have a cleaner way of doing this
                var type = typeof(AutoViewModelLocator).Assembly.GetTypes().First
                 (t => t.IsClass && t.Name.EndsWith(name + "ViewModel"));

                var obj = _viewModels.Find(vm => vm.GetType() == type);


                if (obj != null)
                {
                    return obj;
                }
                else
                {
                    obj = Activator.CreateInstance(type);
                    _viewModels.Add(obj);
                    return obj;
                }
            }
        }

        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        public static void Cleanup()
        {
            foreach (var vm in _viewModels)
            {
                //Cleaup here
            }
        }
    }

Nothing big there. As you can see, we are instantiating the view models based on reflection. In actual implementation, you may use a better resolution method, probably using an Container or so.

Anyway, now you can use the AutoViewModelLocator much like we explained earlier – by creating it as a global resource in App.xaml, and then binding to the same from the View. How ever, please note that in the View, we should bind to the Indexer from Xaml (Please have a look at the source code attached).

Let us test our Locator via a simple ViewModel and a View.

Our Minimal View Model
 //Our minimal view model
 public class MainViewModel
    {

        public string Data 
        { 
            get 
            { 
                return "Hello via AutoViewModelLocator"; 
            } 
        }
    }

Binding the View

Note that we are binding the data context of the control to the Indexer of our AutoViewModelLocator. And we have a text box that binds to our above Data property. Here is how that looks like in Visual Studio.

image

And in Expression Blend 4.0

image

Happy Coding!!

[+] Download Related Source Code

 

There are more interesting reads here, make sure you read about

Read more >>

K-MUG (Kerala Microsoft Users Group) is pretty excited to announce DevCon2010, the Grant Developer conference in Kerala – The God’s own land. DevCon 2010 will be conducted in Park Centre, Techopark, Trivandrum, Kerala - on 3rd and 4th July, 2010. We have different sessions planned as a part of this event.

A lot of excitement is there in the air, and the focus will be on Cloud computing, VS2010 Features, Robotics, Windows Phone 7, Azure, Application Compatibility, ASP.NET etc. I would be one of the speakers as well. Walk in, uplift yourself, and go back with some great energy.

We’ll also launch Microsot Office 2010 during the event.

image

 

 

 

 

 

 

 

 

 

Speakers

We’ve got speakers including Janakiram, Vijay (msigeek), Ramaprasanna, Shobhan, Shiju, Sreeju, Praseed, Jene, Manu etc.  Checkout the speakers page in DevCon 2010 website.

   

     

Oh yea, I’m also taking a session. I’ll be taking a session on VS 2010 + .NET 4.0 – I’ll talk about New VS2010 Features, and .NET 4.0 Frameworks. A session on Windows Phone 7 is also planned

Agenda

Here is the detailed Agenda.

Day 1 (3rd July – Saturday)

  • 08:30AM – 09:15AM – Registration Confirmation
  • 09:15AM – 09:30AM – Welcome Speech
  • 09:30AM – 10:15AM – Key Note Session – “Cloud – The Meta Platform”
  • 10:15AM – 11:00AM – New features in .NET 4.0 & Visual Studio 2010
  • 11:00AM – 11:15AM – Tea break
  • 11:15AM – 12:15PM – Robotics Programing
  • 12:15PM – 01:15PM – Web Security and Security Auditing
  • 01:15PM – 02:15PM – Lunch
  • 02:15PM – 03:15PM – Windows Azure
  • 03:15PM – 04:00PM – Great Developer Contest – Final
  • 04:00PM – 05:00PM – Managing Application Compatibility in Windows 7

Day 2 (4th July – Sunday)

  • 08:30AM – 09:30AM – Registration Confirmation
  • 09:30AM – 10:30AM – Data on the Cloud
  • 10:30AM – 11:30AM – Mixed Mode Windows development using C# and C++
  • 11:30AM – 11:45AM – Tea Break
  • 11:45AM – 12:00PM – Visual Studio 2010 tips
  • 12:30PM – 01:30PM – Tuning Tools in SQL Server 2008
  • 01:30PM – 02:30PM – Lunch
  • 02:30PM – 03:15PM – ASP.NET MVC
  • 03:15PM – 04:00PM – Windows 7 Phone
  • 04:00PM – 05:00PM – Closing Ceremony

We’ll also perform the Microsoft Office 2010 Launch during the event.

How to Reach

Kerala is known for it’s natural beauty, and is called God’s own land. The event is in Technopark, Trivandrum, Kerala – Technopark is the greenest IT Park in India.

See the Map | See Technopark Website | Campus Photos

Places To See

Don’t miss these places, if you are in Trivandrum. These locations are near by.

     

  • Veli Tourist Village:
    • It is about 5kms from the place of DevCon event, and boating facilities are offered at this picnic spot. Speed boats, restaurants and well landscaped gardens are available.
  • Kovalam:
    • It is a globally renowned beach destination of Trivandrum and is strewn with the coconut groves that make it a tropical paradise
  • Padmanabhaswamy Vishnu Temple:
    • It is a magnificent temple, which featuring pure Dravidian style of architecture. The temple houses the shrine of the presiding deity of Thiruvananthapuram, Lord Vishnu and was built by the Maharaja of Travancore.
  • Napier Museum:
    • On your travel to Trivandrum, you will also come across this museum that was built in the 19th century and is an amazing example of the affluent history of Kerala

Visit DevCon2010 website to take part in the cool contests, to book your seat etc. See you guys there!!.

Read more >>

My copy of JQuery & ASP.NET Controls – 51 Tips, Tricks and Recipes (published by DotnetCurry fame (and my friend and ASP.NET Architecture MVP) Suprotim Agarwal) arrived some time back.

I paid some serious attention to the book over the last couple of weekends, when I was playing around with JQuery a bit to implement some desired user experience. I should say I’m pretty impressed with the book – mainly for it’s practical value, and about the way it is organized.

The whole book has ten sections, with multiple recipes under each section, detailing specific tasks. In short, you’ll be able to quickly find how to implement a specific functionality in JQuery, just by looking at the index.

image  

I feel that the practical value of the book is immense. The examples and explanation are clear and concise – you don’t find any useless crap there. Each recipe has a problem statement (challenge), solution (the actual code),  Explanation of the code, A link to a live demo, a note on Browsers supported,  and a set of useful links on this problem. Great.

For my scenario, I was actually trying for implementing a Google reader like feature using JQuery – to high light multiple rows of the Grid view, so that the user can perform an operation with the high lighted rows. Wow, and I got pleasantly surprised when I saw this.

image

I think the book, along with the sample code, can really fasten up the development time of web applications. For a developer, with out doubt, this e-book will serve as a travelling compass in the world of ASP.NET and JQuery, and is definitely recommended for anyone who is interested to build great user experiences with JQuery.    

You may read 5 sample chapters of the book here for free. And if you are serious about learning JQuery, don’t waste time, Go and buy it for $14.99 – The book comes with

  • 51 Tips, Tricks and Recipes with jQuery and ASP.Controls EBook (PDF)
  • Entire Source Code of the EBook
  • Cross Browser Scripts - tested on IE 7, IE 8, Firefox 3, Chrome 2, Safari 4
  • Common code that runs on ASP.NET Page, Master Pages, and in most cases, HTML pages too.

Again, congratulations to Suprotim for his great effort.

Shout it
Read more >>

image

This post explains how to create a View Engine for ASP.NET MVC, leveraging the Text Template (T4) infrastructure already out there for rendering the view based using a custom T4 template host.

Clarification: Here, I’m not using T4 for design time code generation. We are using T4 toolkit to render the views during runtime.

[+] Download Related Source Code

For me, the most beautiful aspect of ASP.NET MVC is it’s extensibility – they way you can ‘stretch’ the framework, to make it suitable for your own needs. I highly recommend you to read this article from Code Climber’s blog - 13 ASP.NET MVC Extensibility Points you have to know

In this post, we’ll explore the following concepts.

  • ViewEngines in ASP.NET MVC
  • Creating a custom ViewEngine for ASP.NET MVC
  • Supporting multiple View Types (our view engine will support both aspx/ascx files and tt files)
  • Partial rendering between view types (you can render a tt view from an aspx view)

Preface About View Engines

This is a quick recap on how the View Engine is invoked with in the ASP.NET MVC Framework. Let us start from how a controller is created and how an action is called. I’m going the easy way - the route handling system in ASP.NET MVC invokes a DefaultControllerFactory by default, which is responsible to choose the correct controller class and an action for a given request. For example, consider the URL http://Portal.com/Customer/Get/3 - As you know, by default, MVC will expect a CustomerController class with a Get Action inside the same, like

   public class CustomerController : Controller
    {
        public ActionResult Get(int id)
        {

           //Get the customer with ID from repository, place it in ViewData
            ViewData["Customer"] = rep.GetCustomerWithId(id);

            //View method returns a Viewresult
            return View();
        }
    }

imageThe controller will invoke the correct action method, which returns an ActionResult object. In the above example, you may see that we are returning a ViewResult. In ASP.NET MVC, there are various action results, including ViewResult, ContentResult etc.

Wow, There we are. If the controller action is returning a ViewResult, the action method can use the ViewData structure (see the above example) to fill it with some values, to pass the same to ViewResult. ViewResult can locate and render a particular view template using ViewData. It does so by invoking WebFormViewEngine.

The default View engine available with ASP.NET MVC is WebFormViewEngine, which creates a WebFormView to render your aspx and ascx files. The View Engine normally passes the path information of the file to render, along with the view context information to the view.

The view file name and path is normally detected based on convention – normally in the path – /Views/{ControllerName}/{ActionName} – i.e, If your Controller class name is CustomerController and action/method name is Get, the default view engine will expect a file in the location /Views/Customer/Get.aspx

Using Text Template Toolkit To Render A View

The beauty of ASP.NET MVC is in it’s extensibility. All interaction points above can be customized the way you like. As of now, we are only interested to see how to create a custom View Engine, which can create a T4View that knows how to render a text template (tt) file, leveraging the Text Templating infrastructure.

Normally, as you are aware, Text Templates (T4) are used with in Visual Studio for activities like Code Generation. When I was going through this exercise of explaining how to create a custom view engine for ASP.NET MVC, I thought it’ll be an interesting exercise to leverage T4 toolkit for the task.

But we’ve got a problem there – We can’t use the Microsoft.VisualStudio.TextTemplating libraries, because I don't believe that T4 can be legally redistributed without Visual Studio. So, I’ve decided to relay on the Mono equivalent T4 implementation, Mono.TextTemplating (included in the download)

Now, let us get in to the actual task. These are the steps we should do to create our view engine.

  • A ViewEngine implementation
    • We’ll create a view engine by implementing the IViewEngine interface. There is an abstract class VirtualPathProviderViewEngine that already implements IViewEngine interface and provides some extra functionality for path detection of view files.  VirtualPathProviderViewEngine  has two methods we are concerned about – CreateView and CreatePartialView from where we should return a custom View, that has the path information to the template file (*.tt) to render.
  • A View
    • We’ll create a view, by implementing the IView interface. IView has a Render method that we are interested about. We’ll read and render the tt file, leveraging the TT engine
  • A T4 Host
    • We’ll also create a custom T4 host, by implementing the ITextTemplatingEngineHost interface, for self hosting the template transformation process.

Once we’ve the above pieces, we need to register our custom view engine with ASP.NET MVC. That’s pretty simple, and we’ll see that soon.

The View Engine Implementation

imageMay be it is time to have a look in to the actual view engine implementation. A slight variation to what we discussed earlier. Instead of creating a ViewEngine from scratch, we are going to inherit our ViewEngine from VirtualPathProviderViewEngine that’s already there in MVC framework – so that all the file path logic will be taken care automatically. VirtualPathProviderViewEngine provides some extra functionality so that we can specify the location formats to look for our view files (in this case, *.tt files), when ever the View Engine is invoked.

Alright. Let us be a bit more creative here. What about creating a View Engine that can handle the aspx and ascx files *along with* the text template files? Creating such a composite view engine is pretty simple – So, this is what our view engine should do.

  • First, the view engine should look for a View file that ends with *.view.tt
    • If that file exists, create and return a T4View that’ll render our tt file
    • If not, look for a View file that ends with *.ascx or *.aspx
      • If exists, create and return a WebFormView that knows how to render the aspx/ascx files.

Enough blabbering. Here we go, the code of our CompositeViewEngine. All the implementations are in MvcT4ViewEngine.Lib project, so you may download the related source code from the above link to have a look at the same side by side.

    /// <summary>
    /// A composite view engine to help plugging view engines
    /// </summary>
    public class CompositeViewEngine : VirtualPathProviderViewEngine
    {
        // Ctor - Let us set all location formats
        public CompositeViewEngine()
        {
            base.MasterLocationFormats = new string[] { "~/Views/{1}/{0}.master", 
                                                        "~/Views/Shared/{0}.master" };
            base.AreaMasterLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.master", 
                                                         "~/Areas/{2}/Views/Shared/{0}.master" };
            base.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.view.tt", 
                                                      "~/Views/{1}/{0}.aspx", "~/Views/{1}/{0}.ascx",  
                                                      "~/Views/Shared/{0}.view.tt", 
                                                      "~/Views/Shared/{0}.aspx", "~/Views/Shared/{0}.ascx" };
            base.AreaViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.view.tt", 
                                                       "~/Areas/{2}/Views/{1}/{0}.aspx", "~/Areas/{2}/Views/{1}/{0}.ascx",
                                                       "~/Areas/{2}/Views/Shared/{0}.view.tt", 
                                                       "~/Areas/{2}/Views/Shared/{0}.aspx", "~/Areas/{2}/Views/Shared/{0}.ascx" };
            base.PartialViewLocationFormats = base.ViewLocationFormats;
            base.AreaPartialViewLocationFormats = base.AreaViewLocationFormats;
        }

        /// <summary>
        /// Handle the creation of a partial view
        /// </summary>
        protected override IView CreatePartialView
                 (ControllerContext controllerContext, string partialPath)
        {
            if (partialPath.EndsWith(".view.tt"))
                return new T4View(partialPath);
            else
                return new WebFormView(partialPath, null);
        }

        /// <summary>
        /// Handle the creation of a view
        /// </summary>
        protected override IView CreateView
               (ControllerContext controllerContext, string viewPath, string masterPath)
        {
            if (viewPath.EndsWith(".view.tt") && String.IsNullOrEmpty(masterPath))
            {
                return new T4View(viewPath);
            }
            else if (viewPath.EndsWith(".view.tt") && !String.IsNullOrEmpty(masterPath))
            {
                return new T4View(viewPath,masterPath);
            }
            else
                return new WebFormView(viewPath, masterPath);
        }

        /// <summary>
        /// Check if the file exists
        /// </summary>
        protected override bool FileExists
                (ControllerContext controllerContext, string virtualPath)
        {
            return base.FileExists(controllerContext, virtualPath);
        }

       
    }

That looks pretty simple, right? Have a look at the constructor, and you’ll find that we are specifying the path format constraints to detect our T4 view files as well (*.view.tt), along with the aspx and ascx path formats. And you may also find that in the CreateView and CreatePartialView, we are creating and returning a WebFormView, in case the *.vew.tt files are not found. CreatePartialView will be invoked when a partial rendering is requested – e.g. when the user calls a RenderPartial method in a view.

Now, the interesting aspect there is, you can render a text template view from an aspx view, using the Ht

The View Implementation

The T4View implementation is also very simple. We are just invoking our T4 host, to render the tt files. Have a look at the Render method below. You may also note that we are passing the ViewContext to the host, so that we can access the view context later in our text template files, via the host variable.

     /// <summary>
    /// A view based on T4
    /// </summary>
    public class T4View : IView
    {

        #region IView Members

        private string viewName = string.Empty;
        private string masterName = string.Empty;

        public T4View(string ttViewName)
        {
            viewName = ttViewName;
        }
        public T4View(string ttViewName, string masterttName)
        {
            viewName = ttViewName;
            masterName = masterttName;
        }

        /// <summary>
        /// Render our tt file
        /// </summary>
        /// <param name="viewContext"></param>
        /// <param name="writer"></param>
        public void Render(ViewContext viewContext, System.IO.TextWriter writer)
        {
            string filePath = viewContext.HttpContext.Server.MapPath(viewName);
            string masterPath=string.Empty;


            if (!string.IsNullOrEmpty(masterName)) 
            {
                masterPath=viewContext.HttpContext.Server.MapPath(masterName);
            }

            var thost = new T4TemplateHost();
            thost["ViewContext"] = viewContext;


            string data = string.Empty;
            var results = thost.ProcessTemplate(filePath, masterPath, out data);
            if (results.HasErrors)
            {
                writer.WriteLine("<h1>errors found</h1>");
            }
            foreach (var res in results)
            {
                writer.WriteLine("Error - " + (res as CompilerError).ToString());
            }
            writer.Write(data);

        }

        #endregion
    }

About the Template Host

You may see that in the Render method, we are creating an instance of our T4 template host, and requesting the template host to process our our *.view.tt file. You may read more about creating a custom template host here, though I’m not detailing that much. How ever, if you are so curios, here is the ProcessTemplate method in our custom T4 host.

        /// <summary>
        /// Process the input template
        /// </summary>
        /// <returns></returns>
        public CompilerErrorCollection ProcessTemplate
		          (string templateFileName, string masterFileName, out string data)
        {

            if (!File.Exists(templateFileName))
            {
                throw new FileNotFoundException("The file cannot be found");
            }

            var engine = new TemplatingEngine();
            TemplateFile = templateFileName;

            //Read the text template.
            string input = File.ReadAllText(templateFileName);


            if (!string.IsNullOrEmpty(masterFileName))
            {
                input = File.ReadAllText(masterFileName).Replace("<!--[Content]-->",input);
            }

            //Transform the text template.
            data = engine.ProcessTemplate(input, this);
            return Errors;
        }

Registering our View Engine

The last piece of the puzzle would be to register our custom View Engine, so that the framework will use our View Engine instead of the default one. Let us create a new ASP.NET MVC Project. Now in the Global.asax.cs file of our MVC application (See MvcT4ViewEngine.Demo project in the downloaded source code), we need to specify our CompositeViewEngine as the default view engine, in the Application_Start.

    protected void Application_Start()
        {
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new CompositeViewEngine()); 
            RegisterRoutes(RouteTable.Routes);
        }

And there we go.

 

The Results

First of all, let us add a new GetMessage method to the HomeController ASP.NET MVC project. Our GetMessage action in the Home controller simply returns a view after storing someting in ViewData, like

   public ActionResult GetMessage()
        {
            ViewData["MessageForT4"] = "Welcome to ASP.NET MVC Views using T4";
            return View();
        }
Now, add a new view, named GetMessage.view.tt in the Views/Home  folder as shown below. And you can access the ViewData like this.

image

Now, run the application, and navigate to the path /Home/GetMessage and you should be able to see the above view getting rendered. If you are wondering what the GetViewData method does, it fetches the ViewData context we set to the host earlier, in the above Render method.

image

 

More interestingly, you may also try partially rendering a T4 view from an aspx file. You can use Html.RenderPartial("YourView"); from your aspx view, to render the YourView.view.tt file – See how I’m rendering IndexPart.view.tt from the Index.aspx view, in the attached example.

Conclusion

The intent of this article is just to explore how to create custom view engines for ASP.NET MVC. The example view engine we put together is very elementary as of now, but I’ld like to evolve that towards something useful, so that finally it can be a part of MvcContrib. For this, several performance features like caching of compiled views needs to be implemented. That is for later.

Recommending you to follow me on twitter – Also, read my previous posts – A duck typed view model in ASP.NET MVC or Understanding Managed Extensibility Framework and Lazy<T> – Happy Coding!!

Shout it
Read more >>

Overview

The objective of this post is to brief how you can leverage the lazy initialization support available in MEF. We’ll examine MEF and Lazy, and then we’ll see how to use them together.

 

Preface About MEF

I hope you are already using Managed Extensibility Framework to build beautiful software. If you are not yet there, that is a crime, and I highly recommend you to read my introductory post on MEF. Let us start with another very basic MEF example. Let us get back our Zoo.

image 

We are composing our Zoo with a couple of Animals here.

    //Abstract animal interface
    interface IAnimal { void Eat(); }

    //Concrete animal 1
    [Export(typeof(IAnimal))]
    class Lion : IAnimal
    {
        public Lion() { Console.WriteLine("Grr.. Lion got created"); }
        public void Eat() { Console.WriteLine("Grr.. Lion eating meat"); }
    }

    //Concrete animal 2
    [Export(typeof(IAnimal))]
    class Rabbit : IAnimal
    {
        public Rabbit() { Console.WriteLine("Crrr.. Rabbit got created"); }
        public void Eat() { Console.WriteLine("Crrr.. Rabbit eating carrot"); }
    }
    
    //Our Zoo. MEF will inject animals to this zoo later, at the time of composition
    class Zoo
    {
        [ImportMany(typeof(IAnimal))]
        public IEnumerable<IAnimal> Animals { get; set; }
    }

    //Let us construct our zoo and animals
    class Program
    {
        static void Main(string[] args)
        {
            //Let us create a catalog and a container
            var catalog = new AssemblyCatalog(typeof(Program).Assembly);
            var container = new CompositionContainer(catalog);

            //Compose the zoo.
            var zoo = new Zoo();
            container.ComposeParts(zoo);

            //Let's feed our animals
            foreach (var animal in zoo.Animals)
                animal.Eat();

        }
    }

As you remember, a catalog is a sack full of types (cookies) to export. The container will fetch these exported types from the catalog, to instantiate and import them where ever applicable - based on the matching contracts specified in the Export and Import attributes. (Note to self - Shh!! don’t mention anything about ExportProvider and ExportDefinition now). In this case, we are using a type as the contract (IAnimal) for our Export and Import. And if you run that app, you’ll see.. hm.. a cute black screen. More importantly, you’ll see that MEF has created an instance of Lion and Rabbit at the time of composition – i.e, when we call container.ComposeParts. See the messages we are writing from the constructor.

image 

Note – If you want to be more realistic or if you hate animals in general, my advice is to think about your Business class instead of the Zoo, and think about some validation rules instead of those Animals. So that, you can plug in new validation rules with out affecting your entire system.

Preface About Lazy<T>

Oh yea, though MEF is seductive, let us stop thinking about that for a moment. Let us be a bit Lazy. You might have already heard about the Lazy<T> class in .NET 4.0. Lazy class allow you to support for lazy initialization. I.e, you can use Lazy<T> to defer the creation of a large or resource-intensive object, till you really need that.

Here is a quick example.

 

            //Let us be lazy about creating a lion
            Lazy<Lion> lazyLion = new Lazy<Lion>();

            //Do something else..

            //Actual object will be created here
            var lion = lazyLion.Value;
   

The interesting point to note is, you can defer the creation of your lion, till you really need it in your flow, probably inside an if block or so. Another interesting aspect of Lazy<T> is, you can specify your own factory method to create your object, via the Lazy<T> constructor. See this example.

            //Let us be lazy about creating our animal
            Lazy<IAnimal> lazyAnimal = new Lazy<IAnimal>(()=>new Lion());

            //Actual object will be created here
            var animal = lazyAnimal.Value;

Being Lazy with MEF

Now, let us see how to combine MEF and Lazy together. All right, you don’t really need to do anything special there – MEF already has some great support for Lazy. MEF can wire up your exports directly to a Lazy at the time of import. Now, let us see how to do that. We just need to make two modifications to our zoo example. Time for a KG Excercise - Spot the difference of this code example with the very first example.

 //Our zoo
    class Zoo
    {
        //** MODIFICATION 1 -You need to directly import to Lazy<IAnimal>
        [ImportMany(typeof(IAnimal))]
        public IEnumerable<Lazy<IAnimal>> Animals { get; set; }
    }

    //Abstract animal interface
    interface IAnimal { void Eat(); }

    //Concrete animal 1
    [Export(typeof(IAnimal))]
    class Lion : IAnimal
    {
        public Lion() { Console.WriteLine("Grr.. Lion got created"); }
        public void Eat() { Console.WriteLine("Grr.. Lion eating meat"); }
    }

    //Concrete animal 2
    [Export(typeof(IAnimal))]
    class Rabbit : IAnimal
    {
        public Rabbit() { Console.WriteLine("Crrr.. Rabbit got created"); }
        public void Eat() { Console.WriteLine("Crrr.. Rabbit eating carrot"); }
    }



    //Let us construct our zoo and animals
    class Program
    {
        static void Main(string[] args)
        {
            //Let us create a catalog and a container
            var catalog = new AssemblyCatalog(typeof(Program).Assembly);
            var container = new CompositionContainer(catalog);

            //Compose the zoo.
            var zoo = new Zoo();
            container.ComposeParts(zoo);

            //** MODIFICATION 2 - An instance will be created only when you access it
            //Use the Value property of the Lazy object to initialize and access the actual value
            foreach (var animal in zoo.Animals) 
                animal.Value.Eat();
        }
    }

And now, to understand how this changed the flow, let us run the application.

image

You’ll see that our animals are getting created only when we consume them – and not at the time of composing the zoo as we did earlier. 

Dealing with Metadata when MEF goes Lazy

MEF allows you to export Metadata along with the types you export. For example, assume that you want to specify whether your exported animals eat meat or not, so that some one can decide what to give them as food. One way is to use the ExportMetaData attribute with your exported types, as shown below.

   //Concrete animal 1
    [Export(typeof(IAnimal))]
    [ExportMetadata("EatMeat",true)]
    class Lion : IAnimal
    {
        public Lion() { Console.WriteLine("Grr.. Lion got created"); }
        public void Eat() { Console.WriteLine("Grr.. Lion eating meat"); }
    }

    //Concrete animal 2
    [Export(typeof(IAnimal))]
    [ExportMetadata("EatMeat", false)]
    class Rabbit : IAnimal
    {
        public Rabbit() { Console.WriteLine("Crrr.. Rabbit got created"); }
        public void Eat() { Console.WriteLine("Crrr.. Rabbit eating carrot"); }
    }
That looks a bit clumsy, because of those strings. Right? A better way is to create your own custom Export attribute that includes the metadata information as well. That is pretty simple. The code below is equivalent to what we have just done above. You see that we are creating a custom export attribute named ExportAnimal.
       //Our custom metadata attribute to exp animals
    [MetadataAttribute]
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    class ExportAnimal : ExportAttribute
    {
        //Pass the contract type to the base
        public ExportAnimal() : base(typeof(IAnimal)) { }
        //Additional metadata info
        public bool EatMeat { get; set; }
    }

    //Concrete animal 1
    [ExportAnimal(EatMeat=true)]
    class Lion : IAnimal
    {
        public Lion() { Console.WriteLine("Grr.. Lion got created"); }
        public void Eat() { Console.WriteLine("Grr.. Lion eating meat"); }
    }

    //Concrete animal 2
    [ExportAnimal(EatMeat = false)]
    class Rabbit : IAnimal
    {
        public Rabbit() { Console.WriteLine("Crrr.. Rabbit got created"); }
        public void Eat() { Console.WriteLine("Crrr.. Rabbit eating carrot"); }
    }
Now let us come to the real question. How to import the metadata information in a Lazy way? Fortunately, MEF has an overload of Lazy, Lazy that supports importing Metadata information. So, all we need to do is create a metadata import interface that matches our export definition, and use it. Like this.
   //An interface to import animal metadata
   //This should match the metadata we've in our custom export definition
    public interface IAnimalMetadata
    {
        bool EatMeat { get; }
    }

    //Our zoo
    class Zoo
    {
        //Directly import to Lazy<IAnimal,IAnimalMetadata>
        [ImportMany(typeof(IAnimal))]
        public IEnumerable<Lazy<IAnimal, IAnimalMetadata>> Animals { get; set; }
    }

So, here is the final piece that includes the above fragments. You can see that we are exporting our animals using a custom export attribute, and importing them in a lazy way along with the metadata. Then, Have a look at the Main method. You'll find that we are using the metadata to feed only the animals that eat meat.

In other words, the Lazy implementation provides you the luxury of initializing only the animals that you want to feed, at the time of feeding them - and not all the animals.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;

namespace MefLazy
{

    #region Contracts

    //Abstract animal interface
    interface IAnimal { void Eat(); }

    #endregion

    #region Export Related

    //Our custom metadata attribute to exp animals
    [MetadataAttribute]
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    class ExportAnimal : ExportAttribute
    {
        //Pass the contract type to the base
        public ExportAnimal() : base(typeof(IAnimal)) { }
        //Additional metadata info
        public bool EatMeat { get; set; }
    }

    //Concrete animal 1
    [ExportAnimal(EatMeat = true)]
    class Lion : IAnimal
    {
        public Lion() { Console.WriteLine("Grr.. Lion got created"); }
        public void Eat() { Console.WriteLine("Grr.. Lion eating meat"); }
    }

    //Concrete animal 2
    [ExportAnimal(EatMeat = false)]
    class Rabbit : IAnimal
    {
        public Rabbit() { Console.WriteLine("Crrr.. Rabbit got created"); }
        public void Eat() { Console.WriteLine("Crrr.. Rabbit eating carrot"); }
    }

    #endregion

    #region Import Related

    //An interface to import animal metadata
    public interface IAnimalMetadata
    {
        bool EatMeat { get; }
    }

    //Our zoo
    class Zoo
    {
        //Directly import to Lazy<IAnimal,IAnimalMetadata>
        [ImportMany(typeof(IAnimal))]
        public IEnumerable<Lazy<IAnimal, IAnimalMetadata>> Animals { get; set; }
    }

    #endregion

    #region Main

    //Let us construct our zoo and animals
    class Program
    {
        static void Main(string[] args)
        {
            //Let us create a catalog and a container
            var catalog = new AssemblyCatalog(typeof(Program).Assembly);
            var container = new CompositionContainer(catalog);

            //Compose the zoo.
            var zoo = new Zoo();
            container.ComposeParts(zoo);

            //An instance will be created only when you access it
            //Use the Value property of the Lazy object to initialize and access the actual value
            //Let us feed only animals eating meat
            foreach (var animal in zoo.Animals) 
                if (animal.Metadata.EatMeat)
                    animal.Value.Eat();
        }
    }

    #endregion
}

Conclusion

I encourage you to apply these concepts to more practical scenarios. Like, instead of a Zoo, think about a RuleProcessor that runs a set of rules on a business object - and instead of an Animal, think about a Rule that can validate or process the business object. I’ll give some more practical examples pretty soon :). For now, the objective was to introduce Lazy concepts and MEF, and I hope you enjoyed the read. And you can download the final version of the code from the above link. Keep in touch, Follow me on twitter

Also, read my last post 4 .NET 4.0 Libraries You should know about

Happy Coding!!

Read more >>

top