From 538297d904b7c1470c306efddc128874c7675680 Mon Sep 17 00:00:00 2001 From: Chanan Braunstein Date: Tue, 8 May 2018 14:14:19 -0600 Subject: [PATCH] Id management --- README.md | 18 +++++++-- src/BlazorDB/BlazorDB.csproj | 2 +- src/BlazorDB/Logger.cs | 34 ++++++++++++++--- src/BlazorDB/ServiceCollectionExtensions.cs | 2 +- src/BlazorDB/StorageContext.cs | 15 +++++++- src/BlazorDB/StorageSet.cs | 42 ++++++++++++++++++++- src/Sample/Pages/Index.cshtml | 2 +- 7 files changed, 99 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 8410b44..fc03be4 100644 --- a/README.md +++ b/README.md @@ -12,13 +12,13 @@ So far the the API is very simplestic but works. First add a reference to the nuget package: ``` -Install-Package BlazorDB -Version 0.0.1 +Install-Package BlazorDB -Version 0.0.2 ``` or ``` -dotnet add package BlazorDB --version 0.0.1 +dotnet add package BlazorDB --version 0.0.2 ``` Then in Program.cs add Blazor DB to the dependency injection services: @@ -26,11 +26,17 @@ Then in Program.cs add Blazor DB to the dependency injection services: ``` var serviceProvider = new BrowserServiceProvider(services => { - services.AddBlazorDB(typeof(Program).Assembly); + services.AddBlazorDB(options => + { + options.LogDebug = true; + options.Assembly = typeof(Program).Assembly; + }); }); new BrowserRenderer(serviceProvider).AddComponent("app"); ``` +Set `LogDebug` to see debug output in the browser console. + ### Setup Create at least one model and context for example: @@ -46,6 +52,8 @@ public class Person } ``` +if the field `public int Id { get; set; }` exists it will be managed by BlazorDB + and a context, for example, Context.cs: ``` public class Context : StorageContext @@ -67,10 +75,12 @@ Inject your context into your component: Create a model and add it to your Context: ``` -var person = new Person { Id = 1, FirstName = "John", LastName = "Smith" }; +var person = new Person { FirstName = "John", LastName = "Smith" }; Context.People.Add(person); ``` +Do not set the Id field. It will be assigned by BlazorDB. + Call SaveChanges: ``` diff --git a/src/BlazorDB/BlazorDB.csproj b/src/BlazorDB/BlazorDB.csproj index 229cf28..1e20fa5 100644 --- a/src/BlazorDB/BlazorDB.csproj +++ b/src/BlazorDB/BlazorDB.csproj @@ -7,7 +7,7 @@ false 7.3 BlazorDB - 0.0.1 + 0.0.2 Chanan Braunstein Blazor localStorage Database In memory, persisted to localstorage, database for .net Blazor browser framework diff --git a/src/BlazorDB/Logger.cs b/src/BlazorDB/Logger.cs index 765a3a8..1847a48 100644 --- a/src/BlazorDB/Logger.cs +++ b/src/BlazorDB/Logger.cs @@ -13,31 +13,53 @@ internal static class Logger internal static void StartContextType(Type contextType) { if (!LogDebug) return; - BlazorLogger.Logger.GroupCollapsed($"Context Loaded: %c{contextType.Namespace}.{contextType.Name}", blue); + BlazorLogger.Logger.GroupCollapsed($"Context loaded: %c{contextType.Namespace}.{contextType.Name}", blue); } - internal static void EndContextType() + internal static void ContextSaved(Type contextType) + { + if (!LogDebug) return; + BlazorLogger.Logger.GroupCollapsed($"Context %csaved: %c{contextType.Namespace}.{contextType.Name}", green, blue); + } + + internal static void StorageSetSaved(Type modelType, int count) + { + if (!LogDebug) return; + BlazorLogger.Logger.Log($"StorageSet %csaved: %c{modelType.Namespace}.{modelType.Name}%c with {count} items", green, blue, normal); + } + + internal static void EndGroup() { if (!LogDebug) return; BlazorLogger.Logger.GroupEnd(); } - internal static void ItemAddedToContext(string contextTypeName, Type modelType) + internal static void ItemAddedToContext(string contextTypeName, Type modelType, int id, object item) { if (!LogDebug) return; - BlazorLogger.Logger.Log($"Item %c{modelType.Namespace}.{modelType.Name}%c %cadded%c to Context: %c{contextTypeName}", blue, normal, green, normal, blue); + BlazorLogger.Logger.GroupCollapsed($"Item %c{modelType.Namespace}.{modelType.Name}%c %cadded%c to context: %c{contextTypeName}%c with id: {id}", blue, normal, green, normal, blue, normal); + BlazorLogger.Logger.Log("Item: %o", item); + BlazorLogger.Logger.GroupEnd(); + } + + internal static void ItemAddedToContext(string contextTypeName, Type modelType, object item) + { + if (!LogDebug) return; + BlazorLogger.Logger.GroupCollapsed($"Item %c{modelType.Namespace}.{modelType.Name}%c %cadded%c to context: %c{contextTypeName}", blue, normal, green, normal, blue); + BlazorLogger.Logger.Log("Item: %o", item); + BlazorLogger.Logger.GroupEnd(); } internal static void LoadModelInContext(Type modelType) { if (!LogDebug) return; - BlazorLogger.Logger.Log($"StorageContext Loaded: %c{modelType.Namespace}.{modelType.Name}", blue); + BlazorLogger.Logger.Log($"StorageSet loaded: %c{modelType.Namespace}.{modelType.Name}", blue); } internal static void ItemRemovedFromContext(string contextTypeName, Type modelType) { if (!LogDebug) return; - BlazorLogger.Logger.Log($"Item %c{modelType.Namespace}.{modelType.Name}%c %cremoved%c from Context: %c{contextTypeName}", blue, normal, red, normal, blue); + BlazorLogger.Logger.Log($"Item %c{modelType.Namespace}.{modelType.Name}%c %cremoved%c from context: %c{contextTypeName}", blue, normal, red, normal, blue); } } } diff --git a/src/BlazorDB/ServiceCollectionExtensions.cs b/src/BlazorDB/ServiceCollectionExtensions.cs index 9c48fbb..d20a54c 100644 --- a/src/BlazorDB/ServiceCollectionExtensions.cs +++ b/src/BlazorDB/ServiceCollectionExtensions.cs @@ -46,7 +46,7 @@ private static void RegisterBlazorDB(IServiceCollection serviceCollection, IEnum } } RegisterContext(serviceCollection, contextType, context); - Logger.EndContextType(); + Logger.EndGroup(); } } diff --git a/src/BlazorDB/StorageContext.cs b/src/BlazorDB/StorageContext.cs index 71c7e1a..fb3d3c8 100644 --- a/src/BlazorDB/StorageContext.cs +++ b/src/BlazorDB/StorageContext.cs @@ -1,4 +1,6 @@ -using Microsoft.AspNetCore.Blazor; +using System; +using System.Reflection; +using Microsoft.AspNetCore.Blazor; namespace BlazorDB { @@ -7,6 +9,7 @@ public class StorageContext : IStorageContext public void SaveChanges() { var type = GetType(); + Logger.ContextSaved(type); foreach (var prop in type.GetProperties()) { if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(StorageSet<>)) @@ -15,8 +18,18 @@ public void SaveChanges() var modelType = prop.PropertyType.GetGenericArguments()[0]; var storageTableName = Util.GetStorageTableName(type, modelType); BlazorDBInterop.SetItem(storageTableName, JsonUtil.Serialize(storageSetValue), false); + var count = GetListCount(prop); + Logger.StorageSetSaved(modelType, count); } } + Logger.EndGroup(); + } + + private int GetListCount(PropertyInfo prop) + { + var list = prop.GetValue(this); + var countProp = list.GetType().GetProperty("Count"); + return (int)countProp.GetValue(list); } } } diff --git a/src/BlazorDB/StorageSet.cs b/src/BlazorDB/StorageSet.cs index 1a59e09..34aef68 100644 --- a/src/BlazorDB/StorageSet.cs +++ b/src/BlazorDB/StorageSet.cs @@ -1,4 +1,5 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; namespace BlazorDB @@ -15,8 +16,45 @@ public class StorageSet : IList where TModel : class public void Add(TModel item) { + if(HasId(item)) + { + var id = GetId(item); + if (id > 0) throw new ArgumentException("Can't add item to set that already has an Id", "Id"); + id = SetId(item); + Logger.ItemAddedToContext(StorageContextTypeName, item.GetType(), id, item); + } + else + { + Logger.ItemAddedToContext(StorageContextTypeName, item.GetType(), item); + } List.Add(item); - Logger.ItemAddedToContext(StorageContextTypeName, item.GetType()); + } + + //TODO: Consider using an "Id table" + private int SetId(TModel item) + { + var prop = item.GetType().GetProperty("Id"); + int max = 0; + foreach (var i in List) + { + int currentId = (int)prop.GetValue(i); + if (currentId > max) max = currentId; + } + int id = max + 1; + prop.SetValue(item, id); + return id; + } + + private int GetId(TModel item) + { + var prop = item.GetType().GetProperty("Id"); + return (int)prop.GetValue(item); + } + + private bool HasId(TModel item) + { + var prop = item.GetType().GetProperty("Id"); + return prop != null; } public void Clear() diff --git a/src/Sample/Pages/Index.cshtml b/src/Sample/Pages/Index.cshtml index 5b47782..fbd3045 100644 --- a/src/Sample/Pages/Index.cshtml +++ b/src/Sample/Pages/Index.cshtml @@ -24,7 +24,7 @@ private Person Person { get; set; } void onclickAddPerson() { - var person = new Person { Id = 1, FirstName = "John", LastName = "Smith" }; + var person = new Person { FirstName = "John", LastName = "Smith" }; Context.People.Add(person); }