I need to have a list with (name and value) of the changed properties stored in the class itself. But I'm not sure what form I'm using is feasible.
I have the Employee class 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 Employee 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:
[TestClass]publicclassTestes{[TestMethod]publicvoidTesteLista(){varfuncionario=newFuncionario{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 mount a generic update statement, I do not want to pass all fields of the entity, as I have cases that I will not have all.