winform MVP实现

以前写winform项目都是界面与实现逻辑都放在一起,自从学了mvc3之后,又看了wpf的mvvp后,觉得winform的程序可以这样来写。

首先定义视图接口:

 public  interface IView<T> where T:class
    {

      T Model { get; set; }
      void BindingData(T model);
    }

然后定义Presenter接口:

 /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IPresention<T> where T : class
    {
        void InsertData(T model);
        void UpdateData(T model);
        void DeleteData(T model);
    }

实现IPresention<T>接口的类:

 public class DataDictPresentation : IPresention<ResourceDict>
    {

        IView<ResourceDict> _view;
        IView<IList<ResourceDict>> _viewList;

        public DataDictPresentation(IView<ResourceDict> view)
        {
            _view = view;
            if (_view.Model != null)
            {
                _view.BindingData(_view.Model);
            }

        }
        public DataDictPresentation(IView<IList<ResourceDict>> view)
        {
            _viewList = view;
            PageIndex = 1;
            PageSize = 14;

        }
        public int PageIndex { get; set; }
        public int PageCount { get; set; }
        public int PageSize { get; set; }
        public void InsertData(ResourceDict model)
        {
            

        }
        public void UpdateData(ResourceDict model)
        {

        }

        public void Query(ResourceDict filter)
        {


        }

        public void DeleteData(ResourceDict model)
        {

        }

}
   

 

最后在窗体页面上实现:

 public partial class DataDictBox : Form, IView<ResourceDict>
    {
        private DataDictPresentation presentation;
       
        public DataDictBox()
        {
            InitializeComponent();
            Initialize();
        }
        private static DataDictBox _instance;

        public static DataDictBox Instance
        {
            get
            {
                if (_instance == null ||
                    _instance.IsDisposed)
                {
                    _instance = new DataDictBox();
                }

                return _instance;
            }
        }
        private void Initialize()
        {
            this.Load += delegate
            {
                

       
                presentation = new DataDictPresentation(this);
            };

           
        }
       

        public ResourceDict Model { get; set; }
        public void BindingData(ResourceDict item)
        {
          

        }
        public void SetData(ResourceDict item)
        {
            
        }

这样完成了一个初步的MVP结构的模型。

 

posted @ 2013-04-28 17:01  小羽信息  阅读(5377)  评论(1编辑  收藏  举报