• CSLA.Net 3.0.5 项目管理示例 业务集合基类(ProjectResources.cs,ProjectResource.cs)


    using System;
    using System.Data;
    using System.Data.SqlClient;
    using Csla;
    using Csla.Data;
    
    namespace ProjectTracker.Library
    {
        /// <summary>
        /// 此?类?的?主?要?功?能?用?于?给?Project提?供?对?其?关?联?子?对?象?的?集?合?的?一?个?维?护?和?映?射?,?Project----ProjectResources----ProjectResource
        /// 她?是?一?个?集?合?,?是?父?对?象?组?织?子?对?象?的?一?个?集?合?
        /// ,?提?供?用?于?托?管?包?含?的?子?类?,?比?较?,?添?加?,?删?除?子?对?象?等?
        /// 
        /// </summary>
        [Serializable()]
        public class ProjectResources : BusinessListBase<ProjectResources, ProjectResource>
        {
            #region Business Methods
            
            /// <summary>
            /// 获?得?一?个?项?目?集?合?中?的?项?目?资?源?对?象?(?)?
            /// </summary>
            /// <param name="resourceId">项?目?资?源?ID</param>
            /// <returns></returns>
            public ProjectResource GetItem(int resourceId)
            {
                //从?当?前?对?象?集?合?中?搜?索?其?中?的?子?对?象?,?比?较?参?数?ID,?相?等?则?返?回?
                foreach (ProjectResource res in this)
                    if (res.ResourceId == resourceId)
                        return res;
                return null;
            }
    
            /// <summary>
            /// 通?过?指?定?的?ID值?,?像?当?前?集?合?中?加?入?子?对?象?,?分?配?
            /// </summary>
            /// <param name="resourceId">待?加?入?子?对?象?的?ID</param>
            public void Assign(int resourceId)
            {
                if (!Contains(resourceId))
                {
                    ProjectResource resource = ProjectResource.NewProjectResource(resourceId);
                    this.Add(resource);
                }
                else
                    throw new InvalidOperationException("资?源?已?经?在?项?目?中?存?在?");
            }
    
            /// <summary>
            /// 从?集?合?中?移?除?一?个?资?源?子?对?象?
            /// </summary>
            /// <param name="resourceId">待?移?除?子?对?象?id</param>
            public void Remove(int resourceId)
            {
                foreach (ProjectResource res in this)
                {
                    if (res.ResourceId == resourceId)
                    {
                        //从?集?合?类?中?移?除?,?集?合?类?是?一?个?泛?型?,?由?于?BusinessListBase集?成?自?集?合?基?类?,?所?以?他?也?同?时?拥?有?集?合?的?方?法?
                        //Remove,Add等?,?这?个?操?作?应?该?将?数?据?放?到?DEleteList中?
                        Remove(res);
                        break;
                    }
                }
            }
            /// <summary>
            ///  检?查?集?合?中?是?否?包?含?此?子?对?象?,?如?果?包?含?返?回?True,?否?则?返?回?false
            /// </summary>
            /// <param name="resourceId">子?对?象?ID,?资?源?id用?的?是?整?型?</param>
            /// <returns></returns>
            public bool Contains(int resourceId)
            {
                foreach (ProjectResource res in this)
                    if (res.ResourceId == resourceId)
                        return true;
                return false;
            }
    
            /// <summary>
            /// 在?删?除?已?删?除?的?集?合?中?搜?索?子?对?象?的?存?在?
            /// </summary>
            /// <param name="resourceId"></param>
            /// <returns></returns>
            public bool ContainsDeleted(int resourceId)
            {
                foreach (ProjectResource res in DeletedList)
                    if (res.ResourceId == resourceId)
                        return true;
                return false;
            }
    
            #endregion
    
            #region Factory Methods
            /// <summary>
            /// 内?联?工?厂?,?代?表?同?一?个?程?序?集?中?调?用?,?因?为?他?只?是?提?供?给?project对?象?在?创?建?的?时?候?同?时?被?创?建?的?一?个?方?法?,?调?用?请?看?project
            /// 而?UI不?会?去?使?用?她?
            /// </summary>
            /// <returns></returns>
            internal static ProjectResources NewProjectResources()
            {
                return new ProjectResources();
            }
            /// <summary>
            /// 通?过?dr获?得?一?个?集?合?
            /// </summary>
            /// <param name="dr"></param>
            /// <returns></returns>
            internal static ProjectResources GetProjectResources(SafeDataReader dr)
            {
                //调?用?带?参?数?的?构?造?器?
                return new ProjectResources(dr);
            }
            /// <summary>
            /// 默?认?构?造?器?
            /// </summary>
            private ProjectResources()
            {
                //标?记?为?子?关?系?
                MarkAsChild();
            }
            /// <summary>
            /// 带?参?数?的?构?造?器?
            /// </summary>
            /// <param name="dr"></param>
            private ProjectResources(SafeDataReader dr)
            {
                MarkAsChild();
                Fetch(dr);
            }
    
            #endregion
    
            #region Data Access
    
            // c从?数?据?看?加?载?数?据?
            private void Fetch(SafeDataReader dr)
            {
                //将?列?表?更?改?事?件?暂?时?设?置?为?失?效?,?增?加?效?率?(?)?
                this.RaiseListChangedEvents = false;
                while (dr.Read())
                    this.Add(ProjectResource.GetResource(dr));//DR传?个?子?对?象?用?于?初?始?化?一?个?子?对?象?
                //启?用?事?件?
                this.RaiseListChangedEvents = true;
            }
            /// <summary>
            /// (?)?,?这?个?方?法?是?内?联?方?法?,?因?为?她?只?是?Project来?调?用?的?
            /// 托?管?让?每?一?个?子?对?象?都?做?相?应?的?操?作?
            /// </summary>
            /// <param name="project"></param>
            internal void Update(Project project)
            {
                this.RaiseListChangedEvents = false;
                // update (thus deleting) any deleted child objects
                //从?已?删?除?列?表?中?轮?寻?每?一?个?元?素?调?用?其?删?除?自?身?方?法?,?注?意?他?们?要?传?入?其?关?联?的?项?目?对?象?
                foreach (ProjectResource obj in DeletedList)
                    obj.DeleteSelf(project);//从?数?据?库?中?删?除?(?)?
                // now that they are deleted, remove them from memory too
                //将?DeletedList从?系?统?缓?存?中?移?除?掉?所?有?的?元?素?,?deletedList被?清?空?,?同?步?数?据?库?
                DeletedList.Clear();
                
                // add/update any current child objects
                // 再?来?看?看?添?加?和?更?新?是?如?何?实?现?的?,?同?样?要?传?入?相?应?关?联?的?项?目?对?象?
                // 轮?寻?每?一?个?子?元?素?,?查?看?其?标?志?,?如?果?是?新?的?就?调?用?子?对?象?的?插?入?方?法?
                // 如?果?不?是?新?的?,?那?么?就?调?用?更?新?方?法?
                
                foreach (ProjectResource obj in this)
                {
                    //根?据?子?对?象?的?状?态?调?用?合?适?的?方?法?
                    if (obj.IsNew)
                        obj.Insert(project);
                    else
                        obj.Update(project);
                }
                this.RaiseListChangedEvents = true;
            }
    
            #endregion
    
        }
    }

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using Csla;
    using Csla.Data;
    using Csla.Validation;
    
    namespace ProjectTracker.Library
    {
        /// <summary>
        /// 此?类?为?分?配?资?源?类?,?用?于?和?ProjectResources类?合?作?为?Project处?理?分?配?给?项?目?的?资?源?的?类?、?
        /// 集?成?子?业?务?基?类?,?与?project是?相?同?的?,?但?是?她?还?集?成?自?一?个?IholdRoles接?口?
        /// 这?个?类?只?提?供?给?ProjectResources使?用?
        /// </summary>
        [Serializable()]
        public class ProjectResource : BusinessBase<ProjectResource>, IHoldRoles
        {
            #region Business Methods
    
            private int _resourceId;//资?源?id
            private string _firstName = string.Empty;//名?字?
            private string _lastName = string.Empty;//姓?氏?
            private SmartDate _assigned;//分?配?日?期?
            private int _role;//职?位?
            private byte[] _timestamp = new byte[8];//时?间?戳?,?用?于?处?理?并?发?修?改?的?情?况?,?先?写?者?赢?的?乐?观?并?发?
    
            //表?明?特?性?,?表?示?是?主?键?,?但?是?并?不?一?定?是?唯?一?的?可?识?别?值?
            [System.ComponentModel.DataObjectField(false, true)]
            public int ResourceId
            {
                [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
                get
                {
                    CanReadProperty(true);
                    return _resourceId;
                }
            }
    
            public string FirstName
            {
                [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
                get
                {
                    CanReadProperty(true);
                    return _firstName;
                }
            }
    
            public string LastName
            {
                [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
                get
                {
                    CanReadProperty(true);
                    return _lastName;
                }
            }
            /// <summary>
            /// 全?名?属?性?
            /// </summary>
            public string FullName
            {
                [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
                get
                {
                    if (CanReadProperty("FirstName") &&  CanReadProperty("LastName"))
                        return string.Format("{0}, {1}", LastName, FirstName);
                    else
                        throw new System.Security.SecurityException( "没?有?读?取?权?限?");
                }
            }
    
            /// <summary>
            /// 分?配?日?期?
            /// </summary>
            public string Assigned
            {
                [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
                get
                {
                    CanReadProperty(true);
                    //获?取?时?间?的?文?本?
                    return _assigned.Text;
                }
            }
    
            public int Role
            {
                [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
                get
                {
                    CanReadProperty(true);
                    return _role;
                }
                [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
                set
                {
                    CanWriteProperty(true);
                    //如?果?不?重?复?则?更?改?
                    if (!_role.Equals(value))
                    {
                        _role = value;
                        //1调?用?对?数?据?验?证?规?则?的?检?查?AddBusinessRules,2,IsDirty标?记?已?经?修?改?,
                        //3,出?发?属?性?更?改?事?件?提?供?给?UI刷?新?的?警?示?(411),?发?生?变?化?就?要?调?用?此?方?法?
                        PropertyHasChanged();
                    }
                }
            }
            //获?得?资?源?
            public Resource GetResource()
            {
                return Resource.GetResource(_resourceId);
            }
            //提?供?Id,?是?为?了?支?持?Object的?,?Equals().toString(),GetHashCode()
            protected override object GetIdValue()
            {
                return _resourceId;
            }
    
            #endregion
    
            #region Validation Rules
    
            protected override void AddBusinessRules()
            {
                //通?过?RuleHandler委?托?,?调?用?Assignment.ValidRole方?法?,?对?Role进?行?验?证?,?这?个?方?法?在?Assignment中?定?义?,?你?可?以?看?一?下?,?是?公?用?的?方?法?
                //如?果?返?回?false的?话?就?是?代?表?验?证?失?败?,?true验?证?成?功?
                ValidationRules.AddRule( new Csla.Validation.RuleHandler( Assignment.ValidRole), "Role");
            }
    
            #endregion
    
            #region Authorization Rules
    
            protected override void AddAuthorizationRules()
            {
                //只?有?ProjectManager,?才?有?权?限?修?改?Role
                AuthorizationRules.AllowWrite( "Role", "ProjectManager");
            }
    
            #endregion
    
            #region Factory Methods
    
            internal static ProjectResource NewProjectResource(int resourceId)
            {
                //调?用?构?造?器?初?始?化?,?,?获?得?一?个?资?源?,?,?给?予?一?个?默?认?职?位?
                return new ProjectResource( Resource.GetResource(resourceId), RoleList.DefaultRole());
            }
    
            internal static ProjectResource GetResource(SafeDataReader dr)
            {
                //调?用?构?造?器?初?始?化?
                return new ProjectResource(dr);
            }
    
            //默?认?构?造?
            private ProjectResource()
            {
                //标?记?为?子?很?重?要?,?必?须?的?
                MarkAsChild();
            }
    
            /// <summary>
            /// 通?过?一?个?已?有?资?源?,?和?一?个?职?位?来?构?造?一?个?ProjectResource
            /// </summary>
            /// <param name="resource"></param>
            /// <param name="role"></param>
            private ProjectResource(Resource resource, int role)
            {
    
                MarkAsChild();
                _resourceId = resource.Id;
                _lastName = resource.LastName;
                _firstName = resource.FirstName;
                _assigned.Date = Assignment.GetDefaultAssignedDate();
                _role = role;
            }
            /// <summary>
            /// 单?参?数?构?造?,?这?个?时?候?DR是?一?个?记?录?
            /// </summary>
            /// <param name="dr"></param>
            private ProjectResource(SafeDataReader dr)
            {
                MarkAsChild();
                Fetch(dr);
            }
    
            #endregion
    
            #region Data Access
    
            private void Fetch(SafeDataReader dr)
            {
                _resourceId = dr.GetInt32("ResourceId");
                _lastName = dr.GetString("LastName");
                _firstName = dr.GetString("FirstName");
                _assigned = dr.GetSmartDate("Assigned");
                _role = dr.GetInt32("Role");
                //非?常?标?准?的?时?间?戳?
                dr.GetBytes("LastChanged", 0, _timestamp, 0, 8);
                //做?旧?,?因?为?此?对?象?与?数?据?库?是?数?据?同?步?的?
                MarkOld();
            }
    
            /// <summary>
            /// 内?联?的?,?当?其?父?对?象?调?用?此?对?象?时?候?判?断?对?象?状?态?,?isDeleted=false isnew=true,?调?用?此?方?法?
            /// </summary>
            /// <param name="project"></param>
            internal void Insert(Project project)
            {
                // if we're not dirty then don't update the database
                //如?果?当?前?对?象?不?脏?也?就?是?没?有?给?其?付?过?值?,?没?有?必?要?更?新?到?数?据?库?
                if (!this.IsDirty) return;
    
                using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
                {
                    cn.Open();
                    //调?用?分?派?类?中?的?方?法?,?并?且?返?回?时?间?戳?
                    //参?数?是?连?接?,?项?目?id父?id,?资?源?id子?id,?分?配?时?间?,?角?色?
                    _timestamp = Assignment.AddAssignment(cn, project.Id, _resourceId, _assigned, _role);
                    //做?旧?This对?象?,?因?为?她?已?经?与?数?据?库?同?步?,?也?就?是?说?她?已?经?在?数?据?库?中?了?,?不?是?新?的?数?据?库?
                    //这?个?方?法?如?果?是?在?根?对?象?数?据?门?户?会?自?动?调?用?,?但?是?子?对?象?需?要?手?动?
                    //这?个?方?法?会?将?isNew标?记?,?和?IsDirty标?记?设?置?为?False,?因?为?这?时?,?这?个?数?据?既?不?是?新?的?也?不?是?脏?的?
                    MarkOld();
                }
            }
    
            internal void Update(Project project)
            {
                // if we're not dirty then don't update the database
                if (!this.IsDirty) return;
    
                using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
                {
                    cn.Open();
                    //与?Insert同?理?
                    _timestamp = Assignment.UpdateAssignment(cn, project.Id, _resourceId, _assigned, _role, _timestamp);
                    MarkOld();
                }
            }
    
            internal void DeleteSelf(Project project)
            {
                // if we're not dirty then don't update the database
                if (!this.IsDirty) return;
    
                // if we're new then don't update the database
                if (this.IsNew) return;
    
                using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
                {
                    cn.Open();
                    Assignment.RemoveAssignment(cn, project.Id, _resourceId);
                    MarkNew();
                }
            }
    
            #endregion
    
        }
    }
    //此?业?务?类?实?现?与?.0完?全?一?致?
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using Csla;
    using Csla.Validation;
    using Csla.Data;
    using System.Reflection;
    
    namespace ProjectTracker.Library
    {
        //定?义?这?个?接?口?是?为?了?使?子?类?提?供?,?可?以?提?供?对?Role属?性?的?访?问?
        internal interface IHoldRoles
        {
            int Role { get; set; }
        }
        /// <summary>
        /// 这?个?类?定?义?为?内?联?的?,?同?时?是?静?态?的?,?是?因?为?这?个?类?是?提?供?了?ProjectResource和?ResourceAssignmet的?通?用?方?法?的?集?合?,?因?为?
        /// 这?个?两?个?对?象?都?在?操?作?这?相?同?的?数?据?,?一?个?资?源?属?于?某?个?项?目?,?和?一?个?项?目?拥?有?多?少?资?源?在?是?一?个?含?义?
        /// 静?态?类?,?是?因?为?这?个?类?没?有?任?何?私?有?变?量?,?也?就?是?数?据?域?,?所?以?声?明?为?静?态?方?便?使?用?
        /// 此?对?象?对?Assignment表?进?行?操?作?,?这?个?表?是?一?个?中?间?表?,?用?来?存?储?多?对?多?的?表?数?据?映?射?
        /// 通?用?原?则?
        /// </summary>
        internal static class Assignment
        {
    
            #region Business Methods
    
            /// <summary>
            /// 获?得?默?认?的?分?配?时?间?
            /// </summary>
            /// <returns></returns>
            public static DateTime GetDefaultAssignedDate()
            {
                return DateTime.Today;
            }
    
            #endregion
    
            #region Validation Rules
    
            /// <summary>
            /// Ensure the Role property value exists
            /// in RoleList
            /// 像?上?面?的?英?文?解?释?一?样?,?这?个?验?证?确?保?了?Role在?RoleList中?可?以?被?找?到?
            /// 这?个?方?法?是?一?个?公?用?的?验?证?方?法?ProjectResource和?ResourceAssignmet通?过?委?托?来?调?用?这?个?方?法?验?证?
            /// </summary>
            public static bool ValidRole(object target, RuleArgs e)
            {
                int role = ((IHoldRoles)target).Role;
    
                //通?过?RoleList的?GetList获?得?List对?象?,?RolesList对?象?拥?有?的?ContainsKey方?法?可?以?判?断?此?id在?列?表?中?是?否?存?在?
                if (RoleList.GetList().ContainsKey(role))
                    return true;
                else
                {
                    e.Description = "职?务?必?须?在?职?务?列?表?中?存?在?";
                    return false;
                }
            }
    
            #endregion
    
            #region Data Access
            /// <summary>
            /// 添?加?一?个?分?配?
            /// </summary>
            /// <param name="cn">连?接?</param>
            /// <param name="projectId">项?目?id</param>
            /// <param name="resourceId">资?源?id</param>
            /// <param name="assigned">分?配?时?间?</param>
            /// <param name="role">职?位?</param>
            /// <returns></returns>
            public static byte[] AddAssignment(SqlConnection cn, Guid projectId, int resourceId, SmartDate assigned, int role)
            {
                using (SqlCommand cm = cn.CreateCommand())
                {
                    cm.CommandText = "addAssignment";
                    return DoAddUpdate(cm, projectId, resourceId, assigned, role);
                }
            }
            /// <summary>
            /// 更?新?一?个?分?配?
            /// </summary>
            /// <param name="cn">连?接?</param>
            /// <param name="projectId">项?目?id</param>
            /// <param name="resourceId">资?源?id</param>
            /// <param name="assigned">分?配?时?间?</param>
            /// <param name="newRole">更?新?后?的?职?位?</param>
            /// <param name="timestamp">时?间?戳?</param>
            /// <returns></returns>
            public static byte[] UpdateAssignment(SqlConnection cn, Guid projectId, int resourceId, SmartDate assigned, int newRole, byte[] timestamp)
            {
                using (SqlCommand cm = cn.CreateCommand())
                {
                    cm.CommandText = "updateAssignment";
                    cm.Parameters.AddWithValue("@lastChanged", timestamp);
                    return DoAddUpdate( cm, projectId, resourceId, assigned, newRole);
                }
            }
            /// <summary>
            /// 添?加?和?更?新?的?公?共?方?法?
            /// </summary>
            /// <param name="cm"></param>
            /// <param name="projectId"></param>
            /// <param name="resourceId"></param>
            /// <param name="assigned"></param>
            /// <param name="newRole"></param>
            /// <returns></returns>
            private static byte[] DoAddUpdate(SqlCommand cm, Guid projectId, int resourceId, SmartDate assigned, int newRole)
            {
                cm.CommandType = CommandType.StoredProcedure;
                cm.Parameters.AddWithValue("@projectId", projectId);
                cm.Parameters.AddWithValue("@resourceId", resourceId);
                cm.Parameters.AddWithValue("@assigned", assigned.DBValue);
                cm.Parameters.AddWithValue("@role", newRole);
                //时?间?戳?返?回?值?
                SqlParameter param = new SqlParameter("@newLastChanged", SqlDbType.Timestamp);
                param.Direction = ParameterDirection.Output;
                cm.Parameters.Add(param);
    
                cm.ExecuteNonQuery();
    
                return (byte[])cm.Parameters["@newLastChanged"].Value;
            }
            /// <summary>
            /// 删?除?一?个?分?配?
            /// </summary>
            /// <param name="cn"></param>
            /// <param name="projectId"></param>
            /// <param name="resourceId"></param>
            public static void RemoveAssignment(SqlConnection cn, Guid projectId, int resourceId)
            {
                using (SqlCommand cm = cn.CreateCommand())
                {
                    cm.CommandType = CommandType.StoredProcedure;
                    cm.CommandText = "deleteAssignment";
                    cm.Parameters.AddWithValue("@projectId", projectId);
                    cm.Parameters.AddWithValue("@resourceId", resourceId);
    
                    cm.ExecuteNonQuery();
                }
            }
    
            #endregion
    
        }
    }

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

    冯瑞涛
  • 相关阅读:
    04-增删改查
    03-编写dao实现类方式
    02-基于注解的入门案例
    .net core api 图片上传与加载
    笔记一、数据库初始化 约定
    .net core MVC中级教程(四)
    .net core MVC中级教程(三)
    Windows 盘符映射
    c# 队列和堆栈
    isValidNode
  • 原文地址:https://www.cnblogs.com/finehappy/p/1578363.html
Copyright © 2020-2023  润新知