Click here to Skip to main content
Click here to Skip to main content

Using Not Nullable String Wrapper with DTO

, 27 Nov 2012
Rate this:
Please Sign up or sign in to vote.
How to identify if the given string can be null in the client side for a WCF server client application


This tip describes how to make a wrapper around the string reference type to be used in a client server interaction in order to transfer the not nullable information.

Using the Code

Recently, I was developing an application that uses WCF service to send information to the client side from the server. I used DTOs to hold the data. An example one of the DTOs - with the WCF directives - can be seen below:

public class Parameter : DTOBase
    public Guid ParameterID { get; set; }

    public NotNullableString Name { get; set; }

    public string Description { get; set; }

    public NotNullableString Type { get; set; }

    public int? SortingNumber { get; set; }

The idea here was to somehow try to transfer the information of the not nullability from the server side to the client and use this information to perform the client side validation before the data uploaded and saved on the server. Here we have to differentiate.

Let us see the value types!

(Reference of value types in C# )

Fortunately for these types, we have the possibility to define them as nullable types. We can use the nullable generic structure to make them nullable. Generally speaking, we can declare them in code like this:


which basically means:  

System.Nullable<T> where T is a value type.  

(from MSDN)

With this information, we can identify if the given value type can hold null or not and perform the validation. For example, based on the DTO described above, the int? SortingNumber can be assigned to null.

What can we do with the string reference type?

Here, we would also need the information if the given, let's say string field can hold null or not in order to perform a client side validation. For this, we would create a new wrapper object around the string type in order to achieve the required functionality. The naming is a bit confusing but here the intention was to use this class for all the strings that cannot be assigned to null (notice that the string type itself can be assigned to null).

Let us see how this class looks like:

public class NotNullableString
    public string Value { get; set; }
    public bool IsNull()
        if (Value == null) return true; else return false;
    public static NotNullableString FromString(String str)
        return new NotNullableString() { Value = str };
    public override string ToString()
        return this.Value;

I think this code snippet is self explanatory. Maybe you already noticed that the string of this given wrapper can also be assigned to null, but here the interest was to distinguish between the nullable and not nullable version. Of course, additional validation can be added here.

Create an extender method for the string reference types

In order to enhance the work with the new class, I created an extender method for the string type in order to quickly create a NotNullableString object from the given string.

public static class CreateNotNullableStringExtenderMethod
    public static NotNullableString CreateNotNullableString(this String str)
        return new NotNullableString() { Value = str };

Points of Interest

This can be used in case the is nullable field information is required to be sent to the client side.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Hungary Hungary
No Biography provided

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 27 Nov 2012
Article Copyright 2012 by M_Tamas
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid