用C#测试友好代码 [英] Test friendly code in C#

查看:96
本文介绍了用C#测试友好代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#.NET创建一个Windows应用程序。

但我担心软件的架构。

现在,我正在编写所有的逻辑和类中的数据访问代码,表单UI将调用该函数,并为该类设置属性值。

这是正确的方法吗?它是否测试友好?

I am making a windows application using C#.NET.
But I am concerned about the architecture of the software.
Now, I am writing all the logic & data access codes in a class, and Form UI will call the function, and set property values for the class.
Is it the right method ? Is it test friendly ?

推荐答案

我对您的设计的第一个想法是您可能会考虑在表单上添加UI元素/控件来引发事件您的业​​务逻辑将订阅而不是...如果我理解正确...允许UI控制事件在运行时直接设置类中的属性。



Marc Clifton是CodeProject的MVP之一,也是长期的主要撰稿人,他刚刚通过SyncFusion发布了一本免费的电子书,名为:单元测试简洁可以在这里下载:[ ^ ]。我认为阅读/学习本书将真正帮助你在评估滑动一词测试友好方面取得进展。
The first thought I have in reaction to your design is that you might consider having UI elements/Controls on the Form(s) raise Events that your business logic would subscribe to rather than ... if I understand you correctly ... allowing UI control Events at run-time to directly set properties in the class.

Marc Clifton, one of CodeProject's MVP's, and long-time major contributor, has just published a free e-book through SyncFusion titled: "Unit Testing Succinctly" which you can download here: [^]. I think reading/studying this book will really help you get oriented in terms of evaluating the somewhat "slippery" term "test friendly."


引用:

编写所有逻辑&一个类中的数据访问代码,

writing all the logic & data access codes in a class,

我希望你不要想到一个类,而是在业务逻辑和数据访问中为每个概念使用不同的类。至于数据访问,您可以查看存储库模式

创建业务逻辑时,只需考虑稍后将Windows窗体GUI替换为例如:一个Web界面 - 你仍然可以使用这些类,还是需要重新编写它们?这个想法将帮助你正确地做到这一点。

保持你的课程小,并保持所有功能小。一个概念的一个类,一个函数在该类中只做一件事。可以测试这些小物品。保持低依赖性 - 您可能需要模拟某些对象。

I hope you don't think of one class, rather use different classes for each concept in business logic and data access. As for data access, you may look at the repository pattern.
When you create your business logic, just think of later replacing your Windows Forms GUI by e.g. a web interface - can you still use those classes, or do you need to re-write them? That idea will help you do it correctly.
Keep your classes small, and keep all the functions small. One class for one concept, and one function does only one thing within that class. Such small items can be tested. Keep dependencies low - you may need to "mock" some objects.


这篇关于用C#测试友好代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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