ASP.NET MVC / C# 2010 最佳实践?
我开始发现自己对代码的整洁性感到非常不安。
为了给你一些背景知识,我从很久以前写的一本 .Net 2.0 书中学习了 C#,之后开始使用 ASP.NET MVC 框架进行一些工作。小应用程序 我开始意识到我并没有达到应有的效率。
对于试图进入 2010 年的 .Net 2.0 开发人员来说,您认为“十大最佳实践”是什么?
像依赖注入(我几乎不知道)之类的东西等等。
谢谢!
丹尼尔
I'm starting to find myself becoming quite uneasy about the cleanliness of my code..
To give you some background i learned C# from a .Net 2.0 book written a long time ago, after starting to use the ASP.NET MVC framework for some small apps i'm starting to realise that i'm not being as efficient as i could be..
What are your like "top ten best practises" for a .Net 2.0 dev trying to get into the year 2010?
Stuff like dependency injection (Which i have pretty much no knowlege of) etc..
Thanks!
Daniel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
进行 MVC 开发时要记住的一个常见短语是保持模型重(或胖),控制器轻(或薄)。将大量逻辑放入控制器中非常容易,并且它们本质上是非常程序化的。但是,理想情况下,业务逻辑应该封装在模型中,而控制器实际上只是事件处理程序(操作方法),它调用模型中的域逻辑来执行任务。
One common phrase to keep in mind when doing MVC development is to keep your models heavy (or fat) and your controllers light (or thin). It's very easy to throw a lot of logic into the controllers and they end up being very procedural in nature. But, ideally, the business logic should be encapsulated in the models and the controllers are really just event handlers (action methods) which call the domain logic in the models to perform a task.
您可以阅读这本针对 ASP.NET MVC IoC 和 TDD 初学者的好书
史蒂文·桑德斯
You can read this great book for beginers in ASP.NET MVC IoC and TDD
Steven Sanders
随着语言的改进,从 .net 2 到 4,您只需使用自动属性即可清理代码。
因此,不要使用以下
private string myVariable;
您可以替换为:
显示模型数据时另一个选项是强类型视图,而不是
ViewData["Something"];
虽然这可以在最近发布的 v3ViewBag.Something
的 RC2 中解决。希望这有帮助
With the improvements of the language, from .net 2 through to 4 you can clean up your code simply by using auto properties.
So instead of using the follow
private string myVariable;
You can replace with:
Another options is strongly typed views when displaying model data instead of
ViewData["Something"];
Although this could be addressed in the case of the recently release RC2 of v3ViewBag.Something
instead.Hope this helps