CSLA4.0在SILVERLIGHT5.0中 构建可编辑的BusinessListBase对象

来源:互联网 发布:淘宝一件代发怎么退货 编辑:程序博客网 时间:2024/06/05 15:12

CSLA4.0在SILVERLIGHT5.0中 构建可编辑的BusinessListBase对象

--服务器端类库

namespace Library
{
    [Serializable]
#if !SILVERLIGHT
    [Csla.Server.ObjectFactory("DataAccess.ShopRepository, DataAccess", "", "FetchCollection", "UpdateCollection", "DeleteCollection", null)]
#endif
    public class ShopCollection : BusinessListBase<ShopCollection, ShopEdit>
    {
        public static void GetAll(EventHandler<DataPortalResult<ShopCollection>> callback)
        {
            DataPortal.BeginFetch<ShopCollection>(callback);
        }
        
        public ShopEdit NewShopEdit()
        {
            var shop = ShopEdit.NewShopEdit();
            Add(shop);             
            OnAddedNew(shop);
            return shop;
        }




    }
}



/******************************************************************************
 ----------------------------------------------------------------
  模块名称: ShopEdit[只读数据模型]
  编者    : 李春雷     创建日期: 2012年08月02日
  功能描述: 
 ----------------------------------------------------------------
  修改日期: 修改人: 
  修改内容: 
 ----------------------------------------------------------------
******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;
using Csla.Serialization;


namespace Library
{


    /// <summary>
    /// <para>ShopEdit Object</para>
    /// <para>Summary description for ShopEdit.</para>
    /// <para><see cref="member"/></para>
    /// <remarks></remarks>
    /// </summary>
    [Serializable]
#if !SILVERLIGHT
    [Csla.Server.ObjectFactory("DataAccess.ShopRepository, DataAccess")]
#endif
    public class ShopEdit : BusinessBase<ShopEdit>
    {


        #region Public Properties
        public static PropertyInfo<string> IdProperty = RegisterProperty<string>(c => c.Id);
        public string Id
        {
            get { return GetProperty(IdProperty); }
            set { SetProperty(IdProperty, value); }
        }


        public static PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);
        public string Name
        {
            get { return GetProperty(NameProperty); }
            set { SetProperty(NameProperty, value); }
        }


        public static PropertyInfo<string> RemarkProperty = RegisterProperty<string>(c => c.Remark);
        public string Remark
        {
            get { return GetProperty(RemarkProperty); }
            set { SetProperty(RemarkProperty, value); }
        }


        public static PropertyInfo<string> CreaterProperty = RegisterProperty<string>(c => c.Creater);
        public string Creater
        {
            get { return GetProperty(CreaterProperty); }
            set { SetProperty(CreaterProperty, value); }
        }


        public static PropertyInfo<DateTime> CreateDateProperty = RegisterProperty<DateTime>(c => c.CreateDate);
        public DateTime CreateDate
        {
            get { return GetProperty(CreateDateProperty); }
            set { SetProperty(CreateDateProperty, value); }
        }


        #endregion


        #region Method
        public static void GetById(string id, EventHandler<DataPortalResult<ShopEdit>> callback)
        {
            DataPortal.BeginFetch<ShopEdit>(id, callback);
        }
        public static ShopEdit NewShopEdit()
        {
            var shop = new ShopEdit();
            shop.CreateDate = DateTime.Now;
            shop.MarkAsChild();
            return shop;
        }
        public static void Create(EventHandler<DataPortalResult<ShopEdit>> callback) {
            DataPortal.BeginCreate<ShopEdit>(callback);
        }
#if !SILVERLIGHT
        public static ShopEdit GetById(string id)
        {
            
            return DataPortal.Fetch<ShopEdit>(id);
        }
#endif


        #endregion
    }
}



--数据访问层,采用工厂模式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;
using Library;
using Dapper;
using Csla.Reflection;


namespace DataAccess
{
    public class ShopRepository : Csla.Server.ObjectFactory
    {
        public ShopList FetchList()
        {
            var result = (ShopList)MethodCaller.CreateInstance(typeof(ShopList));
            string sqlstr = "SELECT Id, [Name], Remark, Creater, CreateDate FROM dbo.ShopInfo order by [Name]";
            var query = Database.DbService.Query<ShopInfo>(sqlstr);           
            result.RaiseListChangedEvents = false;
            result.AddRange(query);
            result.RaiseListChangedEvents = true;
            return result;
        }
       
        public ShopCollection FetchCollection()
        {
            var result = (ShopCollection)MethodCaller.CreateInstance(typeof(ShopCollection));
            string sqlstr = "SELECT Id, [Name], Remark, Creater, CreateDate FROM dbo.ShopInfo order by [Name]";
            var query = Database.DbService.Query<ShopEdit>(sqlstr);
            foreach (var item in query)
            {
                MarkAsChild(item);
                MarkOld(item);
            }
            result.RaiseListChangedEvents = false;
            result.AddRange(query);
            result.RaiseListChangedEvents = true;
            return result;
        }
        #region insert,update,delete
        public ShopEdit Create()
        {
            var result = new ShopEdit();
            MarkAsChild(result);
            MarkNew(result);
            CheckRules(result);
            return result;
        }
        //public ShopEdit Create()
        //{
        //    var obj = (ShopEdit)MethodCaller.CreateInstance(typeof(ShopEdit));
        //    MarkNew(obj);
        //    CheckRules(obj);
        //    return obj;
        //}
        public ShopEdit Fetch(string id)
        {
            string sqlstr = "SELECT Id, [Name], Remark, Creater, CreateDate FROM dbo.ShopInfo" +
                        " WHERE id=@id";
            var result = Database.DbService.Query<ShopEdit>(sqlstr, new { id }).SingleOrDefault();
            MarkOld(result);
            return result;
        }


        public ShopEdit Update(ShopEdit obj)
        {
            if (obj.IsDeleted)
            {
                if (!obj.IsNew)
                {
                    // delete data
                    Delete(obj.Id);
                    return Create();
                }
                MarkNew(obj);
            }
            else
            {
                string sqlstr = "";
                if (obj.IsNew)
                {
                    sqlstr = "INSERT INTO dbo.ShopInfo(Id, [Name], Remark, Creater, CreateDate) " +
                            "VALUES (@Id, @Name, @Remark, @Creater, getdate())";
                }
                else
                {
                    sqlstr = "UPDATE dbo.ShopInfo " +
                            "SET  [Name] = @Name," +
                            "Remark = @Remark" +
                            " WHERE id=@id";
                }
                Database.DbService.Execute(sqlstr, obj);
                MarkOld(obj);
            }
            return obj;
        }


        public void Delete(string id)
        {
            string sqlstr = "delete from shopinfo where id=@id";
            Database.DbService.Execute(sqlstr, new { id });
        }


        public ShopCollection UpdateCollection(ShopCollection obj)
        {
            obj.RaiseListChangedEvents = false;
            foreach (var item in GetDeletedList<ShopEdit>(obj))
                Update(item);
            foreach (var item in obj)
             
                Update(item);
            GetDeletedList<ShopEdit>(obj).Clear();
            obj.RaiseListChangedEvents = true;
            return obj;
        }
        public ShopCollection DeleteCollection(ShopCollection obj)
        {
            foreach (var item in obj)
            {
                Delete(item.Id);
            }
            return obj;
        }
        #endregion
    }
}



--silverlight端

  ShopCollection shops;

 private void ToolbarButton_SaveClick(object sender, RoutedEventArgs e)
        {           
            shops.BeginSave((o, ee) =>
            {
                if (ee.Error != null)
                    WindowsManager.ShowModal("出错", "原因:" + ee.Error.Message);
                else
                    WindowsManager.ShowModal("保存成功", "所有数据已经成功保存");
            });
            maingrid.Columns[0].IsReadOnly = true;
        }


 private void ToolbarButton_AddClick(object sender, RoutedEventArgs e)
        {
            var d = shops.NewShopEdit();
        }