Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm learning MVVM for a week or so, I've seen Jason Dolinger video many times http://blog.lab49.com/archives/2650. Step by step following Jason Dolinger video i've created my own application which almost the same to one presented by Jason Dolinger. I'm completely satisfied with my current application, however, because the recorded video is several years old I do think that something probably may be improved or some things are obsolete?

namespace TerminatorConsole.Model
{
    public interface IWcfModel
    {
        List<ConsoleData> DataList { get; set; }
        event Action<List<ConsoleData>> DataArrived;
    }
}

namespace TerminatorConsole.Model
{
    class WcfModel : IWcfModel
    {
        private List<ConsoleData> _dataList;

        public List<ConsoleData> DataList
        {
            get { return _dataList; }
            set { _dataList = value;
                var dataDel = DataArrived;
                if (dataDel != null)
                {
                    DataArrived(_dataList);
                }
            }
        }

        public event Action<List<ConsoleData>> DataArrived;
    }
}

namespace TerminatorConsole.Utils
{
    class DispatchingWcfModel : IWcfModel
    {

        private readonly IWcfModel _underlying;
        private readonly Dispatcher _currentDispatcher;

        public DispatchingWcfModel(IWcfModel model)
        {
            _currentDispatcher = Dispatcher.CurrentDispatcher;
            _underlying = model;
            _underlying.DataArrived += new Action<List<ConsoleData>>(_underlying_DataArrived);
        }

        private void _underlying_DataArrived(List<ConsoleData> obj)
        {
            Action dispatchAction = () =>
            {
                if (DataArrived != null)
                {
                    DataArrived(obj);
                }
            };
            _currentDispatcher.BeginInvoke(DispatcherPriority.DataBind, dispatchAction);
        }

        public List<ConsoleData> DataList
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public event Action<List<ConsoleData>> DataArrived;
    }
}

namespace TerminatorConsole.ViewModel
{
    public class OverviewViewModel
    {
        private IWcfModel model;
        public ObservableCollection<ConsoleData> DataList { get; set; }


        public OverviewViewModel(IWcfModel model)
        {
            this.model = model;
            this.DataList = new ObservableCollection<ConsoleData>();
            model.DataArrived += new Action<List<ConsoleData>>(model_DataArrived);
        }

        private void model_DataArrived(List<ConsoleData> dataList)
        {
            DataList.Clear();
            dataList.ForEach(x => DataList.Add(x));
        }

        private static ObservableCollection<T> ToObservableCollection<T>(IEnumerable<T> coll)
        {
            var c = new ObservableCollection<T>();
            foreach (var e in coll) c.Add(e);
            return c;
        }
    }

}

namespace TerminatorConsole
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private WcfLoader loader;

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            IUnityContainer container = new UnityContainer();

            WcfModel model = new WcfModel();
            DispatchingWcfModel dispatching = new DispatchingWcfModel(model);
            loader = new WcfLoader(model);
            container.RegisterInstance<IWcfModel>(dispatching);

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

namespace TerminatorConsole
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private OverviewViewModel _vm;

        [Dependency]
        public OverviewViewModel VM
        {
            set
            {
                _vm = value;
                this.DataContext = _vm;
            }
        }

        public MainWindow()
        {
            InitializeComponent();
        }
    }

}

namespace TerminatorConsole.Utils
{
    class WcfLoader
    {
        private IManagementConsole pipeProxy;

        Timer refresh_timer;
        private int interval = 1000;
        private WcfModel model;

        public WcfLoader(WcfModel model)
        {
            this.model = model;

            ChannelFactory<IManagementConsole> pipeFactory =
                new ChannelFactory<IManagementConsole>(
                    new NetNamedPipeBinding(),
                    new EndpointAddress("net.pipe://localhost/PipeMBClientManagementConsole"));

            pipeProxy = pipeFactory.CreateChannel();

            refresh_timer = new System.Timers.Timer(interval);
            refresh_timer.AutoReset = false;
            refresh_timer.Elapsed += OnRefreshTimedEvent;

            refresh_timer.Start();
        }

        void OnRefreshTimedEvent(object source, ElapsedEventArgs args)
        {
            try
            {
                LoadModel(model);
            }
            finally
            {
                refresh_timer.Start();
            }
        }

        public void LoadModel(WcfModel model)
        {
            var dataList = new List<ConsoleData>();
            foreach (StrategyDescriptor sd in pipeProxy.GetStrategies())
            {
                dataList.Add(pipeProxy.GetData(sd.Id));
            }
            model.DataList = dataList;
        }
    }
}
share|improve this question
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.