Last active
June 7, 2016 17:25
-
-
Save jfbueno/e981cc90f47bbfc9bbc6003adac7f4c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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