简体   繁体   中英

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. 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). 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). Public properties should generally be declared with UpperCamelCase notation;

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

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';

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

The final consideration is this; does IContentsService belong in 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.

HTH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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