Property instantiated by Castle being written to Database as Proxy

2

I have a Log system that compares two generic classes and writes the property name and the value of it, in a column of my database.

When the property is of type string, int, datetime, etc., it is written to the database

Name:Valor

When the class property is a class for example:

public Ramal Ramal { get; set; } 

is written to the Castle.Proxies.RamalProxy database. I would like to know how can I fix this (do not write the proxy, but the original class).

 public class Telefone
{
    public string Name { get; set; }
    public Ramal Ramal { get; set; }
}

public class Ramal
{
    public string RamalValue { get; set; }
}

method that separates properties,

 public static List<LogMessage> GetUpdateLogList<T>(T objFrom, T objTo, string[] ignore)
    {
        if (objFrom != null && objTo != null)
        {
            var type = typeof(T);
            var ignoreList = new List<string>(ignore);
            var unequalProperties =
                from pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                where !ignoreList.Contains(pi.Name)
                let propertyName = pi.Name
                let selfValue = type.GetProperty(pi.Name).GetValue(objFrom, null)
                let toValue = type.GetProperty(pi.Name).GetValue(objTo, null)
                where selfValue != toValue && (selfValue == null || !selfValue.Equals(toValue))
                select new LogMessage(propertyName.ToString()
                                        , selfValue == null ? "null" : selfValue.ToString()
                                        , toValue == null ? "null" : toValue.ToString()
                                        );



            List<LogMessage> logsChanged = unequalProperties.ToList();

            return logsChanged;
        }

where objFrom is the Original class and objTo is the modified class (I think this is not the case.)

    
asked by anonymous 05.10.2015 / 18:59

1 answer

2

You can use the ToString

When writing the value of any property in the database you would use the ToString() value method. Depending on how you record in the bank this is already being done.

So when the property is a class of it instead of being a .Net value, you implement ToString in your class by returning a value that best represents the object.

For example, let's say that the property of a Ramal object that best represents it is a Number call. The implementation of ToString would look like this:

class Ramal
{
    public String Numero { get; set; }

    public override string ToString()
    {
        return Numero;
    }
}

If you are calling the ToString method explicitly, be sure to first check if the property value is different from null so you do not have a NullReferenceException .

    
05.10.2015 / 19:17