类库项目 VS App_Code - 优点/缺点?
我目前在所有类中使用 App_Code 文件夹,对我来说(目前)它似乎工作得很好。
然而,我一直在考虑切换到解决方案中的类库项目而不是 App_Code 文件夹。谁能告诉我这样做的优点和缺点?
我的一个想法是测试我的网络应用程序。如果我使用类库,每次想要调整/测试时都必须编译它吗?显然在 App_Code 文件夹中我不需要这样做,因为所有类都是在运行时编译的。
I currently use the App_Code folder for all of my classes, and for me (for now) it seems to be working just fine.
I have however been considering making the switch over to a Class Library Project inside my Solution instead of the App_Code folder. Can anyone tell me the pros and cons of doing this?
One thought I had was with regards to testing my web app. If I use a Class Library, do I have to compile it every time I want to tweak/test? Obviously in the App_Code folder I don't have to since all of the Classes compile at runtime.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用类库。原因很简单。
您想要从 UI 中删除业务或应用程序逻辑。 App_Code 文件夹仅用于处理 UI 的类。
分离的原因是为了创建层级。这意味着今天您有一个网站,明天您可能会被要求制作一个 Windows 程序或一个新网站。如果您必须更改用户界面,您将复制并粘贴代码。这意味着您现在有 2 个或更多位置来维护代码。这将导致代码质量较差,在两个或更多地方修复相同的错误。
如果您将代码放入库中。您创建一个新的 UI,然后引用该库。您现在只使用一组代码。
You should use a class library. The reasons are simple.
You want to remove your business or application logic from the UI. The App_Code folder is for classes that deal with the UI only.
The reason for the separation is to create tiers. Meaning today you have a web site, tommorow you may be asked to make a windows program, or a new web site. If you have to change your UI you are going to be copying and pasting the code. That means you have now 2 or more places to maintain the code. This will lead to poorer code quality, fixing the same bug in 2 or more places.
If you place your code in a library. You create a new UI and then just reference the library. You are only now using 1 set of code.
吉米·博加德(Automapper 的作者)写了一篇出色的 关于他如何构建代码的文章,这可能有助于确认@David 的答案。
Jimmy Bogard (author of Automapper) has written an excellent article on how he structures his code, which may assist in confirming @David's answer.