diff --git a/README.md b/README.md
index 8befd43..34e88cd 100644
--- a/README.md
+++ b/README.md
@@ -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
OtherAddresses { get; set; }
@@ -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 { 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
diff --git a/docs/storageFormat.md b/docs/storageFormat.md
index bc2928d..2a96b7a 100644
--- a/docs/storageFormat.md
+++ b/docs/storageFormat.md
@@ -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.
diff --git a/src/BlazorDB/BlazorDB.csproj b/src/BlazorDB/BlazorDB.csproj
index 27d0ed1..2175514 100644
--- a/src/BlazorDB/BlazorDB.csproj
+++ b/src/BlazorDB/BlazorDB.csproj
@@ -7,7 +7,7 @@
false
7.3
BlazorDB
- 0.0.5
+ 0.1.0
Chanan Braunstein
Blazor localStorage Database
In memory, persisted to localstorage, database for .net Blazor browser framework
diff --git a/src/BlazorDB/Storage/StorageManagerSave.cs b/src/BlazorDB/Storage/StorageManagerSave.cs
index 46e6cca..ea1943e 100644
--- a/src/BlazorDB/Storage/StorageManagerSave.cs
+++ b/src/BlazorDB/Storage/StorageManagerSave.cs
@@ -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;
}
@@ -83,7 +83,6 @@ private static void EnsureAllAssociationsHaveIds(StorageContext context, object
private static void EnsureManyAssociationHasId(StorageContext context, object listObject, PropertyInfo prop, List storageSets, IReadOnlyDictionary metadataMap)
{
- Console.WriteLine("listObject: {0}", listObject);
var method = listObject.GetType().GetMethod(StorageManagerUtil.GetEnumerator);
var enumerator = (IEnumerator)method.Invoke(listObject, new object[] { });
while (enumerator.MoveNext())
@@ -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 metadataMap)
@@ -159,19 +154,6 @@ private static List 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)
@@ -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 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);
diff --git a/src/Sample/Models/Context.cs b/src/Sample/Models/Context.cs
index bc309ff..608e96e 100644
--- a/src/Sample/Models/Context.cs
+++ b/src/Sample/Models/Context.cs
@@ -5,6 +5,5 @@ namespace Sample.Models
public class Context : StorageContext
{
public StorageSet People { get; set; }
- public StorageSet Addresses { get; set; }
}
}
diff --git a/src/Sample/Pages/Associations.cshtml b/src/Sample/Pages/Associations.cshtml
index f8db1d5..5da0f53 100644
--- a/src/Sample/Pages/Associations.cshtml
+++ b/src/Sample/Pages/Associations.cshtml
@@ -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();
}
@@ -68,15 +67,13 @@
var address2 = new Address { Street = "Many test 2", City = "Saved as a reference" };
person.OtherAddresses = new List { 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();
}
}
\ No newline at end of file