Model binding form posts to immutable objects
- 📅
- 📝 633 words
- 🕙 3 minutes
- 📦 .NET
- 🏷️ Azure, C#
- 💬 2 responses
I’ve been working on porting over my blog to a static site generator. I fired up an Azure Function to handle the form-comment to PR process to enable user comments to still be part of the site without using a 3rd party commenting system — more on that in the next post — and found the ASP.NET model binding for form posts distinctly lacking.
It’s been great getting back into .NET and brushing up some skills making the code clear, short and reusable. What I wanted was a super-clear action on my controller that tried to collect, validate and sanitize the data then, if all was well, create the pull request or report errors.
Ideally, it would look like this;
[FunctionName("PostComment")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestMessage request) {
var form = await request.Content.ReadAsFormDataAsync();
if (TryCreateComment(form, out Comment comment, out var errors))
await CreateCommentAsPullRequest(comment);
return request.CreateResponse(errors.Any()
? HttpStatusCode.BadRequest : HttpStatusCode.OK, String.Join("\n", errors));
}
To do that, we need a function capable of creating the Comment class from the form post. You could manually do it field by field, but that’s not reusable, highly repetitive, and, of course, no fun. The Comment class is — like all well-behaved little objects — immutable.
Creating a function to do this is simple with a little bit of reflection;
private static object ConvertParameter(string parameter, Type targetType) {
return String.IsNullOrWhiteSpace(parameter)
? null : TypeDescriptor.GetConverter(targetType).ConvertFrom(parameter);
}
private static bool TryCreateCommentFromForm(NameValueCollection form, out Comment comment, out List<string> errors) {
var constructor = typeof(Comment).GetConstructors()[0];
var values = constructor.GetParameters()
.ToDictionary(p => p.Name, p => ConvertParameter(form[p.Name], p.ParameterType)
?? (p.HasDefaultValue ? p.DefaultValue : new MissingRequiredValue()));
errors = values.Where(p => p.Value is MissingRequiredValue)
.Select(p => $"Form value missing for '{p.Key}'").ToList();
comment = errors.Any() ? null : (Comment)constructor.Invoke(values.Values.ToArray());
return !errors.Any();
}
This method grabs the constructor for the Comment
and tries to find keys in the form that match the parameter name. Any missing are reported as errors unless they have a default value, in which case that default is used. MissingRequiredValue
is just an empty object to act as a sentinel. The use of TypeDescriptor.GetConverter
means it should be quite happy handling integers, decimals, and URLs.
The Comment
constructor specifies which fields are required, and the parameter names must match the form field names by convention. Any optional value has a default value that the constructor provides a sensible default for.
public Comment(string post_id, string message, string author, string email,
DateTime? date = null, Uri url = null, int? id = null, string gravatar = null) {
this.post_id = pathValidChars.Replace(post_id, "-");
this.message = message;
this.author = author;
this.email = email;
this.date = date ?? DateTime.UtcNow;
this.url = url;
this.id = id ?? new { this.post_id, this.author, this.message, this.date }.GetHashCode();
this.gravatar = gravatar ?? EncodeGravatar(email);
}
I’ll post more of the form commenting system source soon once it’s a bit better tested and I’ve looked into anti-spam integration. The Jekyll rendering templates and WordPress exporter are available.
[)amien
2 responses to Model binding form posts to immutable objects
I will! Each blog post is going to be manually approved via a pull request so I can visually check anything that goes through :)
Nice, I need to do something like this for my website. Are you going to allow the body of the content to include markdown?