2
I need to have stored in the class itself a list with (name and value) of the changed properties. However, I don’t know if the form I am using is feasible.
I have the class Employee below:
public class Funcionario
{
public int IdFuncionario { get; set; }
public string Nome { get; set; }
public string Sobrenome { get; set; }
public string Setor { get; set; }
public DateTime DataEntrada { get; set; }
}
Create a base class to be able to identify the change and store it:
public abstract class BaseLista
{
public readonly Dictionary<string, object> Dictionary = new Dictionary<string, object>();
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
{
if (Equals(storage, value))
{
return false;
}
storage = value;
if (propertyName == null) return true;
if (!Dictionary.ContainsKey(propertyName))
{
Dictionary.Add(propertyName, value);
}
else
{
Dictionary[propertyName] = value;
}
return true;
}
}
And I changed the staff class this way:
public class Funcionario : BaseLista
{
private int _idFuncionario;
private string _nome;
private string _sobrenome;
private string _setor;
private DateTime _dataEntrada;
public int IdFuncionario
{
get { return _idFuncionario; }
set { SetProperty(ref _idFuncionario, value);}
}
public string Nome
{
get { return _nome; }
set { SetProperty(ref _nome, value); }
}
public string Sobrenome
{
get { return _sobrenome; }
set { SetProperty(ref _sobrenome, value); }
}
public string Setor
{
get { return _setor; }
set { SetProperty(ref _setor, value); }
}
public DateTime DataEntrada
{
get { return _dataEntrada; }
set { SetProperty(ref _dataEntrada, value); }
}
}
Below the test performed:
[TestClass]
public class Testes
{
[TestMethod]
public void TesteLista()
{
var funcionario = new Funcionario
{
Nome = "Paulo",
Sobrenome = "Balbino",
Setor = "Desenvolvimento"
};
var listaPropriedadesAlteradas = funcionario.Dictionary;
}
}
Is there any better way to do this? I need this list of properties changed to assemble a generic update statement, I don’t want to pass all the entity fields, because I have cases that I won’t have all.
Always prefer to put all the code in text. You can put a screenshot to assist but ensure that all code is available even for someone to use to test. This question seems to me to be a code review more than anything else, right? If you give me time, I’ll try to answer.
– Maniero
I put the image to show the result, but I already edited and entered the test code. So, it might be a code review, but I was wondering if there’s a better way to do this...!
– Paulo Balbino