Skip to content

Commit d2c7bfb

Browse files
committed
Update readme docs
1 parent 5cc5910 commit d2c7bfb

File tree

1 file changed

+98
-6
lines changed

1 file changed

+98
-6
lines changed

README.md

Lines changed: 98 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ MongoFramework tries to bring some of the nice features from Entity Framework in
1616
Some of the major features include:
1717
- Entity mapping for collections, IDs and properties through attributes
1818
- Indexing through attributes (including text and geospatial)
19+
- Fluent mapping builder
1920
- Entity change tracking
2021
- Changeset support (allowing for queuing multiple DB updates to run at once)
2122
- Diff-updates (only _changes_ to an entity to be written)
@@ -43,20 +44,75 @@ Supports profiling database reads and writes, pushing the data into [MiniProfile
4344

4445
## Documentation
4546

46-
### Core Entity Mapping
47-
The core mapping of entities and their properties is automatic however there are certain attributes you can apply to your properties to alter this behaviour.
48-
These attributes (and a few others) are part of the `System.ComponentModel.Annotations` package.
47+
### Entity Mapping Basics
48+
The core mapping of entities and their properties is automatic however you have the choice of using the fluent mapping builder or certain attributes to alter this behaviour.
49+
50+
**Fluent Mapping**
51+
```csharp
52+
using MongoFramework;
53+
54+
public class MyEntity
55+
{
56+
public string Id { get; set; }
57+
public string Name { get; set; }
58+
public string Description { get; set; }
59+
}
60+
61+
public class MyContext : MongoDbContext
62+
{
63+
public MyContext(IMongoDbConnection connection) : base(connection) { }
64+
public MongoDbSet<MyEntity> MyEntities { get; set; }
65+
66+
protected override void OnConfigureMapping(MappingBuilder mappingBuilder)
67+
{
68+
mappingBuilder.Entity<MyEntity>()
69+
.HasProperty(m => m.Name, b => b.HasElementName("MappedName"))
70+
.ToCollection("MyCustomEntities");
71+
}
72+
}
73+
```
74+
75+
**Attribute Mapping**
76+
```csharp
77+
using MongoFramework;
78+
using System.ComponentModel.DataAnnotations;
79+
80+
[Table("MyCustomEntities")]
81+
public class MyEntity
82+
{
83+
public string Id { get; set; }
84+
[Column("MappedName")]
85+
public string Name { get; set; }
86+
public string Description { get; set; }
87+
}
88+
89+
public class MyContext : MongoDbContext
90+
{
91+
public MyContext(IMongoDbConnection connection) : base(connection) { }
92+
public MongoDbSet<MyEntity> MyEntities { get; set; }
93+
}
94+
```
95+
96+
For attribute mapping, many of the core attributes are part of the `System.ComponentModel.Annotations` package.
4997

5098
|Attribute|Description|
5199
|---------|-----------|
52100
|`[Table("MyFancyEntity", Schema = "MyNamespace")]`|Map the Entity to the collection specified. When a schema is specified, it is prefixed onto the name with a "." (dot) separator.|
53101
|`[Key]`|Map the property as the "Id" for the entity. Only required if your key doesn't have a common name like "Id" etc.|
54-
|`[NotMapped]`|Unmaps the property from the entity when reading/writing.|
102+
|`[NotMapped]`|When applied to a class or property, skips mapping when reading/writing.|
55103
|`[Column("NewColumnName")]`|Remaps the property with the specified name when reading/writing.|
56104

57105
### Indexing
58-
MongoFramework supports indexing specified through the `IndexAttribute` class. This is applied to the properties you want indexed and will apply the changes to the database when the context is saved.
106+
MongoFramework supports indexing specified through both the fluent mapping builder and the `IndexAttribute`.
107+
This is applied to the properties you want indexed and will apply the changes to the database when the context is saved.
59108

109+
**Fluent Mapping**
110+
```csharp
111+
mappingBuilder.Entity<IndexExample>()
112+
.HasIndex(e => e.EmailAddress, b => b.HasName("Email").IsDescending(false));
113+
```
114+
115+
**Attribute Mapping**
60116
```csharp
61117
public class IndexExample
62118
{
@@ -69,6 +125,7 @@ public class IndexExample
69125
}
70126
```
71127

128+
72129
The following variations of indexes are supported across various property types:
73130
- [Single field](https://docs.mongodb.com/manual/core/index-single/)
74131
- [Compound](https://docs.mongodb.com/manual/core/index-compound/#compound-indexes)
@@ -77,9 +134,44 @@ The following variations of indexes are supported across various property types:
77134
To support compound indexes, define indexes with the same name across multiple properties.
78135
When doing this, you will want to control the order of the individual items in the compound index which is available through the `IndexPriority` property on the attribute.
79136

137+
For fluent mapping, you can specify multiple indexes in one declaration to combine them and handle nesting for complex data structures including through arrays.
138+
139+
```csharp
140+
public class TestModelBase
141+
{
142+
public string Id { get; set; }
143+
public string Name { get; set; }
144+
public IEnumerable<NestedModel> ManyOfThem { get; set; }
145+
}
146+
147+
public class TestModel : TestModelBase
148+
{
149+
public Dictionary<string, object> ExtraElements { get; set; }
150+
public string OtherName { get; set; }
151+
public int SomethingIndexable { get; set; }
152+
public NestedModelBase OneOfThem { get; set; }
153+
}
154+
155+
public class NestedModelBase
156+
{
157+
public string Description { get; set; }
158+
}
159+
160+
mappingBuilder.Entity<TestModel>()
161+
.HasIndex(m => new
162+
{
163+
m.SomethingIndexable,
164+
m.OneOfThem.Description,
165+
m.ManyOfThem.First().AnotherThingIndexable
166+
}, b => {
167+
b.HasName("MyIndex")
168+
.IsDescending(true, false, false)
169+
});
170+
```
171+
80172
#### Special Index Types
81173
MongoFramework supports [Text](https://docs.mongodb.com/manual/core/index-text/) and [2dSphere](https://docs.mongodb.com/manual/core/2dsphere/) special indexes.
82-
These special index types are selected through the `IndexType` property on the Index attribute.
174+
For attribute mapping, these special index types are selected through the `IndexType` property on the `IndexAttribute`.
83175

84176
Please consult MongoDB's documentation on when the indexes are appropriate and their restrictions.
85177

0 commit comments

Comments
 (0)