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

Commit

Permalink
Added basic logging
Browse files Browse the repository at this point in the history
  • Loading branch information
chanan committed May 8, 2018
1 parent 80a4c77 commit 326aa1e
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/BlazorDB/BlazorDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="BlazorLogger" Version="0.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Browser" Version="0.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="0.3.0" />
</ItemGroup>
Expand Down
43 changes: 43 additions & 0 deletions src/BlazorDB/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;

namespace BlazorDB
{
internal static class Logger
{
internal static bool LogDebug { get; set; } = true;
internal static readonly string blue = "color: blue; font-style: bold;";
internal static readonly string green = "color: green; font-style: bold;";
internal static readonly string red = "color: red; font-style: bold;";
internal static readonly string normal = "color: black; font-style: normal;";

internal static void StartContextType(Type contextType)
{
if (!LogDebug) return;
BlazorLogger.Logger.GroupCollapsed($"Context Loaded: %c{contextType.Namespace}.{contextType.Name}", blue);
}

internal static void EndContextType()
{
if (!LogDebug) return;
BlazorLogger.Logger.GroupEnd();
}

internal static void ItemAddedToContext(string contextTypeName, Type modelType)
{
if (!LogDebug) return;
BlazorLogger.Logger.Log($"Item %c{modelType.Namespace}.{modelType.Name}%c %cadded%c to Context: %c{contextTypeName}", blue, normal, green, normal, blue);
}

internal static void LoadModelInContext(Type modelType)
{
if (!LogDebug) return;
BlazorLogger.Logger.Log($"StorageContext 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);
}
}
}
10 changes: 10 additions & 0 deletions src/BlazorDB/Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Reflection;

namespace BlazorDB
{
public class Options
{
public Assembly Assembly { get; set; }
public bool LogDebug { get; set; }
}
}
31 changes: 20 additions & 11 deletions src/BlazorDB/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ public static class ServiceCollectionExtensions
private static readonly Type storageContext = typeof(StorageContext);
private static readonly Type genericStorageSetType = typeof(StorageSet<>);
private static readonly Type genericListType = typeof(List<>);
public static IServiceCollection AddBlazorDB(this IServiceCollection serviceCollection, Assembly assembly)
public static IServiceCollection AddBlazorDB(this IServiceCollection serviceCollection, Action<Options> configure)
{
Scan(serviceCollection, assembly);
if (configure == null) throw new ArgumentNullException(nameof(configure));
var options = new Options();
configure(options);
if (options.LogDebug) Logger.LogDebug = true;
Scan(serviceCollection, options.Assembly);
return serviceCollection;
}

Expand All @@ -26,28 +30,33 @@ private static void Scan(IServiceCollection serviceCollection, Assembly assembly

private static void RegisterBlazorDB(IServiceCollection serviceCollection, IEnumerable<Type> types)
{
foreach(var type in types)
foreach(var contextType in types)
{
var context = Activator.CreateInstance(type);
foreach(var prop in type.GetProperties())
var context = Activator.CreateInstance(contextType);
Logger.StartContextType(contextType);
foreach(var prop in contextType.GetProperties())
{
if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(StorageSet<>))
{
var modelType = prop.PropertyType.GetGenericArguments()[0];
Logger.LoadModelInContext(modelType);
var storageSetType = genericStorageSetType.MakeGenericType(modelType);
var storageSet = GetStorageSet(storageSetType, type, modelType);
var storageSet = GetStorageSet(storageSetType, contextType, modelType);
prop.SetValue(context, storageSet);
}
}
RegisterContext(serviceCollection, type, context);
RegisterContext(serviceCollection, contextType, context);
Logger.EndContextType();
}
}

private static object GetStorageSet(Type storageSetType, Type type, Type modelType)
private static object GetStorageSet(Type storageSetType, Type contextType, Type modelType)
{
var storageTableName = Util.GetStorageTableName(type, modelType);
var storageTableName = Util.GetStorageTableName(contextType, modelType);
var value = BlazorDBInterop.GetItem(storageTableName, false);
var instance = Activator.CreateInstance(storageSetType);
var prop = storageSetType.GetProperty("StorageContextTypeName", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
prop.SetValue(instance, Util.GetFullyQualifiedTypeName(contextType));
return value != null ? SetList(instance, Deserialize(modelType, value)) : instance;
}

Expand All @@ -63,8 +72,8 @@ private static object Deserialize(Type modelType, string value)
var method = typeof(JsonWrapper).GetMethod("Deserialize");
var listGenericType = genericListType.MakeGenericType(modelType);
var genericMethod = method.MakeGenericMethod(listGenericType);
var x = genericMethod.Invoke(new JsonWrapper(), new object[] { value });
return x;
var list = genericMethod.Invoke(new JsonWrapper(), new object[] { value });
return list;
}

private static IEnumerable<Type> ScanForContexts(IServiceCollection serviceCollection, Assembly assembly)
Expand Down
6 changes: 5 additions & 1 deletion src/BlazorDB/StorageSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace BlazorDB
{
public class StorageSet<TModel> : IList<TModel> where TModel : class
{
private string StorageContextTypeName { get; set; }
private IList<TModel> List { get; set; } = new List<TModel>();
public TModel this[int index] { get => List[index]; set => List[index] = value; }

Expand All @@ -15,6 +16,7 @@ public class StorageSet<TModel> : IList<TModel> where TModel : class
public void Add(TModel item)
{
List.Add(item);
Logger.ItemAddedToContext(StorageContextTypeName, item.GetType());
}

public void Clear()
Expand Down Expand Up @@ -49,7 +51,9 @@ public void Insert(int index, TModel item)

public bool Remove(TModel item)
{
return List.Remove(item);
var removed = List.Remove(item);
if (removed) Logger.ItemRemovedFromContext(StorageContextTypeName, item.GetType());
return removed;
}

public void RemoveAt(int index)
Expand Down
4 changes: 2 additions & 2 deletions src/BlazorDB/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ namespace BlazorDB
{
internal static class Util
{
public static string GetStorageTableName(Type Context, Type Model)
internal static string GetStorageTableName(Type Context, Type Model)
{
var databaseName = GetFullyQualifiedTypeName(Context);
var tableName = GetFullyQualifiedTypeName(Model);
return $"{databaseName}-{tableName}";
}

private static object GetFullyQualifiedTypeName(Type type)
internal static object GetFullyQualifiedTypeName(Type type)
{
return $"{type.Namespace}.{type.Name}";
}
Expand Down
11 changes: 11 additions & 0 deletions src/Sample/Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

<button onclick="@onclickAddPerson">Add Person</button>

<button onclick="@onclickRemovePerson">Remove Person</button>

<button onclick="@onclickGetPerson">Get Person</button>

<button onclick="@onclickSaveChanges">SaveChanges</button>
Expand All @@ -26,6 +28,15 @@
Context.People.Add(person);
}

void onclickRemovePerson()
{
var query = from person in Context.People
where person.Id == 1
select person;
var personToRemove = query.Single();
Context.People.Remove(personToRemove);
}

void onclickGetPerson()
{
var query = from person in Context.People
Expand Down
7 changes: 5 additions & 2 deletions src/Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using BlazorDB;
using Microsoft.AspNetCore.Blazor.Browser.Rendering;
using Microsoft.AspNetCore.Blazor.Browser.Services;
using System;

namespace Sample
{
Expand All @@ -11,7 +10,11 @@ static void Main(string[] args)
{
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");
}
Expand Down

0 comments on commit 326aa1e

Please sign in to comment.