0

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?

Alan2
  • 23,493
  • 79
  • 256
  • 450
  • 1
    Why is `public`? Wouldn't it be ok to just be `protected`, or if not then `internal` – K-ballo May 31 '12 at 03:26
  • 1
    Maybe [this question](http://stackoverflow.com/questions/826821/c-sharp-class-naming-convention-is-it-baseclass-or-classbase-or-abstractclass) could help you to shape your "naming style". :-) – jfoliveira May 31 '12 at 03:27
  • Yes I think public is not correct. Can you explain if protected, private or internal would be best? I am not familiar with internal. – Alan2 May 31 '12 at 03:47

1 Answers1

4

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 (i.e; 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.

RJ Lohan
  • 6,497
  • 3
  • 34
  • 54
  • Now that you mention this I think I am incorrect with calling _cs as public. _cs is only used inside the controller. So should I name this as private or protected? – Alan2 May 31 '12 at 03:45
  • Private if it is used only by the class it is declared in, protected if derived classes also require access to the member. – RJ Lohan May 31 '12 at 03:58