Property instantiated by Castle being recorded in Database as Proxy

Asked

Viewed 57 times

2

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

When the property is string, int, datetime, etc., it is saved to the database

Name:Valor

when the class property is a class for example:

public Ramal Ramal { get; set; } 

is recorded at the bank Castle.Proxies.RamalProxy. I would like to know how I can solve this (not record 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 believe this is not the case.)

  • 2

    Give an example of what you are trying to do. I don’t think it makes sense what you want, but maybe it’s just a matter of terminology.

  • 1

    The comments didn’t help anything, it’s all too loose, no context. It still doesn’t seem to make sense to want to know this, but show us what you do, editing the question and putting something more concrete that demonstrates the need. Maybe the solution is something else. Or you don’t even need it. So you can avoid the question being closed because it’s not clear.

  • A lot of people have shown interest in helping you. Ask yourself this question.

  • 1

    You said you are using Castle. The question should be on how to resolve a property that is being read as Proxy.

  • 1

    We now need the code you use to serialize the element for Log. It may be that you need to unclog the object inside the proxy.

1 answer

2

You can use the Tostring

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

So when the property is your class instead of a . net value, you implement in your class the Tostring returning a value that best represents the object.

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

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

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

If you call the method Tostring explicitly, remember to check first if the property value is different from null so you don’t have a Nullreferenceexception.

  • great, it worked, but I would have to do it in all my classes. The goal would be the most generic possible. the Log component is a DLL. would be something like a foreach in the classes I get from the 'Getupdateloglist method'

  • You’d have to do it in every class. If you have control over all of them is easy - it will take a short time if there are tens and a long time if there are thousands. But it’s easy.

  • 1

    If you do not have control over all classes, you can try to deduce which is the most important property. The "most important" criterion could be for example the first property of a "primitive" type other than the Id of the object. This worked for me once but I was dealing with table metadata instead of classes - the first column of the table that wasn’t Id or standard key was considered the most important column. The problem is that you can get the columns of a table in the order they were created but at first you can’t do it using Reflection.

Browser other questions tagged

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