美化C#语法 [英] Beautifying C# Syntax

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

问题描述

在我的C#/ ASP.NET项目我有一个具有某些类别的行为的对象。每一个行为类是依赖于物理根对象上,但使code读更好,我想明确区分的类别。我想这怎么看我的实现比较别人可能会写来解决同样的问题会很有趣。

In my C#/ASP.NET project I have an object which has certain categories of behavior. Each behavior category is physically dependent on a root object but to make the code read better i want to clearly differentiate the categories. I think it would be interesting to see how my implementation compares to what others might write to solve the same problem.

在下面的例子中,我有产生的URL在网站上不同地方的类。该网站有它有它自己的一套应该从不同的URL来访问,如网页链接店面。

In the example below i have a class which generates URLs to different places on the website. The website has a storefront which has it's own set of links which should be accessed differently from the urls such as the homepage.

public class WebsiteURLs
{
   // One category of urls on the website is the store.
   public class StoreURLs
   {
     private WebsiteURLs _website;
     public class StoreURLs(WebsiteURLs website)
     {
       _website = website;
     }
     public string BestSellers
     { 
        get { return _website.ResolveURL("~/store/bestSellers.html"); }
     }
   }
   private StoreURLs _store;
   public StoreURLs Store // property for generating store urls
   {
     get
     {
        if (_store == null ) {
          _store = new StoreURLs(this);
        }
        return _store;
     }
   }

   public string Homepage
   {
       get { return ResolveURL("~/default.aspx"); }
   }

   // .. Other Categories Here
   protected string ResolveURL(string url)
   {
     return HttpContext.Current.Response.ApplyAppPathModifier(url);
   }
}

使用这个code将类似于以下

Using this code would look similar to the following

WebsiteURLs website;
Console.WriteLine(website.Store.BestSellers);

罗伯特:

这个例子本身只有一个场景,我发现自己想更明确地组织功能。你有没有发现自己使用的是preFIX各地相关的方法。 String.TrimStart(),String.TrimEnd(),String.Trim()是来自C#的框架介意,例如,

The example itself is only one scenario where I have found myself trying to more explicitly organize the functionality. Have you ever found yourself using a prefix around related methods. String.TrimStart(), String.TrimEnd(), String.Trim() is an example that comes to mind from the C# framework.

我试图orgnaize的code以上(从可读性的角度来看)从嵌套类的inabilty访问外部类的成员受到影响。有参与构建内部类,因为我必须传递给StoreURLs类的构造函数的WebsiteURLs实例的引用额外的工作,这几乎是违反了C#编程实践,因为这种封闭的行为是不是在嵌套类的行为语言。我很好奇什么C#code其他人在存在大量相关功能的情况下使用(想想几十或几百的方法)。 (注:以上code是用Java写到哪嵌套类都可以访问其外部类的成员更流畅)。

My attempt to orgnaize the code above (from a readability standpoint) suffers from the inabilty of nested classes to access outer classes members. There is extra work involved in constructing the inner class because i must pass a reference of the WebsiteURLs instance to the constructor of the StoreURLs class, it is almost a violation of C# coding practices because this closure behavior is not the behavior of nested classes within the language. I'm curious what C# code others would use in situations where there exists lots of related functionality (think tens or hundreds of methods). (Note: The code above is much more fluid to write in Java where nested classes do have access to their outer classes members).

推荐答案

我的,当我看到你的例子是认为你的东西结合相对静态的即时反应 - 一组类型 - 的东西相对动态的 - 一堆属于一个网站的网址。那味道我错了。这样做紧密结合您的code到网站的结构和功能。任何更改任何URL的网站,你结合你的力量,重建和重新部署软件的任何地方。

My immediate response when I see your example is to think that you're binding something relatively static - a set of types - to something relatively dynamic - a bunch of URLs belonging to a web site. That smells wrong to me. Doing this tightly couples your code to the structure and function of the web site. Any change to any URL anywhere in the web site that you're binding to forces you to rebuild and redeploy your software.

所以,如果你要做到这一点,你必须有一个pretty很好的理由。什么是你走出这件事?在网页的等级使用类型检查(与智能感知,可能)的能力。你付出一个非常显著的成本得到这个。

So if you're going to do that, you have to have a pretty good reason. What are you getting out of this? The ability to use type-checking (and IntelliSense, probably) on a hierarchy of web pages. You're paying a really significant cost to get this.

是否值得呢?是这样的:

Is it worth it? Is this:

url = website.Store.BestSellers;

比这真的好多了:

really so much better than this:

url = website.GetUrl("Store.BestSellers");

这是值得你要去的工作量将不得不把在短短实现呢?

that it's worth the amount of work you're going to have to put in just to achieve it?

有一定情况下使用这个问题的答案可能是是。但我不会花这个设计一分钟不被肯定,我知道这一点。

There are certainly circumstances under which the answer to that question could be "yes." But I wouldn't spend another minute on this design without being sure I knew it.

这篇关于美化C#语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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