Is there any way to run an event whenever the SqlConnection.Update method of Dapper.Contrib.Extensions runs in an entity?

3

I'm using Dapper a little time and so I have little experience with it. I would like to know if there is any way to detect changes in a specific field.

I need this because in a given table, every time a field is updated, I need to update another field, concatenating with ".JPG".

For example

using Dapper.Contrib.Extensions;

// ...

var remessa = conn.Get<Remessa>(1);

remessa.Campo1 = "5000";

// remessa.Campo2 = remessa.Campo1 + ".jpg";

conn.Update(remessa);

In the example above, I need the commented line for Campo2 to run automatically, every time I run SqlConnection.Update (that is, I do not want to be repeating this snippet in my application). Can you do this with Dapper?

    
asked by anonymous 21.03.2018 / 13:45

1 answer

3

Dapper does not have this feature to make this change, so do directly in the class property, every time you change the Campo1 property by the code it also changes Campo2 , example :

public class Exemplo 
{
    private string _campo1;
    public string Campo1 
    {
        get 
        {
            return _campo1;
        }
        set 
        {
            _campo1 = value;
            Campo2 = value + ".JPG"; // mudança a partir do Campo1 para Campo2
        }
    }
    public string Campo2 { get; set; }
}

in your use:

Exemplo ex = new Exemplo { Campo1 = "0001" };
System.Console.WriteLine(ex.Campo2);

That's the way it is, since% w /% to change fields requires that their properties have been modified. This is the first example, in the Dapper property, you may not have to Campo2 , because it is a data generated by another property ( set; ) example:

public string Campo2 { get; private set; }

Example OnLine Ideone

    
21.03.2018 / 13:56