Skip to content
This repository has been archived by the owner on May 10, 2020. It is now read-only.

Commit

Permalink
Id management
Browse files Browse the repository at this point in the history
  • Loading branch information
chanan committed May 8, 2018
1 parent 326aa1e commit 538297d
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 16 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,31 @@ 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:

```
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>("app");
```

Set `LogDebug` to see debug output in the browser console.

### Setup

Create at least one model and context for example:
Expand All @@ -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
Expand All @@ -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:

```
Expand Down
2 changes: 1 addition & 1 deletion src/BlazorDB/BlazorDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<BlazorLinkOnBuild>false</BlazorLinkOnBuild>
<LangVersion>7.3</LangVersion>
<PackageId>BlazorDB</PackageId>
<Version>0.0.1</Version>
<Version>0.0.2</Version>
<Authors>Chanan Braunstein</Authors>
<Title>Blazor localStorage Database</Title>
<Description>In memory, persisted to localstorage, database for .net Blazor browser framework</Description>
Expand Down
34 changes: 28 additions & 6 deletions src/BlazorDB/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion src/BlazorDB/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static void RegisterBlazorDB(IServiceCollection serviceCollection, IEnum
}
}
RegisterContext(serviceCollection, contextType, context);
Logger.EndContextType();
Logger.EndGroup();
}
}

Expand Down
15 changes: 14 additions & 1 deletion src/BlazorDB/StorageContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Blazor;
using System;
using System.Reflection;
using Microsoft.AspNetCore.Blazor;

namespace BlazorDB
{
Expand All @@ -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<>))
Expand All @@ -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);
}
}
}
42 changes: 40 additions & 2 deletions src/BlazorDB/StorageSet.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;

namespace BlazorDB
Expand All @@ -15,8 +16,45 @@ public class StorageSet<TModel> : IList<TModel> 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()
Expand Down
2 changes: 1 addition & 1 deletion src/Sample/Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 538297d

Please sign in to comment.