-1

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.

DDiVita
  • 4,225
  • 5
  • 63
  • 117
  • @Servy, maybe I am missing something, but let's assume I have no idea what the developer is going to pass into other than it is a user object. Value is a Func in this case. How do I test if the User.Contact is null before it is invoked? Maybe I am making this harder than I need to. I am not going to check user.Contact == null, I need to make it more dynamic than that...So something that would be expressed by this: If the property in Value is null do not try to invoke it. So, if I try to get get user.Contact.FirstName and invoke it I'll get a Null Reference exception. – DDiVita Apr 07 '14 at 19:38
  • 2
    To answer your question - you check a delegate for being a `null`, same as any other object. As a side note, it does look over-complicated for what it's doing above. I dont understand why such complexity is required to begin with. If you could elaborate on that... – Victor Zakharov Apr 07 '14 at 19:41
  • @DDiVita Then you check if that value is null before accessing one of its members, just like any other code anywhere. – Servy Apr 07 '14 at 19:47
  • @DDiVita And it doesn't change my comments. If you want some code to be able to get the value of some property if an object isn't null, then check if it's null. It's that simple. – Servy Apr 07 '14 at 19:50
  • I'd say just make a call, get an exception of a missing column on a data table, or another mention of missing member (if you are not using data tables), then take action as appropriate - notify user, silently ignore etc. – Victor Zakharov Apr 07 '14 at 19:54
  • You could try to implement something like AutoMapper's [`MapFrom`](http://stackoverflow.com/a/14886345/781792), but it'd probably be easier to modify your lambdas to include the null checks. – Tim S. Apr 07 '14 at 20:07

2 Answers2

1

You could do this in the lambda:

Value = u => u.Contact != null ? u.Contact.FirstName : string.Empty
Bill
  • 1,431
  • 10
  • 13
  • Yes, this would work, but what do we do inside the foreach in case the expression is written like I have it? – DDiVita Apr 07 '14 at 19:50
  • 1
    @DDiVita You don't do anything. There is nothing that the invoker of a delegate can do if the delegate wants to throw an exception for the data given. – Servy Apr 07 '14 at 19:51
  • @Servy, that was my question? How do I without throwing the exception or is that not possible. I was asking if it was possible to check if the property was null before it is invoked. – DDiVita Apr 07 '14 at 19:55
  • @DDiVita Technically your question asks how to not call a delegate when the delegate is null, not how to not call a delegate when it accesses a null object somewhere within its body. – Servy Apr 07 '14 at 19:57
0

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;
        }
Community
  • 1
  • 1
DDiVita
  • 4,225
  • 5
  • 63
  • 117