From bb738b13d8b07a226480128a35d195620306af75 Mon Sep 17 00:00:00 2001 From: Omar Muscatello <7016897+OmarMuscatello@users.noreply.github.com> Date: Tue, 8 Aug 2017 08:19:18 +0200 Subject: [PATCH] Update version number Added Delta.GetSpecifiedPropertyNames and Delta.GetNotSpecifiedPropertyNames methods. Documentation comments translated to English. Example project comments translated to English. --- .../Controllers/EFController.cs | 50 +++++------ .../Controllers/HomeController.cs | 25 +++--- .../SimplePatch.WebAPI/Global.asax.cs | 1 + src/SimplePatch/Delta.cs | 90 ++++++++++++------- src/SimplePatch/Properties/AssemblyInfo.cs | 4 +- 5 files changed, 96 insertions(+), 74 deletions(-) diff --git a/src/Examples/SimplePatch.WebAPI/Controllers/EFController.cs b/src/Examples/SimplePatch.WebAPI/Controllers/EFController.cs index 875c6ed..e06179d 100644 --- a/src/Examples/SimplePatch.WebAPI/Controllers/EFController.cs +++ b/src/Examples/SimplePatch.WebAPI/Controllers/EFController.cs @@ -17,57 +17,53 @@ public async Task> GetAll() return await db.People.ToListAsync(); } -[HttpPatch] -public async Task PatchOne(int id, Delta person) -{ - //Determino l'entità da aggiornare in base al parametro id - var personToPatch = await db.People.FindAsync(id); - if (personToPatch == null) return BadRequest("Person not found"); + [HttpPatch] + public async Task PatchOne(int id, Delta person) + { + // Determines the entity to be updated according to the id parameter + var personToPatch = await db.People.FindAsync(id); + if (personToPatch == null) return BadRequest("Person not found"); - /* - * Applico le modifiche specificate all'entità originale. Tuttavia, il parametro Id non viene mai aggiornato. - * Vedi Global.asax - */ - person.Patch(personToPatch); - db.Entry(personToPatch).State = EntityState.Modified; + // Apply the specified changes to the original entity. The Id property won't be changed. See why in Global.asax. + person.Patch(personToPatch); - //Adesso la variabile personToPatch è aggiornata + // Mark the entity as modified + db.Entry(personToPatch).State = EntityState.Modified; - //Salvo le modifiche - await db.SaveChangesAsync(); + // Now the personToPatch variable is updated - return Ok(personToPatch); -} + // Save the changes + await db.SaveChangesAsync(); + + return Ok(personToPatch); + } [HttpPatch] public async Task PatchMultiple(DeltaCollection people) { foreach (var person in people) { - //Tento di ottenere il valore della proprietà Id + // Try to get the value of the Id property if (person.TryGetPropertyValue(nameof(PersonEF.Id), out var id)) { - //Determino l'entità da aggiornare in base all'id specificato + // Determines the entity to be updated according to the id parameter var personToPatch = await db.People.FindAsync(Convert.ToInt32(id)); if (personToPatch == null) return BadRequest("Person not found (Id = " + id + ")"); - - /* - * Applico le modifiche specificate all'entità originale. Tuttavia, il parametro Id non viene mai aggiornato. - * Vedi Global.asax - */ + + // Apply the specified changes to the original entity person.Patch(personToPatch); - //Contrassegno l'entità come modificata + // Mark the entity as modified db.Entry(personToPatch).State = EntityState.Modified; } else { - //La proprietà Id non è stata specificata per la persona rappresentata dalla variabile person + // The Id property was not specified for the person represented by the person variable return BadRequest("Id property not found for a person"); } } - //Salvo le modifiche + // Save the changes await db.SaveChangesAsync(); return Ok(await db.People.ToListAsync()); diff --git a/src/Examples/SimplePatch.WebAPI/Controllers/HomeController.cs b/src/Examples/SimplePatch.WebAPI/Controllers/HomeController.cs index 5f51160..3cc9edd 100644 --- a/src/Examples/SimplePatch.WebAPI/Controllers/HomeController.cs +++ b/src/Examples/SimplePatch.WebAPI/Controllers/HomeController.cs @@ -17,17 +17,17 @@ public IEnumerable GetAll() [HttpPatch] public IHttpActionResult PatchOne(int id, Delta person) { - //Determino l'entità da aggiornare in base al parametro id + // Determines the entity to be updated according to the id parameter var personToPatch = TestData.People.FirstOrDefault(x => x.Id == id); - if (personToPatch == null) return BadRequest("Person not found"); - /* - * Applico le modifiche specificate all'entità originale. Tuttavia, il parametro Id non viene mai aggiornato. - * Vedi Global.asax - */ + var c = person.GetSpecifiedPropertyNames(); + + if (personToPatch == null) return BadRequest("Person not found"); + + // Apply the changes specified to the original entity person.Patch(personToPatch); - //Adesso la variabile personToPatch è aggiornata + // Now the personToPatch variable is updated return Ok(personToPatch); } @@ -37,22 +37,19 @@ public IHttpActionResult PatchMultiple(DeltaCollection people) { foreach (var person in people) { - //Tento di ottenere il valore della proprietà Id + // Try to get the value of the Id property if (person.TryGetPropertyValue(nameof(Person.Id), out var id)) { - //Determino l'entità da aggiornare in base all'id specificato + // Determines the entity to be updated according to the specified id var entityToPatch = TestData.People.FirstOrDefault(x => x.Id == Convert.ToInt32(id)); if (entityToPatch == null) return BadRequest("Person not found (Id = " + id + ")"); - /* - * Applico le modifiche specificate all'entità originale. Tuttavia, il parametro Id non viene mai aggiornato. - * Vedi Global.asax - */ + // Apply the specified changes to the original entity person.Patch(entityToPatch); } else { - //La proprietà Id non è stata specificata per la persona rappresentata dalla variabile person + // The Id property was not specified for the person represented by the person variable return BadRequest("Id property not found for a person"); } } diff --git a/src/Examples/SimplePatch.WebAPI/Global.asax.cs b/src/Examples/SimplePatch.WebAPI/Global.asax.cs index 173c04d..34d53ef 100644 --- a/src/Examples/SimplePatch.WebAPI/Global.asax.cs +++ b/src/Examples/SimplePatch.WebAPI/Global.asax.cs @@ -10,6 +10,7 @@ protected void Application_Start() GlobalConfiguration.Configure(WebApiConfig.Register); DeltaConfig.Init((cfg) => { + // Id property will not be changed when calling the Patch method. cfg.ExcludeProperties(x => x.Id); }); } diff --git a/src/SimplePatch/Delta.cs b/src/SimplePatch/Delta.cs index 5a455fa..091c885 100644 --- a/src/SimplePatch/Delta.cs +++ b/src/SimplePatch/Delta.cs @@ -8,10 +8,13 @@ namespace SimplePatch { public sealed class Delta : IDictionary where TEntity : class, new() { + /// + /// Contains the property-value pair for the entity. + /// private Dictionary dict = new Dictionary(); /// - /// Il nome completo del tipo + /// The full name of the type . /// private string typeFullName; @@ -32,18 +35,18 @@ public Delta() : base() } /// - /// Crea una nuova entità del tipo e popola le proprietà per cui si dispone un valore. + /// Create a new instance and fill the properties for which you have a value. /// - /// Una nuova entità di tipo + /// The instance public TEntity GetEntity() { return SetPropertiesValue(new TEntity()); } /// - /// Imposta il valore delle proprietà modificate per l'entità specificata. + /// Sets the value of the modified properties for the specified entity. /// - /// L'entità da modificare. + /// Entity to be edited. public void Patch(TEntity entity) { if (entity == null) throw new ArgumentNullException(); @@ -51,21 +54,21 @@ public void Patch(TEntity entity) } /// - /// Indica se il nome della proprietà specificata è presente nella lista dei nomi delle proprietà modificate. + /// Indicates whether the specified property name is present in the list of changed property names. /// - /// Il nome della proprietà da verificare - /// True se il nome della proprietà è presente nella lista dei nomi delle proprietà modificate, altrimenti False + /// The name of the property to be verified. + /// True if the property name is present in the list of changed property names, otherwise False. public bool HasProperty(string propertyName) { return dict.ContainsKey(propertyName); } /// - /// Tenta di ottenere il valore della proprietà con nome specificato. + /// Try to get the property value with the specified name. /// - /// Il nome della proprietà per cui ottenere il valore. - /// Il valore della proprietà specificata. - /// True se è stato possibile ottenere il valore della proprietà, altrimenti False. + /// The name of the property to get the value. + /// The value of the specified property. + /// True if it was possible to get the property value, otherwise False. public bool TryGetPropertyValue(string propertyName, out object propertyValue) { if (!HasProperty(propertyName)) @@ -79,39 +82,62 @@ public bool TryGetPropertyValue(string propertyName, out object propertyValue) } /// - /// Aggiunge un elemento al dizionario solo se la chiave specificata è un nome di proprietà di . + /// Adds an element to the dictionary only if the specified key is a property name of . /// - /// Elemento da aggiungere. Se .Value è null o l'elemento non verrà aggiunto. Vedi + /// Item to be added. The element will not be added if .Value is null or it is equal to . See public void Add(KeyValuePair item) { if (IsPropertyAllowed(item.Key)) dict.Add(item.Key, item.Value); } /// - /// Aggiunge la chiave e il valore specificati al dizionario solo se la chiave specificata è un nome di proprietà di . + /// Adds the specified key and value to the dictionary only if the specified key is a property name of . /// - /// Chiave dell'elemento da aggiungere. - /// Valore dell'elemento da aggiungere. Se null o l'elemento non verrà aggiunto. Vedi + /// Element key to add. + /// Value of element to be added. The element will not be added if null or equal to . See public void Add(string key, object value) { if (IsPropertyAllowed(key)) dict.Add(key, value); } /// - /// Indica se espone una proprietà con il nome specificato. + /// Returns the properties that have been specified (compared to properties) as an enumeration of property names. + /// + /// The property names. + public IEnumerable GetSpecifiedPropertyNames() + { + foreach (var item in dict) + { + yield return item.Key; + } + } + + /// + /// Returns the properties that haven't been specified (compared to properties) as an enumeration of property names. /// - /// Il nome della proprietà da verificare. - /// True se espone una proprietà con il nome specificato, altrimenti False + /// The property names. + public IEnumerable GetNotSpecifiedPropertyNames() + { + return DeltaCache.entityProperties[typeFullName].Select(x => x.Name).Where(x => !dict.ContainsKey(x)); + } + + #region Private methods + + /// + /// Indicates whether exposes a property with the specified name. + /// + /// The name of the property to be verified. + /// True if exposes a property with the specified name, otherwise False. private bool IsPropertyAllowed(string propertyName) { return !string.IsNullOrEmpty(propertyName) && DeltaCache.entityProperties[typeFullName].Any(x => x.Name == propertyName); } /// - /// Imposta il valore per ogni proprietà di per cui esiste un riferimento in . + /// Set the value for each property of for which there is a reference in . /// - /// L'istanza di per cui impostare le proprietà. - /// L'entità modificata + /// The instance of to set properties. + /// The modified entity. private TEntity SetPropertiesValue(TEntity entity) { //Se la cache non contiene la lista delle proprietà per il tipo specificato, aggiungo le proprietà @@ -135,11 +161,11 @@ private TEntity SetPropertiesValue(TEntity entity) } /// - /// Indica se, per la proprietà con nome specificato appartenente all'entità specificata, deve essere disabilitata la modifica. + /// Specifies whether the change must be disabled for the property with specified name belonging to the specified entity. /// - /// Il nome intero dell'entità che espone la proprietà. - /// Il nome della proprietà. - /// True se la proprietà è esclusa dalle modifiche, altrimenti False. + /// The entity's full name that exposes the property. + /// The name of the property. + /// True if property is excluded from changes, otherwise False. private bool IsExcludedProperty(string typeFullName, string propertyName) { if (!DeltaCache.excludedProperties.ContainsKey(typeFullName)) return false; @@ -148,16 +174,18 @@ private bool IsExcludedProperty(string typeFullName, string propertyName) } /// - /// Restituisce il tipo specificato dal parametro o il tipo sottostante se è . + /// Returns the type specified by the parameter or type below if is . /// - /// Il tipo da verificare. - /// Il tipo specificato dal parametro o il tipo sottostante se è . + /// The type to be verified. + /// The type specified by the parameter or type below if is . private Type GetTrueType(Type type) { return Nullable.GetUnderlyingType(type) ?? type; } - #region Implementazione dell'interfaccia IDictionary + #endregion + + #region Implementing the IDictionary Interface public ICollection Keys => dict.Keys; public ICollection Values => dict.Values; public int Count => dict.Count; diff --git a/src/SimplePatch/Properties/AssemblyInfo.cs b/src/SimplePatch/Properties/AssemblyInfo.cs index 09295fd..29b400c 100644 --- a/src/SimplePatch/Properties/AssemblyInfo.cs +++ b/src/SimplePatch/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // È possibile specificare tutti i valori oppure impostare valori predefiniti per i numeri relativi alla revisione e alla build // usando l'asterisco '*' come illustrato di seguito: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.1.0.0")] +[assembly: AssemblyFileVersion("1.1.0.0")]