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

Commit

Permalink
Many association
Browse files Browse the repository at this point in the history
  • Loading branch information
chanan committed May 17, 2018
1 parent 0d58f28 commit 6cce7e7
Show file tree
Hide file tree
Showing 11 changed files with 712 additions and 494 deletions.
34 changes: 28 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ So far the the API is very simplestic but works.
First add a reference to the nuget package:

```
Install-Package BlazorDB -Version 0.0.4
Install-Package BlazorDB -Version 0.0.5
```

or

```
dotnet add package BlazorDB --version 0.0.4
dotnet add package BlazorDB --version 0.0.5
```

Then in Program.cs add Blazor DB to the dependency injection services:
Expand All @@ -39,7 +39,7 @@ 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.
**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:

Expand Down Expand Up @@ -109,8 +109,6 @@ 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.
Expand All @@ -132,7 +130,7 @@ public class AssociationContext : StorageContext

`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:
Therefore, like in `Associations.cshtml` example, changing the Address will Change the Person's HomeAddress:

```
Context.People[0].HomeAddress.Street = "Changed Streeet";
Expand All @@ -141,7 +139,31 @@ Console.WriteLine("Person address changed: {0}", Context.People[0].HomeAddress.S
Console.WriteLine("Address entity changed as well: {0}", Context.Addresses[0].Street);
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`:

```
public List<Address> OtherAddresses { get; set; }
```

This is association is then used in `Associations.cshtml` like so:

```
var person = new Person { FirstName = "Many", LastName = "Test" };
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.

## Example

Expand Down
9 changes: 9 additions & 0 deletions docs/storageFormat.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Contents:

### Associations

**One Association**

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

```
Expand All @@ -28,6 +30,13 @@ Person:
```

**Many Association**

Many associations are stored as an array of ids:

```
{"Id":1,"FirstName":"Many","LastName":"Test","HomeAddress":null,"OtherAddresses":[1,2]}
```

## Metadata

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.4</Version>
<Version>0.0.5</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
Loading

0 comments on commit 6cce7e7

Please sign in to comment.