Pregunta

I am trying to figure out how to check if an object is null before it is invoked. Here is some sample code:

 public class NotifyTemplate<T> where T : class
    {
        public Func<T, string> Value { get; set; }
        public string Key { get; set; }
    }

    public class User
    {
        public int Id { get; set; }
        public string UserName { get; set; }

        public Contact Contact { get; set; }

    }

    public class Contact
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

I want check if any of the objects i am trying to invoke are null before I actually invoke the delegate. I can throw a try catch, but that seems overkill. Here is an example of my usage:

var notifyList = new List<NotifyTemplate<User>>()
            {
                new NotifyTemplate<User>()
                {
                    Value = u => u.Contact.FirstName,
                    Key = "FName"
                },
                new NotifyTemplate<User>()
                {
                    Value = u => u.UserName,
                    Key = "UserName"
                }
            };

            var user = new User()
            {
                Id = 1,
                UserName = "ddivita@dd.com",

            };

            foreach (var notifyTemplate in notifyList)
            {
               //This is where I want to check if the value is null or not before I invoke.


                var name =  notifyTemplate.Value.Invoke(user);
            }

As you can see the Contact object is null and not initialized in the new User declaration. So, inside the foreach statement I want to inspect the Value property and see if the object inside my delegate is null before I invoke it.

The whole point to this is that we are setting up a notification system and to make it easier on the developer, the Func will be used to replace keys inside a notification template.

Here is what teh temaplte might look like:

<html>
   <body>
      Hello There [FName] [LName]. Thank you for signing up using the username: [UserName].

   </body>
</html>

The developer will pass into our notification service what template they want to use and the list of NotifyTemplate

If for some reason the contact information was not persisted correctly and we pull a User object from the database, let's assume the Contact object is null. We need to evaluate that the Contact object is null before we call invoke to get the value.

¿Fue útil?

Solución 2

As Neolisk suggested, I am doing this:

foreach (var notifyTemplate in notifyList)
            {
                var value = string.Empty;
                try
                {
                   value  = notifyTemplate.Value.Invoke(user);
                }
                catch (Exception ex)
                {

                    //log error
                }

                var theVlaue = value;
        }

Otros consejos

You could do this in the lambda:

Value = u => u.Contact != null ? u.Contact.FirstName : string.Empty
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top