Web API - null assignment definition

I am trying to create a template that can detect when a property has been set to null. Something similar to a class Nullable<T>, but a little more advanced. Let me call him MoreThanNullable<T>. Basically I need to do something different depending on the following three scenarios:

  • The property has never been set.
  • The property was set to null
  • The property is assigned an instance of T.

I created my own class to do this using the “created” property, and it all works in a test script. Code example:

public struct MoreThanNullable<T>
{
    private bool hasValue;
    internal T value;
    public bool Instantiated { get; set; }

    public MoreThanNullable(T value)
    {
        this.value = value;
        this.hasValue = true;
        Instantiated = true;
    }
// ... etc etc...

And the test that passes, as I expect it:

[TestFixture]
public class MoreThanNullableTests
{
    public class AccountViewModel
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public MoreThanNullable<string> Test1 { get; set; }
        public MoreThanNullable<string> Test2 { get; set; }
        public MoreThanNullable<string> Test3 { get; set; }
    }

    [Test]
    public void Tests()
    {
        var myClass = new AccountViewModel();
        Assert.AreEqual(false, myClass.Test1.Instantiated);
        myClass.Test1 = null;
        Assert.AreEqual(true, myClass.Test1.Instantiated);
    }
}

Then, using this same view model, I connect it to POST on my REST service, and I pass the following JSON:

{
    Name:"test",
    Test1:null,
    Test2:"test"
}

... . Test1, Test3 ! , , Test3 , Test1, , . , , REST. (: null)

- ? -API?

** **

, , , , Nullable<T> , :

public static implicit operator MoreThanNullable<T>(T value)
{
    return new MoreThanNullable<T>(value);
}

, , ...

+4
2

? JSON

{
    Name:"test",
    Test1:null,
    Test2:"test"
}

Test1 end Test2 - , , , , , . JSON . ,

{
    Name:"test",
    Test1:{Instantiated:false, value: null},
    Test2:{Instantiated:false, value: "test"},
}

. , , , .

, , , ? , -api json OnActionExecuting.

2

, JSON.NET newtonsoft, JSON-. web api, .

public sealed class MoreThanNullableConverter : Newtonsoft.Json.JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType.IsGenericType && objectType.GetGenericTypeDefinition() == typeof (MoreThanNullable<>);
    }
    public override bool CanWrite { get { return false; } }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        // return a new MoreThanNullable instance with value
        return Activator.CreateInstance(objectType, reader.Value);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

webapiconfig.cs

var formatters = GlobalConfiguration.Configuration.Formatters;
var jsonFormatter = formatters.JsonFormatter;
var settings = jsonFormatter.SerializerSettings;
jsonFormatter.SerializerSettings.Converters.Add(new MoreThanNullableConverter());
+2

, .NET.

WCF, , json deserialization

https://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute(v=vs.110).aspx

.NET , , ,

[OnDeserialized()]
internal void OnDeserializedMethod(StreamingContext context) 
{
   // ... logic here after deserialization
}

. hasValue null . . . hasValue , , , , ? , , hasvalue false, instatiated true?

, , - . - , ...

+1

All Articles