c# - 在选择设计模式方面需要帮助

标签 c# .net design-patterns

目前我有一堆 if else 语句来根据每个集合中的项目数设置 CategoryId。

例如,

public class TeamWork
{
    public string EmployeeName { get; set; }
    public int CategoryId { get; set; }
}

public class BLL
{
    public void SetCategoryId(ICollection<TeamWork> Converted, ICollection<TeamWork> Sourced)
    {
        if (Converted.Count == 1 && Sourced.Count == 1)
        {                
            if (String.Compare(Sourced.First().EmployeeName, Converted.First().EmployeeName) == 0)
            {
                // set category id to 1
                Converted.First().CategoryId = 1;
                Sourced.First().CategoryId = 1;                                            
            }
            else
            {
                // set category id to something                  
            }
        }
        else if (Sourced.Rows.Count == 1 && Converted.Rows.Count > 1)
        {
            // set category id to something           
        }
        // more if else statements...
    }
}

我认为有更好的方法可以通过应用某种设计模式来做到这一点。有什么建议么?谢谢!

最佳答案

Chain of responsibility是要走的路。

所以这个对象被传递给一系列命令对象,直到一个人能够采取行动并设置状态。

关于c# - 在选择设计模式方面需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5341696/

相关文章:

c# - 为什么删除 "throw"语句会导致 "use of unassigned local variable"编译错误?

C#:非法 Switch 语句的解决方法?

c# - 代码剖析 ASP.NET MVC2 应用程序

c# - 构建 Office 加载项时程序集绑定(bind)错误 : "FindRibbons" task failed unexpectedly

java - java中的设计模式

c# - 比较对象

c# - Unity3D - 是否可以通过脚本设置动画剪辑的属性?

c# - Azure Cosmos DB 添加字符串数组的复合索引

language-agnostic - 如何解决违反迪米特法则的问题?

ios - 在 UITableViewCell 和 UICollectionViewCell 之间共享代码