Skip to content

Instantly share code, notes, and snippets.

@jfbueno
Last active June 7, 2016 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfbueno/e981cc90f47bbfc9bbc6003adac7f4c3 to your computer and use it in GitHub Desktop.
Save jfbueno/e981cc90f47bbfc9bbc6003adac7f4c3 to your computer and use it in GitHub Desktop.
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutContact(int id, EntityContact entityContact)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != entityContact.Id)
{
return BadRequest();
}
_db.Entry(entityContact).State = EntityState.Modified;
try
{
await _db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ContactExists(id))
{
return NotFound();
}
throw;
}
return StatusCode(HttpStatusCode.NoContent);
}
protected static T GetFromEntityModel<T>(Entity entityModel) where T : EntityBaseViewModel, new()
{
var derivedTypes = GetDerivedClasses<T>().ToList();
var typeToConvert = (from type in derivedTypes
let properties = type.GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(SpecificInfoPerCountryAttribute)))
where properties != null
where entityModel.SpecificInfoPerCountry.Count > 0 &&
entityModel.SpecificInfoPerCountry.All(attr => properties.Any(prop => prop.Name == attr.Description))
select type).SingleOrDefault() ?? typeof(T);
var specificViewModel = (T)Activator.CreateInstance(typeToConvert);
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment