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

Commit

Permalink
Merge pull request #4 from chanan/one_to_one_association
Browse files Browse the repository at this point in the history
One to one association
  • Loading branch information
chanan authored May 13, 2018
2 parents 3630159 + ae679cf commit 886141a
Show file tree
Hide file tree
Showing 17 changed files with 531 additions and 57 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Set `LogDebug` to see debug output in the browser console.

### Setup

*NOTE:* Models stored by BlazorDB require that an int Id property exist on the model. The Id property will be maintained by BlazorDB, you dont need to set it yourself.

Create at least one model and context for example:

Person.cs:
Expand All @@ -49,6 +51,7 @@ public class Person
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address HomeAddress { get; set; }
}
```

Expand Down Expand Up @@ -104,6 +107,42 @@ void onclickGetPerson()
}
```

## Associations

So far, only one to one associations work.

Associations work in the same context. If you have an object in another object that is not in the context, it will be serialized to localStorage as one "complex" document.

For example, in `Context.cs` only Person is in the Context and Address is not. Therefore, Person will contain Address, and Address will not be a seperate object.

### One to One Association

When an object refers to another object that are both in Context, they are stored as a reference, such that changing the reference will update both objects.

For example, `AssociationContext.cs`:


```
public class AssociationContext : StorageContext
{
public StorageSet<Person> People { get; set; }
public StorageSet<Address> Addresses { get; set; }
}
```

`Person.cs` as shown above has a property `public Address HomeAddress { get; set; }`. Because unlike `Context.cs`, `AssociationContext.cs` does define `public StorageSet<Address> Addresses { get; set; }` references are stored as "foreign keys" instead of complex objects.

Therefore, like in `Associations.cshtml` example, chaning the Address will Change the Person's HomeAddress:

```
Context.People[0].HomeAddress.Street = "Changed Streeet";
Context.SaveChanges();
Console.WriteLine("Person address changed: {0}", Context.People[0].HomeAddress.Street);
Console.WriteLine("Address entity changed as well: {0}", Context.Addresses[0].Street);
StateHasChanged();
```


## Example

A Todo sample built with BlazorDB is included in the sample project:
Expand Down
21 changes: 21 additions & 0 deletions docs/storageFormat.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ Contents:

* Each file stores the json serialized output of the model. One "row".

### Associations

For objects that contain other object not in the same context, they will be serialized as one object, for example (`Context.cs`):

```
Person with an Address:
{"Id":1,"FirstName":"John","LastName":"Smith","HomeAddress":{"Id":0,"Street":"221 Baker Street","City":"This should be part of the json, since address is not in context (Very long city)"}}
```

For objects that contain object in the same context, they will be serialzied with a "foreign key", for example (`AssociationContext.cs`):

```
Address:
{"Id":1,"Street":"Changed Streeet","City":"This should be a refrence to address since Address exists in the context"}
Person:
{"Id":1,"FirstName":"John","LastName":"Smith","HomeAddress":1}
```


## Metadata

`{FQN context class}-{FQN model class}-{metadata}`
Expand Down
2 changes: 2 additions & 0 deletions src/BlazorDB/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace BlazorDB
{
public static class ServiceCollectionExtensions
{
private static readonly IStorageManager StorageManager = new StorageManager();
private static readonly Type storageContext = typeof(StorageContext);

public static IServiceCollection AddBlazorDB(this IServiceCollection serviceCollection, Action<Options> configure)
Expand All @@ -29,6 +30,7 @@ private static void Scan(IServiceCollection serviceCollection, Assembly assembly

private static void RegisterBlazorDB(IServiceCollection serviceCollection, IEnumerable<Type> types)
{
serviceCollection.AddSingleton<IStorageManager>(StorageManager);
foreach(var contextType in types)
{
StorageManager.LoadContextFromStorageOrCreateNew(serviceCollection, contextType);
Expand Down
11 changes: 11 additions & 0 deletions src/BlazorDB/Storage/IStorageManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.Extensions.DependencyInjection;
using System;

namespace BlazorDB.Storage
{
public interface IStorageManager
{
int SaveContextToLocalStorage(StorageContext context);
void LoadContextFromStorageOrCreateNew(IServiceCollection serviceCollection, Type contextType);
}
}
4 changes: 2 additions & 2 deletions src/BlazorDB/Storage/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ internal static void ItemAddedToContext(string contextTypeName, Type modelType,
BlazorLogger.Logger.GroupEnd();
}

internal static void LoadModelInContext(Type modelType)
internal static void LoadModelInContext(Type modelType, int count)
{
if (!LogDebug) return;
BlazorLogger.Logger.Log($"StorageSet loaded: %c{modelType.Namespace}.{modelType.Name}", blue);
BlazorLogger.Logger.Log($"StorageSet loaded: %c{modelType.Namespace}.{modelType.Name}%c with {count} items", blue, normal);
}

internal static void ItemRemovedFromContext(string contextTypeName, Type modelType)
Expand Down
10 changes: 10 additions & 0 deletions src/BlazorDB/Storage/SerializedModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace BlazorDB.Storage
{
internal class SerializedModel
{
public bool ScanDone { get; set; }
public bool HasAssociation { get; set; }
public string StringModel { get; set; }
public object Model { get; set; }
}
}
Loading

0 comments on commit 886141a

Please sign in to comment.