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

Asked

Viewed 39 times

3

I’ve been using Dapper for a short time and so I have little experience with it. I wonder 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 above example, I need the commented line referring to Campo2 run automatically, every time I run SqlConnection.Update (that is, I don’t want to keep repeating this excerpt in my application). There is a way to do this with Dapper?

  • 2

    Shouldn’t this field/property then be "calculated"? Do you really need this "real" field if its value is really always dependent on the value of the other? I believe that in the definition of 'remittance', declare it as public string Campo2 { get { return Campo1 + ".jpg"; } } would be enough. It does not even need to exist in the bank.

1 answer

3


The Dapper does not have this feature to make this change, so do it directly in the class property, every time you change the property Campo1 by the code is also amended 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 its use:

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

That’s right, since the Dapper to change the fields you need your properties to have been modified. This is the first example in the property Campo2, maybe you don’t need to have the set;, because, the same is a data generated by another property (calculado) example:

public string Campo2 { get; private set; }

Ideone Online Example

Browser other questions tagged

You are not signed in. Login or sign up in order to post.