简体   繁体   English

当我有基类时,变量的C#命名约定?

[英]C# naming conventions for variables when I have a base class?

I'd like some help with the naming conventions that I have used for my application. 我想要一些有关我的应用程序使用的命名约定的帮助。 Here's what I have: 这是我所拥有的:

public class BaseController : Controller
    {
        public BaseController()
        {
        }

        [Dependency]
        public IContentService _cs { get; set; }
   }


public class ContentsController : BaseController
    {

        public ContentsController(
            IContentService contentService)
        {
            _cs = contentService;
        }

Can someone let me know if I should perhaps choose a better naming convention and also if the way I set public is correct? 有人可以让我知道我是否应该选择一个更好的命名约定,以及我公开的方式是否正确?

Update: 更新:

I only use _cs inside the Contents controller. 我仅在Contents控制器内使用_cs。 Should I have a different access property for this? 我应该为此使用其他访问属性吗? Should it be private or protected in the base controller? 它是私有的还是在基本控制器中受保护的?

Your choice of member variable naming (_cs) is not appropriate for public properties, (though it's really a matter of preference). 您选择的成员变量命名(_cs)不适合公共属性(尽管这实际上是优先选择的问题)。 Use of 'lower camel case' naming, prefixed with an underscore, is usually reserved for private members (though, the .NET recommendation is lower camel case with no prefix). 通常为私有成员保留使用“小写驼峰”命名,并带有下划线作为前缀(尽管.NET建议使用小写驼峰小写, 没有前缀)。 Public properties should generally be declared with UpperCamelCase notation; 公共属性通常应使用UpperCamelCase表示法声明;

public IContentsService ContentsService { get; set;

You can find info on Microsoft recommendations for naming conventions here: http://msdn.microsoft.com/en-us/library/ms229045.aspx 您可以在此处找到有关Microsoft命名约定建议的信息: http : //msdn.microsoft.com/zh-cn/library/ms229045.aspx

As to your base controller class, unless you have a case where a caller would instantiate BaseController, rather than a more specific type (e.gl ContentsController) declare the class as abstract to make the usage clear (ie; that 'BaseController' is not to be created or used directly), and declare the constructor as 'protected'; 至于您的基本控制器类,除非您有调用者将实例化BaseController的情况,否则不使用更具体的类型(例如,ContentsController)将类声明为抽象类以使用法清楚(即,“ BaseController”不是直接创建或使用),并将构造函数声明为“受保护”;

public abstract class BaseController
{
    protected BaseController()
    {
        ...
    }
}

The final consideration is this; 最终的考虑是这样; does IContentsService belong in BaseController? IContentsService是否属于BaseController? From your naming, it would seem that only the ContentsController would know about or use an IContentsService, so probably move that property into the ContentsController class directly. 从您的命名看来,似乎只有ContentsController会知道或使用IContentsService,因此可能将该属性直接移到ContentsController类中。

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM