Click here to Skip to main content
15,893,668 members
Articles / Desktop Programming / WPF

Extending Castle DynamicProxy

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
8 May 2010CPOL7 min read 25.8K   183   12  
Shows how to extend the proxy generated by this framework by using Reflection.Emit.
using System;
using System.Collections.Generic;
using System.Text;
using PresentationModelBase;
using System.Threading;
using System.ComponentModel;

namespace App.Core
{
    public class EntityCollectionViewPresentationModel<T> : SelectableViewPresentationModel, IEntityCollectionViewPresentationModel<T>
        where T : class, new()
    {
        public EntityCollectionViewPresentationModel()
        {
            _mainView = Get<IMainViewPresentationModel>();
            _entityDao = Get<IDao<T>>();
            _entityCollection = Get<ICollection<IEntityViewPresentationModel<T>>>();
            _entityDao.Changed += _entityDao_Changed;
        }

        void _entityDao_Changed(object sender, ListChangedEventArgs e)
        {
            T item = (T)sender;
            switch (e.ListChangedType)
            {
                case ListChangedType.ItemAdded:
                    var newView = Get<IEntityViewPresentationModel<T>>();
                    newView.Entity = item;
                    _entityCollection.Add(newView);
                    break;
                case ListChangedType.ItemChanged:
                    var view = Find(item);
                    view.Entity = null;
                    view.Entity = item;
                    break;
            }
        }

        IEntityViewPresentationModel<T> Find(T entity)
        {
            foreach (var item in _entityCollection)
            {
                if (entity == item.Entity)
                    return item;
            }
            return null;
        }

        IMainViewPresentationModel _mainView;
        IDao<T> _entityDao;

        public void LoadData()
        {
            Thread.Sleep(3000);
            _entityCollection.Clear();
            foreach (T item in _entityDao.SelectAll())
            {
                var entityView = Get<IEntityViewPresentationModel<T>>();
                entityView.Entity = item;
                _entityCollection.Add(entityView);
            }
        }

        #region IEntityCollectionViewPresentationModel<T> Members

        private ICollection<IEntityViewPresentationModel<T>> _entityCollection;
        public ICollection<IEntityViewPresentationModel<T>> EntityCollection
        {
            get { return _entityCollection; }
        }

        private IEntityViewPresentationModel<T> _selected;
        public virtual IEntityViewPresentationModel<T> Selected
        {
            get { return _selected; }
            set
            {
                if (_selected == value)
                    return;
                _selected = value;
                OnPropertyChanged("Selected");
                OnPropertyChanged("CanRemove");
            }
        }    

        public void CreateNew()
        {
            _mainView.CreateNew<T>();
        }

        public void RemoveSelected()
        {
            if (AskQuestion("Are you sure you remove this item?") == QuestionResult.Yes)
            {
                _entityDao.Remove(_selected.Entity);
                _entityCollection.Remove(_selected);
            }
        }

        public bool CanRemove
        {
            get { return Selected != null; }
        }

        #endregion
    }
}

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
Brazil Brazil
Software developer specialized in the .NET framework

Comments and Discussions