.net c#业务逻辑类的正确接口

Correct interface of a business logic class
2021-03-07
  •  译文(汉语)
  •  原文(英语)

在具有与业务逻辑分离的UI的程序中;我有一个车库,其中包含车辆清单(licensePlate,coniditionOfVehicle)

我想允许Garage用户在特定的给定conditionOfVehicle条件下接收所有licensePlate的列表.

  1. 该方法的返回类型应该是什么?字符串的IList(正是要求所说明的)与Vehicle的IList(允许客户查看汽车的更多属性,从而提高可扩展性)

  2. 我们甚至可以退还车辆吗?这将允许用户更改我们可能不希望他做的事情.(假设客户端可以访问"车辆",因为它正在使用构建器来接收车辆,然后调用Garage.AddVehicle(vehicle)将其添加到车库中)

谢谢

解决过程1

有一个YAGNI(您将需要它)原则指出:

始终在真正需要它们时执行它们,永远不要在仅仅预见到需要它们时就执行它们.

如果要求说"用户需要licensePlates列表",则只需返回用户需要的内容,而不必试图预见他们可能需要的内容.否则,您会浪费时间进行预测,可能会增加问题(按用户更换车辆),会使代码对于用户(他们期望licensePlates)和维护者(他们不知道新要求的来源)都是出乎意料的,更糟糕的是-用户可能不会使用新的"功能".

您试图通过返回整车实例而违反的另一个原则是KISS(保持简单,愚蠢):

如果您在简单的解决方案和复杂的解决方案之间进行选择,那么选择复杂性将是愚蠢的.

返回字符串列表是更简单的解决方案,可以满足要求.无需为系统增加不必要的复杂性.

速聊1:
谢谢你的评论.我同意这一点,但想知道如果以这种方式实施后,客户来找您,并说他还需要知道相关车辆的颜色(因为他想制造一辆汽车状况与其颜色的关系图).您现在将简单地定义为一个新的类型(LicensePlate,Color)还是返回Vehicle?
速聊2:
那要看.通常我会尽量避免向系统添加新的数据类型.但是,如果您正在通过网络(DTO)或其特定的视图模型传输数据,那么通常创建新的数据类型将是更好的解决方案.因此,在这里您应该在通过添加新类型和传输用户不需要的其他数据来增加系统复杂性之间进行选择.
速聊3:
也许我错了,但是我不认为这真的是偏爱的问题.假设我没有通过网络传输数据,我正在寻找一种更好的方式,或者至少在一些指导方针之间做出决定,因为对于每个需要的方法创建一个新的类对我来说都是没有意义的返回一组不同的值
速聊4:
我既不是说偏爱,也不是只在通过网络传输数据时才这样做(视图模型也是一个很好的例子).您应该根据项目需求做出决策,就像在性能和可读性之间进行选择一样.

In a program with separated UI from business logic; I have a Garage, containing a list of Vehicle(licensePlate, coniditionOfVehicle)

I want to allow users of Garage to receive a list of all the licensePlates at a specific given conditionOfVehicle.

  1. what should be the return type of the method? IList of string (exactly what the requirement states) vs IList of Vehicle (allowing for the client to see more properties of the car, thereby improving extensibility)

  2. are we even allowed to return a Vehicle? this would allow the user to change things we might not want him to. (assume that the client has access to "Vehicle", as it is using a Builder to receive a Vehicle, and then calls the Garage.AddVehicle(vehicle) to add it to the garage)

Thanks

Solutions1

There is a YAGNI (You Arent Gonna Need It) principle which states:

Always implement things when you actually need them, never when you just foresee that you need them.

If requirement says 'users need list of licensePlates' then simply return what users need without trying to foresee what they might need. Otherwise you are wasting time on forecasting, you are adding possible problems (changing of vehicles by user), you are making your code unexpected both for users (they expect licensePlates) and for maintainers (they don't know where new requirement come from), and worst thing - new 'feature' might be not used by users.

Another principle which you are trying to violate by returning whole vehicle instance is KISS (Keep It Simple, Stupid):

If you have a choice between a simple solution and a complex one, then opting for complexity would be stupid.

Returning list of strings is more simple solution, which satisfies requirements. There is no need to add needless complexity to your system.

Talk1:
Thanks for your comment. I agree on that point, but would like to know what would you do if after you have implemented it this way, the client came to you and said that he also needs to know the color of the associated cars (because he wants to make a graph of condition of cars vs their colors). would you now make a new type consiting of simply (LicensePlate, Color) or would you return Vehicle?
Talk2:
that depends. Usually I try to avoid adding new data types to system. But if you are transferring data over network (DTO), or its a specific view model, then usually creating new data type will be better solution. So here you should chose between increasing complexity of system by adding new types and transferring additional data which not required by user.
Talk3:
Maybe I'm wrong, but I don't think it's really a matter of preference. assuming I am not transferring data over the network, I am looking for a better way of doing it or at least some guidelines to decide between the two, Because it doesn't make sense to me to create a new class for every method that wants to return a different set of values
Talk4:
I neither said that is matter of preference, nor that you should do it only for transferring data over network (view models are also good example). You should make decisions based on your project needs, as with selecting between performance and readability.
转载于:https://stackoverflow.com/questions/23027953/correct-interface-of-a-business-logic-class

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入