I have a bunch of business class with autoproperties :
public class A {
public int Id { get; set; }
public string Title { get; set;}
}
Because the application evolves, there is a new requirement to enable tracking the changes of the properties, in order to send to the backing store only changed data.
In order to reach this goal, I have to convert ALL properties to field + property like this :
public class A {
private int m_Id;
public int Id {
get { return m_Id; }
set {
if(m_Id != value){
SetChanged("Id");
m_Id = value;
}
}
}
private string m_Title;
public string Title
{
get { return m_Title; }
set {
if(m_Title != value){
SetChanged("Title");
m_Title = value;
}
}
}
protecte void SetChanged(string propertyName) {
// Not important here
}
}
Is there a way to quickly refactor my code to avoid having to manually change the properties ?
There's no way in the IDE to do this, but if you need to replace all X properties, I would write a short console application to do it.
The process would be:
Iterate over all files in directory matching *.cs
Foreach file, regex find and replace old property for new property syntax
Using regex to match is very powerful. Regex can be used in VS2010 to do a find/replace operation. If you try finding this (with regex enabled)
{(public|private|internal|protected)}:b{[a-zA-Z0-9]+}
:b{[a-zA-Z0-9]+}:b\{ get; set; \}
It will match properties like this
public Type Foo { get; set; }
In your console application find all lines of code that match the above, then start splitting them up into Modifier, Type, Property Name and finally replacing the whole block with something like this
// PS: this is pseudocode ;-) or could be your new property template
private [Type] m_[PropertyName].ToPascaleCase
public [Type] PropertyName
{
get { return m_[PropertyName].ToPascaleCase; }
set
{
if(m_[PropertyName].ToPascaleCase != value){
SetChanged([PropertyName]);
m_[PropertyName].ToPascaleCase = value;
}
}
}
Finally I would advocate taking a backup of your code or running this test offline and testing before checking in!!
You can always just create a generic method that will do the assignment and call SetChange
void SetChangeIfNeeded<T>(ref T field, T value, string propertyName)
{
if (!EqualityComparer<T>.Default.Equals(field, value))
{
field = value;
SetChanged(property);
}
}
You would still need to have a private back field. Your class would look something like:
public class A {
private int m_id
public int Id
{
get { return m_id };
set { SetChangeIfNeeded<int>(ref m_id, value, "Id"); }
}
}
ReSharper can do this, but wouldn't modify setter.
public string Title {
get { return m_title; }
set { m_title = value; }
}
There is probably no direct way of doing this with refraction. If this was my problem. I would make code to generate this:
public string MakePropertyBigger(string varName, string propName, string dataType)
{
string output = "";
output += string.Format("private {0} {1};", dataType, varName) + Environment.NewLine;
output += string.Format("public {0} {1}", dataType, propName) + Environment.NewLine;
output += "{" + Environment.NewLine;
output += string.Format("get { return {0}; }", varName) + Environment.NewLine;
output += string.Format("set { if({0} != value){ SetChanged(\"{1}\");", varName, propName) + Environment.NewLine;
output += string.Format("{0} = value; }", varName) + Environment.NewLine;
output + "}" + Environment.NewLine + "}";
Now just plug this in and chug it out.
Related
So, i have a method
public void AddToSearch(List<FullName> fullNames)
{
foreach (var fullName in fullNames)
{
if (fullName.Surname != null)
_sb.Append(fullName.Surname.Trim() + " ");
if (fullName.Name != null)
_sb.Append(fullName.Name.Trim() + " ");
if (fullName.Patronymic != null)
_sb.Append(fullName.Patronymic.Trim());
fullNamesList.Add(_sb.ToString().TrimEnd());
_sb.Clear();
}
it takes a list of FullName and by using StringBuilder instance converts each element into a string(which format is "$Surname $Name $Patronymic"). At the end i put the result into my list. The Question is - how can i optimize all of that "Trim" stuff. It bothers me that i use it in multiple occassions and i am pretty sure it effects the time.
how can i optimize all of that "Trim" stuff
Very simple, simply don't call Trim() on those strings. What spaces are you worried about? Who's entering those values in your business objects? Because short of solar flares randomly flipping bits enough to append spaces to your strings, you're in full control from beginning to end, so simply don't add the spaces.
You also don't need the two string builders, just insert in your main one. There's no need for yet another Trim() here either, because simply decrementing the Length property of your string builder is a constant operation (it literally decrements one integer with guaranteed no extra allocations).
the strings normalization process should be done in the data layer (in application or database) for stored strings. While dynamic strings such as user input, needs to be normalized as soon as you get them to prepare them for the next task.
For your current code, you can modify the FullName class, adjust the setters to trim the value before it's been stored, and override the ToString to return the full name.
Example :
public class FullName
{
public string Name
{
get => Name;
set => Name = value?.Trim();
}
public string Surname
{
get => Surname;
set => Surname = value?.Trim();
}
public string Patronymic
{
get => Patronymic;
set => Patronymic = value?.Trim();
}
public override string ToString()
{
return $"{GetValueOrEmpty(Surname)}{GetValueOrEmpty(Name)}{GetValueOrEmpty(Patronymic, false)}";
}
private string GetValueOrEmpty(string name, bool addSpaceAfter = true)
{
if(!string.IsNullOrWhiteSpace(name))
{
return name + (addSpaceAfter ? " " : string.Empty);
}
return string.Empty;
}
}
Then, you can do this :
fullNamesList.AddRange(fullNames.Select(x=> x.ToString()));
UPDATE :
Thanks to #olivier-jacot-descombes, the above code is missing the use of backing fields, which will avoid causing overflow exception by the properties infinite recursions. The following adjustments will do the trick.
public class FullName
{
private string _name;
private string _surname;
private string _patronymic;
public string Name
{
get => _name;
set => _name = value?.Trim();
}
public string Surname
{
get => _surname;
set => _surname = value?.Trim();
}
public string Patronymic
{
get => _patronymic;
set => _patronymic = value?.Trim();
}
public override string ToString()
{
return $"{GetValueOrEmpty(Surname)}{GetValueOrEmpty(Name)}{GetValueOrEmpty(Patronymic, false)}";
}
private string GetValueOrEmpty(string name, bool addSpaceAfter = true)
{
if(!string.IsNullOrWhiteSpace(name))
{
return name + (addSpaceAfter ? " " : string.Empty);
}
return string.Empty;
}
}
Try and extension something like this.
public static class Helper
{
public static StringBuilder AppendValue(this StringBuilder builder,string value)
{
if(!string.IsNullOrEmpty(value))
{
builder.Append(value.Trim());
return builder;
}
}
}
call as follows:
sb.AppendValue(fullName.Name);
sb.AppendValue(fullName.Surname);
...
You will get the StringBuilder back with the value if it is not empty otherwise nothing will be added to it.
public struct volt_struct
{
public string volt1;
public string volt2;
public string volt2;
public string volt3;
}
private class Injection_class
{
public volt_struct stru1;
public volt_struct stru2;
public volt_struct stru3;
public volt_struct stru4;
public volt_struct stru5;
public volt_struct stru6;
}
public void main()
{
Injection_class Time = new Injection_class();
//Here is code that fills Time with Time values as string type
string s="";
FieldInfo[] fi_inner = Time.stru1.GetType().GetFields();
FieldInfo[] fi_outer = Time.GetType().GetFields();
// This part is wrong, but shows what I want to achive.
foreach(FieldInfo field_outer in fi_outer)
{
foreach(FieldInfo field_inner in fi_inner)
{
s = string.concat(s+field_outer.field_inner.GetValue(Time) + ";");
}
}
}
I want to concatenate the strings stored inside Time into the string s using reflection. Later on I have to modify the class and struct and I don't want to adjust the concatenating code.
I got the results I want with using a foreach loop for each struct inside the class.
foreach (FieldInfo field in fi_inner)
{
s = string.Concat(s + field.GetValue(Time.stru1) + ";");
//field.SetValue(Time, "not measured"); //reset value
}
foreach (FieldInfo field in fi_inner)
{
s = string.Concat(s + field.GetValue(Time.stru2) + ";");
//field.SetValue(Time, "not measured"); //reset value
}
//and so one for each other struct
I want to achieve it like in the first example I gave.
Is this possible?
You can do this much easier without reflection. Change the struct and class like this:
public struct volt
{
private string[] _volts = new string[4];
public string[] volts {get {return _volts;} }
public string volt1 {
get {return _volts[0];}
set {_volts[0] = value;}
}
public string volt2 {
get {return _volts[1];}
set {_volts[1] = value;}
}
public string volt3 {
get {return _volts[2];}
set {_volts[2] = value;}
}
public string volt4 {
get {return _volts[3];}
set {_volts[3] = value;}
}
}
private class Injection
{
private _volt[] = new volt[5];
public volt[] {get {return _volt;} }
public volt stru1 {
get {return _volt[0];}
set {_volt[0] = value;}
}
public volt stru2 {
get {return _volt[1];}
set {_volt[1] = value;}
}
public volt stru3 {
get {return _volt[2];}
set {_volt[2] = value;}
}
public volt stru4 {
get {return _volt[3];}
set {_volt[3] = value;}
}
public volt stru5 {
get {return _volt[4];}
set {_volt[4] = value;}
}
public volt stru6 {
get {return _volt[5];}
set {_volt[5] = value;}
}
}
And now you have nice, convenient arrays you can use:
public void main()
{
Injection Time = new Injection();
//Here is code that fills Time with Time values as string type
string result = "";
foreach(volt v in Time.volt)
{
foreach(string s in v.volts)
{
result += s + ";"
}
}
Console.WriteLine(result);
}
FieldInfo properties that operate on 'field_inner' need a referece to an object of type 'volt_struct' so Time won't work here. You need to do a GetValue on 'field_outer' first, kind of like this:
foreach(FieldInfo field_outer in fi_outer)
{
var outer_object = field_outer.GetValue(Time);
if (outer_object == null) throw someexception;
foreach (FieldInfo field_inner in fi_inner)
{
s = string.concat(s+field_inner.GetValue(outer_object) + ";");
}
}
If you want to vary the parent and child types, you could pass them in as System.Type parameters, or you could write a Generic function with two type parameters. You might also move the 'fi_inner =' into the outer loop and do fi_inner = outer_object.GetType().GetFields(). That would concatenate strings on any child object whatever the type.
I am trying to learn C# and I am up to an example that uses a boolean. For the life of me I cant figure out why the program isnt noticing that I am trying to pass a value of true to the boolean. Here is the code in the Form.cs:
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
HappyBirthday birthdayMessage = new HappyBirthday();
string returnedMessage;
birthdayMessage.PresentCount = 5;
birthdayMessage.MyProperty = "Adam";
birthdayMessage.hasParty = true;
returnedMessage = birthdayMessage.MyProperty;
MessageBox.Show(returnedMessage);
}
}
}
Here is the Class that I created:
class HappyBirthday
{
//====================
// CLASS VARIABLES
//====================
private int numberOfPresents;
private string birthdayMessage;
private bool birthdayParty;
//===========================
// DEFAULT CONSTRUCTOR
//===========================
public HappyBirthday()
{
numberOfPresents = 0;
//birthdayParty = false;
}
//===========================
// METHOD
//===========================
private string getMessage(string givenName)
{
string theMessage;
theMessage = "Happy Birthday " + givenName + "\n";
theMessage += "Number of presents = ";
theMessage += numberOfPresents.ToString() + "\n";
if (birthdayParty == true)
{
theMessage += "Hope you enjoy the party!";
}
else
{
theMessage += "No party = sorry!";
}
return theMessage;
}
//================================
// READ AND WRITE PROPERTY
//================================
public string MyProperty
{
get { return birthdayMessage; }
set { birthdayMessage = getMessage(value); }
}
//================================
// WRITE-ONLY PROPERTY
//================================
public int PresentCount
{
set { numberOfPresents = value; }
}
public bool hasParty
{
set { birthdayParty = value; }
}
}
Now I set the initial value to false (even though if my understanding is correct that should be the default value), but when I try to set it = true, the program does not recognize it. Am I supposed to pass a boolean differently then I would a string or int?
You're setting MyProperty before you're setting hasParty. getMessage() is not being called every time MyProperty is polled.
The way MyProperty works is confusing, because the set and get deal with different values (you set the name, and then get the whole message, which is confusing). I'd replace it with a GivenName property and then make the GetMessage() (or expose it as a read-only property Message) public.
Also, you can make your code much simpler by using auto-properties (you can use private gets to keep the write-only behavior, though in the real world write-only properties are very rare, and you should probably just make them public like the sets). And since the default int value is 0, you don't need to specify your default constructor. Here's how the code looks now:
class HappyBirthday
{
public string Message
{
get
{
string theMessage;
theMessage = "Happy Birthday " + GivenName + "\n";
theMessage += "Number of presents = ";
theMessage += PresentCount.ToString() + "\n";
if (HasParty)
{
theMessage += "Hope you enjoy the party!";
}
else
{
theMessage += "No party = sorry!";
}
return theMessage;
}
}
public string GivenName { private get; set; }
public int PresentCount { private get; set; }
public bool HasParty { private get; set; }
}
I've got something like this in my property/accessor method of a constructor for my program.
using System;
namespace BusinessTrips
{
public class Expense
{
private string paymentMethod;
public Expense()
{
}
public Expense(string pmtMthd)
{
paymentMethod = pmtMthd;
}
//This is where things get problematic
public string PaymentMethod
{
get
{
return paymentMethod;
}
set
{
if (string.IsNullOrWhiteSpace(" "))
paymentMethod = "~~unspecified~~";
else paymentMethod = value;
}
}
}
}
When a new attribute is entered, for PaymentMethod, which is null or a space, this clearly does not work. Any ideas?
do you perhaps just need to replace string.IsNullOrWhiteSpace(" ") with string.IsNullOrWhiteSpace(value) ?
From your posted code, you need to call:
this.PaymentMethod = pmtMthd;
instead of
paymentMethod = pmtMthd;
The capital p will use your property instead of the string directly. This is why it's a good idea to use this. when accessing class variables. In this case, it's the capital not the this. that makes the difference, but I'd get into the habit of using this.
Jean-Barnard Pellerin's answer is correct.
But here is the full code, which I tested in LinqPad to show that it works.
public class Foo {
private string _paymentMethod = "~~unspecified~~";
public string PaymentMethod
{
get
{
return _paymentMethod;
}
set
{
if (string.IsNullOrWhiteSpace(value))
_paymentMethod = "~~unspecified~~";
else _paymentMethod = value;
}
}
}
With a main of:
void Main()
{
var f = new Foo();
f.PaymentMethod = "";
Console.WriteLine(f.PaymentMethod);
f.PaymentMethod = " ";
Console.WriteLine(f.PaymentMethod);
f.PaymentMethod = "FooBar";
Console.WriteLine(f.PaymentMethod);
}
Output from console:
~~unspecified~~
~~unspecified~~
FooBar
This might be a stupid one but I'll shoot it out there.
For example let's say I have a model class:
public class PermissionModel
{
public bool AppName_Home_Product_SaveButton_Enabled { get; set; }
public bool AppName_Home_Product_ConfirmButton_Enabled { get; set; }
}
And I have the following list of strings:
"AppName_Home_Product_SaveButton_Enabled_true"
"AppName_Home_Product_SaveButton_Enabled_false"
I want to automatically populate the model properties with true/false without having to use if statements as in the following example:
if (aString.Contains("AppName_Home_Product_SaveButton_Enabled"))
{
PermissionModel.AppName_Home_Product_SaveButton_Enabled = Convert.ToBoolean(AString.Substring(AString.IndexOf("Enabled_") + 8));
}
Any ideas or is this crazy? I just want to avoid a bunch of if statements to populate the model and make it more re-usable.
This can be done via reflection
const string delimiter = "_Enabled";
foreach (string data in aString) {
int index = data.IndexOf(delimiter);
if (index >= 0) {
// Get the name and value out of the data string
string name = data.Substring(0, index + delimiter.Length);
bool value = Convert.ToBoolean(data.Substring(index + delimiter.Length + 1));
// Find the property with the specified name and change the value
PropertyInfo property = GetType().GetProperty(name);
if (property != null) {
property.SetValue(this, value);
}
}
}