CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Injecting context specific state into parts

423 Views
Copy Code Show/Hide Line Numbers
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ComponentModel.Composition.Hosting;
 
namespace Given_two_order_view_models
{
    [TestClass]
    public class when_they_are_created : using_scoping_context
    {
        [TestInitialize]
        public void Context()
        {
            _order1 = new Order() {OrderNumber = 100};
            _orderState.Value = _order1;
            _orderViewModel1 = _container.GetExportedValue<OrderViewModel>();
 
            _order2 = new Order() {OrderNumber = 200};
            _orderState.Value = _order2;
            _OrderViewModel2 = _container.GetExportedValue<OrderViewModel>();
        }
 
        [TestMethod]
        public void then_the_first_view_model_gets_order_100()
        {
            Assert.AreEqual(_order1, _orderViewModel1.Order.Value);
        }
 
        [TestMethod]
        public void then_the_second_view_model_gets_order_200()
        {
            Assert.AreEqual(_order2, _OrderViewModel2.Order.Value);
        }
    }
 
    public class using_scoping_context
    {
        public using_scoping_context()
        {
            var catalog = new TypeCatalog(typeof(Order), typeof(OrderViewModel), typeof(OrderState), typeof(CurrentOrderState));
            _container = new CompositionContainer(catalog);
            _orderState = _container.GetExportedValue<ICurrentState<Order>>();
        }
 
        protected ICurrentState<Order> _orderState;
        protected CompositionContainer _container;
        protected Order _order1;
        protected Order _order2;
        protected OrderViewModel _orderViewModel1;
        protected OrderViewModel _OrderViewModel2;
    }
 
    public class Order
    {
        public int OrderNumber { get; set; }
    }
 
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class OrderViewModel
    {
        [Import]
        public IState<Order> Order { get; set; }
    }
 
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class OrderState : IState<Order>
    {
        [ImportingConstructor]
        public OrderState(ICurrentState<Order> currentOrder)
        {
            Value = currentOrder.Value;
        }
 
        public Order Value { get; private set; }
    }
 
    public class CurrentOrderState : CurrentState<Order>
    {
    }
 
    [InheritedExport]
    public interface IState<T>
    {
        T Value { get; }
    }
 
    public class CurrentState<T> : ICurrentState<T>
    {
        public T Value { get; set; }
    }
 
    [InheritedExport]
    public interface ICurrentState<T>
    {
        T Value { get; set; }
    }
 
}
by Glenn Block
  May 28, 2010 @ 7:04pm
Description:
Sample illustrates building up viewmodels that each get a specific model injected

Note: This is a general pattern that can be used with any IoC mechanism although the sample uses MEF.

Many containers support parameterized construction which removes the need for such an approach.

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate