//3.0一?致?
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;

namespace ProjectTracker.Library
{
    /// <summary>
    /// 集?成?自?ReadOnlyListBase基?类?,?提?供?了?两?个?泛?型?参?数?,?一?个?是?业?务?对?象?本?身?,?另?一?个?是?子?对?象?类?型?
    /// 提?供?对?只?读?列?表?显?示?的?支?持?(?)?
    /// </summary>
    [Serializable()]
    public class ResourceList : ReadOnlyListBase<ResourceList, ResourceInfo>
    {
        #region Factory Methods
        
        /// <summary>
        /// CriTeria参?数?由?于?没?有?参?数?要?传?递?,?所?以?默?认?构?造?,?保?留?Fetch的?方?法?签?名?不?变?,?
        /// </summary>
        /// <returns></returns>
        public static ResourceList GetResourceList()
        {
            return DataPortal.Fetch<ResourceList>(new Criteria());
        }

        /// <summary>
        /// 私?有?构?造?器?
        /// </summary>
        private ResourceList()
        { /* require use of factory methods */ }

        #endregion

        #region Data Access

        /// <summary>
        /// 标?准?类?
        /// </summary>
        [Serializable()]
        private class Criteria
        { /* no criteria - retrieve all resources */ }

        private void DataPortal_Fetch(Criteria criteria)
        {
            this.RaiseListChangedEvents = false;
            using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
            {
                cn.Open();
                using (SqlCommand cm = cn.CreateCommand())
                {
                    cm.CommandType = CommandType.StoredProcedure;
                    cm.CommandText = "getResources";

                    using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
                    {
                        IsReadOnly = false;
                        while (dr.Read())
                        {
                            //调?用?子?类?的?构?造?并?添?加?到?此?业?务?对?象?中?来?
                            ResourceInfo info = new ResourceInfo(dr);
                            this.Add(info);
                        }
                        IsReadOnly = true;
                    }
                }
            }
            this.RaiseListChangedEvents = true;
        }

        #endregion

    }
}

 

//3.0一?致?
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Csla;
using Csla.Data;

namespace ProjectTracker.Library
{
    /// <summary>
    /// 集?成?基?类?,?需?要?一?个?业?务?对?象?类?型?泛?型?参?数?(?)?
    /// </summary>
    [Serializable()]
    public class ResourceInfo : ReadOnlyBase<ResourceInfo>
    {
        #region Business Methods

        //-----------------------------
        private int _id;
        private string _name;

        public int Id
        {
            get { return _id; }
        }

        public string Name
        {
            get { return _name; }
        }
        //-----------------------------
        /// <summary>
        /// 为?Object提?供?比?较?参?数?的?重?载?
        /// </summary>
        /// <returns></returns>
        protected override object GetIdValue()
        {
            return _id;
        }

        /// <summary>
        /// 从?在?Tostring,?返?回?名?称?
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return _name;
        }

        #endregion

        #region Constructors 构?造?器?

        /// <summary>
        /// 默?认?构?造?器?
        /// </summary>
        private ResourceInfo()
        { /* require use of factory methods */ }
        
        /// <summary>
        /// 内?联?调?用?,?构?造?初?始?化?私?有?成?员?
        /// </summary>
        /// <param name="dr"></param>
        internal ResourceInfo(SafeDataReader dr)
        {
            _id = dr.GetInt32("Id");
            _name = string.Format("{0}, {1}", dr.GetString("LastName"), dr.GetString("FirstName"));
        }

        #endregion
    }
}

 

代码下载:CSLA3.0中文学习演示程序1.2.rar

posted on 2009-10-06 09:55  冯瑞涛  阅读(642)  评论(0编辑  收藏  举报