I suggest you ...

Property tempate feature [More reusability, readability, DRY principle. Reduce redundant code. Solve default value issue]

I suggest a feature that help us to create similar propeties.
We frequently create properies something similar like this.

public String NVarCharColumn
{
get
{
return _NVarCharColumn;
}
set
{
if (Object.Equals(value, _NVarCharColumn)) return;
_NVarCharColumn = value;
this.OnPropertyChanged();
}
}

I suggest property template feature in C#6.0.
We can declare property template like this.

public property T MyObservableProperty
{
get
{
return field;
}
set
{
if (Object.Equals(value, field)) return;
field = value;
this.OnPropertyChanged();
}
void OnPropertyChanged();
}

field is a keyword that means private field that will be generated automatically by compiler.
T means a type that is used by this property.You can also use Int32,String instead of T.
And the class that use MyObservableProperty must have a method that signature is void OnPropertyChanged().

And we can use it in class like this.
You can assign default value when use property template.

public class Person
{
property MyObservableProperty String Name;
property MyObservableProperty Int32 Age = 0;
property MyObservableProperty Boolean IsMan = true;

pulbic void OnPropertyChanged()
{
//implementation of method
}
}

Above class will be compiled like this.

public class Person
{
private String _Name;
private Int32 _Age = 0;
private Boolean _IsMan = true;

public String Name
{
get
{
return _Name;
}
set
{
if (Object.Equals(value, _Name)) return;
_Name = value;
this.OnPropertyChanged();
}
}
public Int32 Age
{
get
{
return _Age;
}
set
{
if (Object.Equals(value, _Age)) return;
_Age = value;
this.OnPropertyChanged();
}
}
public Boolean IsMan
{
get
{
return _IsMan;
}
set
{
if (Object.Equals(value, _IsMan)) return;
_IsMan = value;
this.OnPropertyChanged();
}
}
pulbic void OnPropertyChanged()
{
//implementation of method
}
}
-------------------------------------------

This feature provide us below advantage
1.More reusability as component
2.More DRY principle
3.Solve default value of automatic property
4.More readability
5.Reduce redundant code
6.Protect from accessing private field instead of property by mistake

-------------------------------------------
1.More reusability as component

You can declare property template like this.

public property Int32 MyInt32PositiveValueProperty
{
get
{
return field;
}
set
{
if (value < 0) throw new ArgumentException();
field = value;
}
}

And use it every where.

public class Person
{
property MyInt32PositiveValueProperty Age = 0;
}
public class BookSales
{
property MyInt32PositiveValueProperty Price = 0;
property MyInt32PositiveValueProperty Count = 0;
}

When T is determined(as Int32 in this case), you don't have to declare it when using in class.

-------------------------------------------
2.More DRY principle

Think about a case that you sell some item and price must be greater than 1000$.
I declare

public property Int32 MyItemPriceProperty
{
get
{
return field;
}
set
{
if (value < 1000) throw new ArgumentException();
field = value;
}
}

Business rule is changed that price must be greater than 2000$.
We only change the code inside property template.
Property template provide more DRY principle.

-------------------------------------------
3.Solve default value of automatic property

This feature solve the default value issue of automatic property.
We can assign default value when using property template in class like this.

public class Person
{
property MyInt32PositiveValueProperty Age = 0;
}

-------------------------------------------
4.More readability
You can declare property template with your own name that represent about property feature.
So, this feature add more readability like this.

public property Int32 ItemPriceMustBeGreaterThan1200
{
get
{
return field;
}
set
{
if (value < 1200) throw new ArgumentException();
field = value;
}
}
public class BookSales
{
property ItemPriceMustBeGreaterThan1200 Price = 1200;
}

Note that you can assign 0 as default value because above code will be compiled as
public class BookSales
{
private Int32 _Price = 0;
public Int32 Price
{
get
{
return _Price;
}
set
{
if (value < 1200) throw new ArgumentException();
_Price = value;
}
}
}

It would be more better if we can use operator against generic T.
Something like this.
public property T ItemPriceMustBeGreaterThan1200
where T : number
{
get
{
return field;
}
set
{
if (value < 1200) throw new ArgumentException();
field = value;
}
}
But we can't above sample in current version .NET framework.

-------------------------------------------
5.Reduce redundant code
As you can see in above sample, we can reduce redundant code block from our source code.

-------------------------------------------
6.Protect from accessing private field instead of property by mistake
There are no private field declaration because we use field keyword.
So, we never access private field by mistake.

-------------------------------------------

I posted it here, but I think visualstudio.uservoice.com is more proper to discussion.
http://social.msdn.microsoft.com/Forums/vstudio/en-US/bb72ff20-80c4-4939-be1d-7a542f6d0e97/property-template-feature-c60-new-feature-request?forum=csharpgeneral
So, I replace my post to here.

I get feedback that this would be a alternative way.
http://houseofbilz.com/archives/2010/05/08/adventures-in-mvvm-my-viewmodel-base/#VMB.2
But I think my idea is better because of below reason.

1.Property template don't have to be inheritance from ViewModelBase
2.Less code to achieve same feature

houseofbilz.com idea
public string Text
{
get { return Get(() => Text, "This is the default value"); }
set { Set(() => Text, value);}
}
Property template
property MyTextProperty Text = "This is the default value";

-------------------------------------------

I greatly appreciate all feedback about this feature.

regards.

6 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Higty shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    Thank you for your suggestion! While we aim to respond to every suggestion, we are closing older ones that don’t have enough votes so newer ones from you can move to the top. If this suggestion is still important to you, feel free to open it again.

    0 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      Signed in as (Sign out)
      Submitting...

      Feedback and Knowledge Base