Skip to content

Instantly share code, notes, and snippets.

@stefc
Created August 6, 2016 12:26
Show Gist options
  • Save stefc/66b43041394688ef447bec99cde870a9 to your computer and use it in GitHub Desktop.
Save stefc/66b43041394688ef447bec99cde870a9 to your computer and use it in GitHub Desktop.
NancyFX ModelBinding Example
public class AufnehmenAufgabe : IDomainCommand {
public Guid Id { get; }
public string Titel { get; }
public AufnehmenAufgabe(Guid id, string titel)
{
Id = id;
Titel = titel;
}
}
public class AendernAufgabenTitel : IDomainCommand {
public Guid Id { get; }
public string Titel { get; }
public AendernAufgabenTitel(Guid id, string titel)
{
Id = id;
Titel = titel;
}
}
public class EntfernenAufgabe : IDomainCommand {
public Guid Id { get; }
public EntfernenAufgabe(Guid id)
{
Id = id;
}
}
// Update Command's
Put("/{id}", args => {
var id = Guid.Parse(args.id);
var domainModel = DomainModel;
if (domainModel.Equals(nameof(AendernAufgabenTitel))) {
var token = JObject.Parse(Request.Body.AsString())?.Root;
var titel = token.Value<string>(nameof(AendernAufgabenTitel.Titel));
var cmd = new AendernAufgabenTitel(id, titel);
return true;
}
else if (domainModel.Equals(nameof(ErledigenAufgabe))) {
var cmd = new ErledigenAufgabe(id);
return true;
}
else if (domainModel.Equals(nameof(WiedervorlegenAufgabe))) {
var cmd = new WiedervorlegenAufgabe(id);
return true;
}
return CommandNotAccepted;
});
@stefc
Copy link
Author

stefc commented Aug 6, 2016

I would be happy if I can Bind it easy with NancyFx. For example the parameter naming of the ctor can be named like the args.id or body.titel stuff.

this.Bind<AendernAufgabenTitel>( (id,titel) => new AendernAufgabenTitel(id,title))

The above work fine but if I can delegate that binding to NancyFx it would be even finer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment