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

Commit bebdd79

Browse files
committed
v0.1.0
1 parent b82bff8 commit bebdd79

File tree

6 files changed

+17
-38
lines changed

6 files changed

+17
-38
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,13 @@ StateHasChanged();
132132
```
133133
### One to Many, Many to Many Association
134134

135-
Define a Many association by adding a property of type `List<>` to the association. For example in `Person.cs`:
135+
Define a "One" association by adding a property of the other model. For example in `Person.cs`:
136+
137+
```
138+
public Address HomeAddress { get; set; }
139+
```
140+
141+
Define a "Many" association by adding a property of type `List<>` to the association. For example in `Person.cs`:
136142

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

143149
```
144150
var person = new Person { FirstName = "Many", LastName = "Test" };
151+
person.HomeAddress = new Address { Street = "221 Baker Streeet", City = "This should be a refrence to address since Address exists in the context" };
145152
var address1 = new Address { Street = "Many test 1", City = "Saved as a reference" };
146153
var address2 = new Address { Street = "Many test 2", City = "Saved as a reference" };
147154
person.OtherAddresses = new List<Address> { address1, address2 };
148155
Context.People.Add(person);
149-
Context.Addresses.Add(address1);
150-
Context.Addresses.Add(address2);
151156
Context.SaveChanges();
152157
StateHasChanged();
153158
```
154159

155160
### Maintaining Associations
156161

157-
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.
162+
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.
163+
164+
**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.
158165

159166
## Example
160167

docs/storageFormat.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Many associations are stored as an array of ids:
4545
Contents:
4646

4747
* Guids - List of persisted guids
48+
* MaxId - The last id of the StorageSet
4849

4950
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.
5051

src/BlazorDB/BlazorDB.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<BlazorLinkOnBuild>false</BlazorLinkOnBuild>
88
<LangVersion>7.3</LangVersion>
99
<PackageId>BlazorDB</PackageId>
10-
<Version>0.0.5</Version>
10+
<Version>0.1.0</Version>
1111
<Authors>Chanan Braunstein</Authors>
1212
<Title>Blazor localStorage Database</Title>
1313
<Description>In memory, persisted to localstorage, database for .net Blazor browser framework</Description>

src/BlazorDB/Storage/StorageManagerSave.cs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public int SaveContextToLocalStorage(StorageContext context)
1313
{
1414
var total = 0;
1515
var contextType = context.GetType();
16-
//Logger.ContextSaved(contextType);
16+
Logger.ContextSaved(contextType);
1717
var storageSets = StorageManagerUtil.GetStorageSets(contextType);
1818
var metadataMap = LoadMetadataList(context, storageSets, contextType);
1919
total = SaveStorageSets(context, total, contextType, storageSets, metadataMap);
20-
//Logger.EndGroup();
20+
Logger.EndGroup();
2121
return total;
2222
}
2323

@@ -83,7 +83,6 @@ private static void EnsureAllAssociationsHaveIds(StorageContext context, object
8383

8484
private static void EnsureManyAssociationHasId(StorageContext context, object listObject, PropertyInfo prop, List<PropertyInfo> storageSets, IReadOnlyDictionary<string, Metadata> metadataMap)
8585
{
86-
Console.WriteLine("listObject: {0}", listObject);
8786
var method = listObject.GetType().GetMethod(StorageManagerUtil.GetEnumerator);
8887
var enumerator = (IEnumerator)method.Invoke(listObject, new object[] { });
8988
while (enumerator.MoveNext())
@@ -98,15 +97,11 @@ private static void EnsureOneAssociationHasId(StorageContext context, object ass
9897
var idProp = GetIdProperty(associatedModel);
9998
var id = Convert.ToString(idProp.GetValue(associatedModel));
10099
var metadata = metadataMap[Util.GetFullyQualifiedTypeName(propType)];
101-
Console.WriteLine("metadata: {0}", metadata.ModelName);
102-
Console.WriteLine("maxId: {0}", metadata.MaxId);
103-
Console.WriteLine("id: {0}", id);
104100
if (id == "0")
105101
{
106102
metadata.MaxId = metadata.MaxId + 1;
107103
SaveAssociationModel(context, associatedModel, propType, storageSets, metadata.MaxId);
108104
}
109-
Console.WriteLine("maxId: {0}", metadata.MaxId);
110105
}
111106

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

162-
//TODO: Move this to metadata
163-
private static int GetMaxId(IEnumerator enumerator)
164-
{
165-
var max = 0;
166-
while (enumerator.MoveNext())
167-
{
168-
var model = enumerator.Current;
169-
var id = GetId(model);
170-
if (id > max) max = id;
171-
}
172-
return max;
173-
}
174-
175157
private static void DeleteOldModelsFromStorage(Metadata metadata, string storageTableName)
176158
{
177159
foreach (var guid in metadata.Guids)
@@ -216,25 +198,18 @@ private static string FixOneAssociation(object model, PropertyInfo prop, string
216198
var associatedModel = prop.GetValue(model);
217199
var idProp = GetIdProperty(associatedModel);
218200
var id = Convert.ToString(idProp.GetValue(associatedModel));
219-
Console.WriteLine("id: {0}", id);
220201
var serializedItem = JsonUtil.Serialize(associatedModel);
221-
Console.WriteLine("serializedItem: {0}", serializedItem);
222202
result = ReplaceModelWithId(result, serializedItem, id);
223203
return result;
224204
}
225205

226206
private static int SaveAssociationModel(StorageContext context, object associatedModel, Type propType, IEnumerable<PropertyInfo> storageSets, int id)
227207
{
228-
Console.WriteLine("SaveAssociationModel id: {0}", id);
229-
Console.WriteLine("associatedModel: {0}", associatedModel);
230-
Console.WriteLine("propType: {0}", propType);
231208
var q = from p in storageSets
232209
where p.PropertyType.GetGenericArguments()[0] == propType
233210
select p;
234211
var storeageSetProp = q.Single();
235-
Console.WriteLine("storeageSetProp: {0}", storeageSetProp);
236212
var storeageSet = storeageSetProp.GetValue(context);
237-
Console.WriteLine("storeageSet: {0}", storeageSet);
238213
var listProp = storeageSet.GetType().GetProperty(StorageManagerUtil.List, StorageManagerUtil.Flags);
239214
var list = listProp.GetValue(storeageSet);
240215
var addMethod = list.GetType().GetMethod(StorageManagerUtil.Add);

src/Sample/Models/Context.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ namespace Sample.Models
55
public class Context : StorageContext
66
{
77
public StorageSet<Person> People { get; set; }
8-
public StorageSet<Address> Addresses { get; set; }
98
}
109
}

src/Sample/Pages/Associations.cshtml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
var address = new Address { Street = "221 Baker Streeet", City = "This should be a refrence to address since Address exists in the context" };
4848
person.HomeAddress = address;
4949
Context.People.Add(person);
50-
//Context.Addresses.Add(address); // Shouldn't need this line
5150
Context.SaveChanges();
5251
StateHasChanged();
5352
}
@@ -68,15 +67,13 @@
6867
var address2 = new Address { Street = "Many test 2", City = "Saved as a reference" };
6968
person.OtherAddresses = new List<Address> { address1, address2 };
7069
Context.People.Add(person);
71-
Context.Addresses.Add(address1);
72-
Context.Addresses.Add(address2);
7370
Context.SaveChanges();
7471
StateHasChanged();
7572
}
7673

7774
void OnLoadPerson(UIMouseEventArgs e)
7875
{
79-
_person = Context.People[0];
80-
StateHasChanged();
76+
_person = Context.People[1];
77+
StateHasChanged();
8178
}
8279
}

0 commit comments

Comments
 (0)