Static null returns processing method

So, I created a provider (some of them actually), and I understand that there is a bit of a template in my logic. This repeats itself, and I believe I can remove many lines of code if I can just create this extension method: D

So basically, something like this:

// Get our item to be deleted var model = await this._service.GetAsync(id); // If we have nothing, throw an error if (model == null) throw new HttpException(404, string.Format(Resources.GenericNotFound, "List item")); 

Now I do this in many places, not just for deletion, but for updating. I would like to create an extension method that allows me to do something like this:

 // Get our item to be deleted var model = await this._service.GetAsync(id).ThowIfNull("List item"); 

I also need this to work with any type of return. Thus, in this case it can be an Account , but there will be a supplier who also has this code that returns the Order , but I need an extension method to work. / p>

I think there is an asynchronous bit here, but I could be wrong!

Does anyone know if this is possible?

+6
source share
3 answers

One way to avoid the async part of the wait is to make the extension method work with the type returned inside the Task

 public static T ThrowIfNull<T>(this T obj, string message) where T : class { if (obj == null) throw new HttpException(404, string.Format(Resources.GenericNotFound, message)); return obj; } 

I made a general extension method since I don’t know what type of model . Then you can just use it like that.

 var model = (await this._service.GetAsync(id)).ThrowIfNull("List item"); 

By placing await in brackets, you make sure that it will wait for the task and expand it before passing the result to the extension method.

Another option is to make the extension method work on Task<T>

 public static async Task<T> ThrowIfNullAsync<T>(this Task<T> task, string message) where T : class { var obj = await task; if (obj == null) throw new HttpException(404, string.Format(Resources.GenericNotFound, message)); return obj; } 

And you do not need a bracket.

 var model = await this._service.GetAsync(id).ThrowIfNullAsync("List item"); 

But this means that the exception is now wrapped up in a task, which may or may not be desirable depending on how you use this method.

+5
source

You can define an extension method on any T :

 public static class GenericExtensions { public static T ThrowIfNull<T>(this T obj, string message) { if (obj == null) throw new HttpException(404, string.Format(Resources.GenericNotFound, message)); return obj; } } 

If you don't need a return type, you can use object , but this will lead to boxing for value types (not sure if I will actually use this):

 public static class ObjectExtensions { public static void ThrowIfNull(this object obj, string message) { if (obj == null) throw new ArgumentNullException(message); } } 

And then use it for any return type:

 async Task SomeAsyncMethod() { (await Foo()).ThrowIfNull("hello"); } public Task<int> Foo() { return Task.FromResult(0); } 
+3
source

I created a method like this:

 /// <summary> /// Throws a 404 Not found exception /// </summary> /// <param name="model">The model to check</param> /// <param name="name">The name to display in the message</param> /// <returns></returns> public static T ThrowIfNotFound<T>(this T model, string name) { // If we have nothing, throw an error if (model == null) throw new HttpException(404, string.Format(Resources.GenericNotFound, name)); // Return our model return model; } 
0
source

All Articles