在C#中路由对象而不使用switch语句 [英] Routing an object in C# without using switch statements

查看:144
本文介绍了在C#中路由对象而不使用switch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用c#.net 4.0编写一个软件,并且为了确保代码库在特定区域中的可扩展性,可重用性和灵活性而遇到麻烦。

I am writing a piece of software in c# .net 4.0 and am running into a wall in making sure that the code-base is extensible, re-usable and flexible in a particular area.

我们收到的数据需要按离散的组织单位细分。随着公司的发展,这些单位将需要更改,分类,删除和添加。

We have data coming into it that needs to be broken down in discrete organizational units. These units will need to be changed, sorted, deleted, and added to as the company grows.

无论我们如何分割数据结构,我们都会不断尝试避免遇到一系列条件语句(从100左右开始),

No matter how we slice the data structure we keep running into a boat-load of conditional statements (upwards of 100 or so to start) that we are trying to avoid, allowing us to modify the OUs easily.

我们希望找到一种面向对象的方法,该方法可以使我们根据该对象的属性将其路由到不同的工作流

We are hoping to find an object-oriented method that would allow us to route the object to different workflows based on properties of that object without having to add switch statements every time.

例如,假设我有一个名为 Order的对象进入系统。该对象内部有 orderItems。这些不同类型的 orderItems中的每一个都需要在代码中触发不同的函数,以便进行适当处理。每个 orderItem都有不同的工作流程。条件条件基本上是这样的-

So, for example, let's say I have an object called "Order" come into the system. This object has 'orderItems' inside of it. Each of those different kinds of 'orderItems' would need to fire a different function in the code to be handled appropriately. Each 'orderItem' has a different workflow. The conditional looks basically like this -

if(order.orderitem == 'photo')
  {do this} 
else if(order.orderitem == 'canvas')
  {do this}

编辑:尝试澄清。

推荐答案

我不确定您的问题定义是否很好,您需要很多

I'm not sure your question is very well defined, you need a lot more specifics here - a sample piece of data, sample piece of code, what have you tried...


无论我们如何分割数据结构,我们一直遇到大量我们试图避免的条件语句(开始时需要向上输入100个左右)

No matter how we slice the data structure we keep running into a boat-load of conditional statements (upwards of 100 or so to start) that we are trying to avoid

这通常意味着您正在尝试在代码中编码数据-只需添加一个数据字段(或几个)。

This usually means you're trying to encode data in your code - just add a data field (or a few).

机会就是您的如果是相互链接的,则很难提出100个独立的ifs-这意味着您有100个独立的分支用于100个独立的数据条件。在我的职业生涯中,我还没有遇到过需要硬编码100个ifs的事情。

Chances are your ifs are linked to each other, it's hard to come up with 100 independent ifs - that would imply you have 100 independent branches for 100 independent data conditions. I haven't encountered such a thing in my career that really would require hard-coding 100 ifs.

在最坏的情况下,您可以使其他数据字段包含配置文件甚至您选择的脚本。无论哪种情况-如果您需要100个ifs,数据都是不完整的

Worst case scenario you can make an additional data field contain a config file or even a script of your choice. Either case - your data is incomplete if you need 100 ifs

使用更新后的问题,这里是一个简单方法,技术含量低。您可以通过依赖项注入和一些配置来做得更好,但是也会变得过多,因此请注意:

With the update you've put in your question here's one simple approach, kind of low tech. You can do better with dependency injection and some configuration but that can get excessive too, so be careful:

public class OrderHandler{ 
  public static Dictionary<string,OrderHandler> Handlers = new Dictionary<string,OrderHandler>(){
  {"photo", new PhotoHandler()},
  {"canvas", new CanvasHandler()},
};

  public virtual void Handle(Order order){
    var handler = handlers[order.OrderType];
    handler.Handle(order);
  } 
}

public class PhotoHandler: OrderHandler{...}
public class CanvasHandler: OrderHandler{...}

这篇关于在C#中路由对象而不使用switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆