/// <summary> /// Define a subdocument (or a list of) as a reference /// </summary> public EntityBuilder <T> DbRef <K>(Expression <Func <T, K> > property, string collectionName) { if (string.IsNullOrEmpty(collectionName)) { throw new ArgumentNullException("collectionName"); } return(this.GetProperty(property, (p) => { var typeRef = typeof(K); p.IsDbRef = true; if (Reflection.IsList(typeRef)) { var itemType = typeRef.IsArray ? typeRef.GetElementType() : Reflection.UnderlyingTypeOf(typeRef); var mapper = _mapper.GetPropertyMapper(itemType); RegisterDbRefList(p, collectionName, typeRef, itemType, mapper); } else { var mapper = _mapper.GetPropertyMapper(typeRef); RegisterDbRef(p, collectionName, typeRef, mapper); } })); }
internal EntityBuilder(BsonMapper mapper) { LitePlatform.ThrowIfNotInitialized(); _mapper = mapper; _prop = mapper.GetPropertyMapper(typeof(T)); }
/// <summary> /// Get a field name passing mapper type and returns property type /// </summary> private string GetTypeField(Type type, string property, out Type propertyType) { // lets get mapping bettwen .NET class and BsonDocument var map = _mapper.GetPropertyMapper(type); PropertyMapper prop; if (map.TryGetValue(property, out prop)) { propertyType = prop.PropertyType; return(prop.FieldName); } else { throw LiteException.PropertyNotMapped(property); } }
/// <summary> /// Get a bson string field based on class PropertyInfo using BsonMapper class /// Support Name1.Name2 dotted notation /// </summary> private string GetBsonField(string property) { var parts = property.Split('.'); var fields = new string[parts.Length]; var type = _type; var isdbref = false; PropertyMapper prop; // loop "first.second.last" for (var i = 0; i < parts.Length; i++) { var map = _mapper.GetPropertyMapper(type); var part = parts[i]; if (map.TryGetValue(part, out prop)) { type = prop.PropertyType; fields[i] = prop.FieldName; if (prop.FieldName == "_id" && isdbref) { isdbref = false; fields[i] = "$id"; } // if this property is DbRef, so if next property is _id, change to $id if (prop.IsDbRef) { isdbref = true; } } else { throw LiteException.PropertyNotMapped(property); } } return(string.Join(".", fields)); }
internal EntityBuilder(BsonMapper mapper) { _mapper = mapper; _prop = mapper.GetPropertyMapper(typeof(T)); }