Search code examples
c#language-designsyntactic-sugar

Cleaner property declaration


I'm working on some code for binding a Silverlight view to a presenter in the MVP pattern. In this particular case, it's a really long exercise in doing this over and over again:

Model:

public bool MyBoolean
{
    get { return _myThingy.MyBoolean; }
    set { _myThingy.MyBoolean = value; }
}

Presenter:

public bool MyBoolean
{
    get { return _model.MyBoolean; }
    set { _model.MyBoolean = value; }
}

Clearly the presenter could be done away with in this example, but there are a few things that make it worth having both in this case, as not every property is like this, and our coding standard calls for having Model, View, and Presenter for every window/page/user control.

My real question here is, I'd really like to be able to do something like this:

Model:

public bool MyBoolean -> _myThingy.MyBoolean;

Presenter:

public bool MyBoolean -> _model.MyBoolean;

Wherein the -> is the "bind property operator" or some similar name - essentially syntactic sugar for the first bit of code. Is there something like this already? If not, is there a cleaner way to do what I'm doing?


Solution

  • Is there something like this already? If not, is there a cleaner way to do what I'm doing?

    No. In terms of code, what you have is likely the best option. C# does not provide this type of functionality in any feature.

    However, you could use tooling to make this simpler. For example, a Resharper template could easily make creating one of these very quick. The second could be turned into a template that would just require typing something like (assuming "ptprop" as the keyword):

    ptprop{TAB}bool{TAB}MyBoolean{Tab}_model{Enter}
    

    This would simplify your development effort, though the code would be identical...