我在哪里把我的公用事业类在ASP.NET应用程序MVC3? [英] Where shall I put my utilities classes in a ASP.NET MVC3 application?

查看:180
本文介绍了我在哪里把我的公用事业类在ASP.NET应用程序MVC3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发C#和剃刀在ASP.NET MVC3 Web应用程序。

I am developing a web application in ASP.NET MVC3 with C# and Razor.

我需要创建一个工具类这里我把函数字符串转换成日期(年,月,日,等...)。

I need to create an utility class where I put functions to convert string into dates(years, months, days, etc...).

在ASP.NET Web窗体我用来放置这种类的 APP_ code里面文件夹中。在MVC中不存在这样的文件夹,我不认为工具类既不属于为模式也没有助手(一个文件夹,我创建放我的HTML辅助扩展)。

In ASP.NET Web Forms I used to place this kind of classes inside the App_Code folder. In MVC there is not such folder and I don't think utility classes belong neither to Models nor to Helpers(a folder I created to put my extensions on HTML Helpers).

我读到是将实用工具类中的不同的装配一个很好的做法。我想不同的项目应该做的工作,但我会创造什么样的项目?一个普通的类库项目似乎是最合理的选择我。

I read that is a good practice to place the utility classes in a different assembly. I guess a different project should do the job but what kind of project shall I create? A plain Class Library project seem the most logical choice to me.

然而,在我来说,我只是需要把一个特定的类与几个方法,所以,如果我们忽略重复使用性,是不是更多的逻辑把工具类的地方在我MVC3 Web应用程序?

However in my case I just need to put one single class with several methods so, if we ignore re-usability, isn't it more logic to put the utility class somewhere in my MVC3 Web application?

推荐答案

您不应该有工具类。将它们转换为扩展方法是有所好转。查看模型甚至更好。

You should not have utility classes. Convert them to extension methods which is somewhat better. View models are even better.

我通常会创建一个名为HtmlHelpers或基础设施的管道。

I usually create a folder called "HtmlHelpers" or "Infrastructure" for plumbing.

一个通用的文件夹就像垃圾桶恕我直言。你把所有的垃圾在里面。

A "Common" folder is like the trashcan imho. You put all the junk in it.

我将其放置在用于日期时间的扩展方法(放置在被放置在一个命名空间称为基础结构的一类称为DateTimeExtensions)。

I would place it in an extension method for DateTime (placed in a class called DateTimeExtensions which is placed in a namespace called Infrastructure).

和我会在内部视图模型生成视图模型(控制器)时,使用它,或者。

And I would use it internally in the view model or when generating the view model (in the controller).

至于哪个项目,它其实并不重要。最重要的是,你有小班特定任务(或责任)。

As for which project, it doesn't really matter. What's important is that you got small classes with specific tasks (or responsibilities).

有些人认为,你应该有不同的职责几个类库。我不这样做。我创建一个UI项目和业务逻辑的一个项目。然而,我确保类实现一个或多个接口,以便能够稍后重构应用程序。 KISS也应适用于项目的结构,而不仅仅是code在其中。

Some argue that you should have several class libraries with distinct responsibilities. I don't do that. I create one UI project and one project for the business logic. However, I make sure that the classes implement one or more interfaces to be able to refactor the application later on. KISS should also apply to project structure and not just the code in them.

在换句话说:我会把我的核心助手(业务逻辑的项目)在命名空间中称为基础设施

In other words: I would put my helpers in the Core (business logic project) in a namespace called Infrastructure.

这篇关于我在哪里把我的公用事业类在ASP.NET应用程序MVC3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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