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

CSLA4.0在SILVERLIGHT5.0中 构建可编辑的BusinessListBase对象,第1张

概述CSLA4.0在SILVERLIGHT5.0中 构建编辑的BusinessListBase对象 --服务器端类库 namespace Library {     [Serializable] #if !SILVERLIGHT     [Csla.Server.ObjectFactory("DataAccess.ShopRepository, DataAccess", "", "FetchColl

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,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,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,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();         }

总结

以上是内存溢出为你收集整理的CSLA4.0在SILVERLIGHT5.0中 构建可编辑的BusinessListBase对象全部内容,希望文章能够帮你解决CSLA4.0在SILVERLIGHT5.0中 构建可编辑的BusinessListBase对象所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1065027.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存