如何在C# Web应用程序中改善项目结构

How to improve Project structure in c# web application [closed]
2020-10-18
  •  译文(汉语)
  •  原文(英语)

我正在使用C#Web应用程序,并且我用下面的图像所示的简单项目结构来构建各种应用程序.

如何在C#Web应用程序中改善项目结构

如何在C#Web应用程序中改善项目结构

如何在C#Web应用程序中改善项目结构

基本上,我想了解C#上大型Web应用程序的最佳项目结构吗?还是上面的项目结构(如图所示)是构建Web应用程序的正确方法?

现在我想改善我的项目结构.因此,我应该从哪里开始改进自己,以构建一个大型的Web应用程序.

解决过程1

没有"最佳方法",但是有一些"最糟糕的方法".我看到您正在使用某些"最糟糕的方法".这里有几个:

  1. 不要使用网站"项目".他们不是项目.它们就是特定目录树中的任何东西.我注意到您已经~1.pdf在项目中,还是刚好在同一文件夹中拥有它?改用Web应用程序项目-使用文件->新建项目.

  2. 不要使用App_Code.App_Code用于少数在运行时编译的代码文件.没有其他类型的"项目"在运行时编译代码.这是计算机编程的一种奇怪的破坏.

    至少要为代码的不同部分创建单独的文件夹,这些文件夹不在代码的后面.您可以从一个文件夹开始(可能称为"My_Code",而不是App_Code),并且当您开始在那个文件夹中获取"太多"文件时,请根据文件的功能开始分离文件.每个文件保留一个类,并且不同的类集具有不同的功能集很快就会变得很明显.例如,您将找到一组与数据库访问相关的类.将它们放入"DataAccess"文件夹中.

    更好的是,一旦发现有许多此类文件夹,请将每个文件夹移到它们自己的类库项目中.这样,您不必直接进入复杂的项目结构中-该结构可以随着时间而变化.

    而且,如果您在10年内只使用了3个类My_Code,那么您就节省了自己创建微型类库的浪费.

  3. 您的代码后面可能有太多代码.我看不到它,但是具有前两个症状的项目通常会有第三个症状.背后的代码中应该只包含直接涉及用户界面的代码-例如,它可以调用获取数据的方法以绑定到数据绑定控件,但是获取数据的代码应该在不同的类中,而不是在代码隐藏.App_Code就像我在#2中所说的,这个不同的类不应该在.

  4. 您在使用源代码管理吗?我在项目图标上看不到任何源代码管理图标.使用源代码管理.Visual Studio Online最多可免费使用五个用户,并且允许您使用TFS或Git进行源代码控制,并提供工作项跟踪,自动生成等功能.免费.

速聊1:
请注意,我的意思是从一开始就做出这个答案.所有人都应该随意阐述.OTOH,我也要说我投票结束了这个问题,我认为这不属于Stack Overflow.有点精神分裂症?是的,没有.
速聊2:
我认为不,您应该删除您的投票.感谢回答我的问题
解决过程2

我认为没有真正正确的方法来构建项目.这取决于我们的项目目标和您的实施计划.每个项目都是不同的,并且会有不同的要求.

对于您的特定项目,我会采用最合乎逻辑的方法.是将相似的组件组合在一起,还是将整个子系统组合在一起.

在开始开发之前,您应该仔细分析需求,并相应地计划结构.如果事情还没有解决,您需要进行重组,那么在开始编码之前有一个好的计划可以为您节省很多麻烦.

我建议您阅读一本类似《代码完成》的书,它会为您提供有关如何计划和构建项目的出色技巧.您可以在这里找到它:代码完成

I am working on C# web application and i build various application with simple project structure like image is given below.

How to improve Project structure in c# web application [closed]

How to improve Project structure in c# web application [closed]

How to improve Project structure in c# web application [closed]

Basically i want to know about the best project structure of large web application on C#? or is above project structure(in the image) is right way to build an web application ?

Now i want to improve my project structure. So where should i start improve myself to build an lage web application.

Solutions1

There's no "best way", but there are some "worst ways". I see that you are using some of the "worst ways". Here are a few of them:

  1. Do not use web site "projects". They are not projects. They're just whatever happens to be in a particular directory tree. I notice that you have ~1.pdf in your project, or did you just happen to have it in the same folder? Use Web Application Projects instead - use File->New Project.

  2. Do not use App_Code. App_Code is for a small number of code files which are compiled at runtime. No other kind of "project" has code compiled at runtime. This is a bizarre corruption of computer programming.

    At the very least, create separate folders for the different parts of your code which are not in code-behind. You can start with a single folder (maybe called "My_Code", not App_Code), and when you start to get "too many" files in that one folder, start separating the files based on their function. Keep one class per file, and it will become obvious quickly that different sets of classes have different sets of functions. For instance, you'll find you have a set of classes related to database access. Put those into a "DataAccess" folder.

    Better, once you find that you have many such folders, move each of the folders out into their own class library project. This way, you don't have to jump right into a complex project structure - the structure can evolve over time.

    And if you wind up in ten years with only three classes in My_Code, then you have saved yourself the waste of creating tiny class libraries.

  3. You may have too much code in your code behind. I can't see it, but projects which have the first two symptoms usually have the third. Your codebehind should only have code in it that directly involves user interface - for instance, it can call a method to fetch data to bind to a data bound control, but the code to fetch the data should be in a different class, not in the codebehind. This different class should not be in App_Code, as I say in #2.

  4. Are you using source control? I don't see any source control icons on your project icons. Use source control. Visual Studio Online is free for up to five users, and allows you to use either TFS or Git for source control, as well as providing work item tracking, automated builds and quite a bit more. For free.

Talk1:
Note that I meant to make this answer cw from the start. All should feel free to elaborate. OTOH, I'll also say that I voted to close the question, which I don't think belongs on Stack Overflow. Slightly schizo? Yes and no.
Talk2:
i think no, you should remove your vote. thanks to answer my question
Solutions2

I don't think there is really a correct way to structure a project. It depends on what the goals of the project our and your plans for implementation. Each project is different and have will different requirements.

I would go with the most logical approach for your particular project. Whether that's grouping similar components together or grouping whole sub-systems together.

You should carefully analyze your requirements up front before you even begin development and plan your structure accordingly. Having a good plan before you begin coding will save you a bunch of hassle down-stream if things aren't working out and you need to restructure.

I would suggest reading a book like Code Complete, it'll give you excellent tips on how to plan and structure your projects. You can find it here: Code Complete

转载于:https://stackoverflow.com/questions/30793382/how-to-improve-project-structure-in-c-sharp-web-application

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

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