Click here to Skip to main content
15,891,136 members
Articles / Web Development

WPF Two-way Databinding in ASP.NET - Enabling MVVM

Rate me:
Please Sign up or sign in to vote.
4.88/5 (35 votes)
29 Jan 2011CPOL21 min read 139.6K   66  
Bringing WPF like declarative data binding to ASP.NET Web Forms to enable declarative two-way data binding to any object whilst opening up MVVM UI development.
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Binding.Services;
using BinderTests.MockingInterfaces;
using Binding;
using Rhino.Mocks;
using ASPBinding.Services;
using System.Web.UI;
using Binding.Interfaces;

namespace BinderTests
{
    [TestClass]
    public class ContextRetrievalTests
    {

        private const string VIEW_MODEL_KEY = "VIEW_MODEL";

        private IBindingContainer mockContainer = null;
        private IDataStorageService dataStorageService = null;
        private IControlService controlService = null;
        private SimpleViewModel viewModel = null;


        //object under test
        private ViewStateBinder binder = null;
        //used by the mock data service as a backing store
        private Binding.BindingCollection collection = null;

        [TestInitialize()]
        public void SetupForTest()
        {

            mockContainer = MockRepository.GenerateMock<IServiceProviderBindingContainer>();
            dataStorageService = new ViewStateStorageService(new StateBag());
            controlService = MockRepository.GenerateMock<IControlService>();
           
            //object under test
            binder = new ViewStateBinder(mockContainer, dataStorageService, controlService);

        }

        [TestMethod]
        public void ContextIsConsistentBetweenGetsTest()
        {
            viewModel = new SimpleViewModel() { EmployeeName = "Dave Smith" };
            //view supplies initial state
            mockContainer.Expect(mc => mc.DataContext).IgnoreArguments().Return(viewModel);

            //will store in datastorage
            object binderContext = binder.DataContext;
            Assert.IsNotNull(dataStorageService.Retrieve<object>(VIEW_MODEL_KEY));

            //set is post back to true
            mockContainer.Expect(mc => mc.IsPostBack).IgnoreArguments().Return(true);

            //retreive = should get the vm from storage
            SimpleViewModel vm = binder.DataContext as SimpleViewModel;
            vm.EmployeeName = "Sam Shiles";

            SimpleViewModel vm2 = binder.DataContext as SimpleViewModel;
         
            Assert.AreEqual(vm.EmployeeName, vm2.EmployeeName);
        }
    
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions