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

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
chanan committed May 31, 2018
1 parent b82bff8 commit bebdd79
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 38 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ StateHasChanged();
```
### One to Many, Many to Many Association

Define a Many association by adding a property of type `List<>` to the association. For example in `Person.cs`:
Define a "One" association by adding a property of the other model. For example in `Person.cs`:

```
public Address HomeAddress { get; set; }
```

Define a "Many" association by adding a property of type `List<>` to the association. For example in `Person.cs`:

```
public List<Address> OtherAddresses { get; set; }
Expand All @@ -142,19 +148,20 @@ This is association is then used in `Associations.cshtml` like so:

```
var person = new Person { FirstName = "Many", LastName = "Test" };
person.HomeAddress = new Address { Street = "221 Baker Streeet", City = "This should be a refrence to address since Address exists in the context" };
var address1 = new Address { Street = "Many test 1", City = "Saved as a reference" };
var address2 = new Address { Street = "Many test 2", City = "Saved as a reference" };
person.OtherAddresses = new List<Address> { address1, address2 };
Context.People.Add(person);
Context.Addresses.Add(address1);
Context.Addresses.Add(address2);
Context.SaveChanges();
StateHasChanged();
```

### Maintaining Associations

Currently, associations are not maintained automatically. As in the example above, Person and Address need both be added to the context. In the future, BlazorDB may maintain those automatically.
As you can see in the example above BlazorDB will detect associations added to the model so no need to add them to the Context explicitly. In the example above, the address objects do not need to be explicitly added to the context, instead they are persisted when the person object is added and `SaveChanges()` is called.

**Note:** At this time removing/deleting is not done automatically and needs to be done manually. A future update of BlazorDB will handle deletions properly.

## Example

Expand Down
1 change: 1 addition & 0 deletions docs/storageFormat.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Many associations are stored as an array of ids:
Contents:

* Guids - List of persisted guids
* MaxId - The last id of the StorageSet

Initial implementation will regenerate the guid on every `SaveChanges()` and the list in the metadata table. Future implementation might store metadata about the model in the model value itself, so the guid will be loaded into memory and won't be regenerated.

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.5</Version>
<Version>0.1.0</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
29 changes: 2 additions & 27 deletions src/BlazorDB/Storage/StorageManagerSave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public int SaveContextToLocalStorage(StorageContext context)
{
var total = 0;
var contextType = context.GetType();
//Logger.ContextSaved(contextType);
Logger.ContextSaved(contextType);
var storageSets = StorageManagerUtil.GetStorageSets(contextType);
var metadataMap = LoadMetadataList(context, storageSets, contextType);
total = SaveStorageSets(context, total, contextType, storageSets, metadataMap);
//Logger.EndGroup();
Logger.EndGroup();
return total;
}

Expand Down Expand Up @@ -83,7 +83,6 @@ private static void EnsureAllAssociationsHaveIds(StorageContext context, object

private static void EnsureManyAssociationHasId(StorageContext context, object listObject, PropertyInfo prop, List<PropertyInfo> storageSets, IReadOnlyDictionary<string, Metadata> metadataMap)
{
Console.WriteLine("listObject: {0}", listObject);
var method = listObject.GetType().GetMethod(StorageManagerUtil.GetEnumerator);
var enumerator = (IEnumerator)method.Invoke(listObject, new object[] { });
while (enumerator.MoveNext())
Expand All @@ -98,15 +97,11 @@ private static void EnsureOneAssociationHasId(StorageContext context, object ass
var idProp = GetIdProperty(associatedModel);
var id = Convert.ToString(idProp.GetValue(associatedModel));
var metadata = metadataMap[Util.GetFullyQualifiedTypeName(propType)];
Console.WriteLine("metadata: {0}", metadata.ModelName);
Console.WriteLine("maxId: {0}", metadata.MaxId);
Console.WriteLine("id: {0}", id);
if (id == "0")
{
metadata.MaxId = metadata.MaxId + 1;
SaveAssociationModel(context, associatedModel, propType, storageSets, metadata.MaxId);
}
Console.WriteLine("maxId: {0}", metadata.MaxId);
}

private static void EnsureAllModelsHaveIds(object storageSetValue, Type modelType, IReadOnlyDictionary<string, Metadata> metadataMap)
Expand Down Expand Up @@ -159,19 +154,6 @@ private static List<Guid> SaveModels(object storageSetValue, Type modelType, str
return guids;
}

//TODO: Move this to metadata
private static int GetMaxId(IEnumerator enumerator)
{
var max = 0;
while (enumerator.MoveNext())
{
var model = enumerator.Current;
var id = GetId(model);
if (id > max) max = id;
}
return max;
}

private static void DeleteOldModelsFromStorage(Metadata metadata, string storageTableName)
{
foreach (var guid in metadata.Guids)
Expand Down Expand Up @@ -216,25 +198,18 @@ private static string FixOneAssociation(object model, PropertyInfo prop, string
var associatedModel = prop.GetValue(model);
var idProp = GetIdProperty(associatedModel);
var id = Convert.ToString(idProp.GetValue(associatedModel));
Console.WriteLine("id: {0}", id);
var serializedItem = JsonUtil.Serialize(associatedModel);
Console.WriteLine("serializedItem: {0}", serializedItem);
result = ReplaceModelWithId(result, serializedItem, id);
return result;
}

private static int SaveAssociationModel(StorageContext context, object associatedModel, Type propType, IEnumerable<PropertyInfo> storageSets, int id)
{
Console.WriteLine("SaveAssociationModel id: {0}", id);
Console.WriteLine("associatedModel: {0}", associatedModel);
Console.WriteLine("propType: {0}", propType);
var q = from p in storageSets
where p.PropertyType.GetGenericArguments()[0] == propType
select p;
var storeageSetProp = q.Single();
Console.WriteLine("storeageSetProp: {0}", storeageSetProp);
var storeageSet = storeageSetProp.GetValue(context);
Console.WriteLine("storeageSet: {0}", storeageSet);
var listProp = storeageSet.GetType().GetProperty(StorageManagerUtil.List, StorageManagerUtil.Flags);
var list = listProp.GetValue(storeageSet);
var addMethod = list.GetType().GetMethod(StorageManagerUtil.Add);
Expand Down
1 change: 0 additions & 1 deletion src/Sample/Models/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ namespace Sample.Models
public class Context : StorageContext
{
public StorageSet<Person> People { get; set; }
public StorageSet<Address> Addresses { get; set; }
}
}
7 changes: 2 additions & 5 deletions src/Sample/Pages/Associations.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
var address = new Address { Street = "221 Baker Streeet", City = "This should be a refrence to address since Address exists in the context" };
person.HomeAddress = address;
Context.People.Add(person);
//Context.Addresses.Add(address); // Shouldn't need this line
Context.SaveChanges();
StateHasChanged();
}
Expand All @@ -68,15 +67,13 @@
var address2 = new Address { Street = "Many test 2", City = "Saved as a reference" };
person.OtherAddresses = new List<Address> { address1, address2 };
Context.People.Add(person);
Context.Addresses.Add(address1);
Context.Addresses.Add(address2);
Context.SaveChanges();
StateHasChanged();
}

void OnLoadPerson(UIMouseEventArgs e)
{
_person = Context.People[0];
StateHasChanged();
_person = Context.People[1];
StateHasChanged();
}
}

0 comments on commit bebdd79

Please sign in to comment.