public static DSPResource CreateDSPResource(BsonDocument document, MongoMetadata mongoMetadata, string resourceName, string ownerPrefix = null) { var resourceType = mongoMetadata.ResolveResourceType(resourceName, ownerPrefix); if (resourceType == null) { throw new ArgumentException(string.Format("Unable to resolve resource type {0}", resourceName), "resourceName"); } var resource = new DSPResource(resourceType); foreach (var element in document.Elements) { var resourceProperty = mongoMetadata.ResolveResourceProperty(resourceType, element); if (resourceProperty == null) { continue; } object propertyValue = ConvertBsonValue(element.Value, resourceType, resourceProperty, resourceProperty.Name, mongoMetadata); resource.SetValue(resourceProperty.Name, propertyValue); } AssignNullCollections(resource, resourceType); return(resource); }
private static object ConvertBsonValue(BsonValue bsonValue, ResourceType resourceType, ResourceProperty resourceProperty, string propertyName, MongoMetadata mongoMetadata) { if (bsonValue == null) { return(null); } object propertyValue = null; bool convertValue; if (bsonValue.GetType() == typeof(BsonDocument)) { var bsonDocument = bsonValue.AsBsonDocument; if (IsCsharpNullDocument(bsonDocument)) { convertValue = false; } else { propertyValue = CreateDSPResource(bsonDocument, mongoMetadata, propertyName, MongoMetadata.GetQualifiedTypePrefix(resourceType.Name)); convertValue = true; } } else if (bsonValue.GetType() == typeof(BsonArray)) { var bsonArray = bsonValue.AsBsonArray; if (bsonArray != null && bsonArray.Count > 0) { propertyValue = ConvertBsonArray(bsonArray, resourceType, propertyName, mongoMetadata); } convertValue = false; } else if (bsonValue.GetType() == typeof(BsonNull) && resourceProperty.Kind == ResourcePropertyKind.Collection) { propertyValue = ConvertBsonArray(new BsonArray(0), resourceType, propertyName, mongoMetadata); convertValue = false; } else { propertyValue = ConvertRawValue(bsonValue); convertValue = true; } if (propertyValue != null && convertValue) { var propertyType = resourceProperty.ResourceType.InstanceType; Type underlyingNonNullableType = Nullable.GetUnderlyingType(resourceProperty.ResourceType.InstanceType); if (underlyingNonNullableType != null) { propertyType = underlyingNonNullableType; } propertyValue = Convert.ChangeType(propertyValue, propertyType); } return(propertyValue); }
/// <summary>Constructor</summary> public MongoDataServiceBase(string connectionString, MongoConfiguration mongoConfiguration) { this.connectionString = connectionString; this.mongoConfiguration = mongoConfiguration; this.createUpdateProvider = () => new MongoDSPUpdateProvider(this.connectionString, this.CurrentDataSource, this.mongoMetadata); ResetDataContext = x => { this.mongoMetadata = new MongoMetadata(x, this.mongoConfiguration == null ? null : this.mongoConfiguration.MetadataBuildStrategy); MongoDataServiceBase <T, Q> .context = this.CreateContext(x); }; ResetDataContext(connectionString); }
public static BsonDocument CreateBSonDocument(DSPResource resource, MongoMetadata mongoMetadata, string resourceName) { var document = new BsonDocument(); var resourceSet = mongoMetadata.ResolveResourceSet(resourceName); if (resourceSet != null) { foreach (var property in resourceSet.ResourceType.Properties) { var propertyValue = resource.GetValue(property.Name); if (propertyValue != null) { document.Set(property.Name, BsonValue.Create(propertyValue)); } } } return document; }
public static BsonDocument CreateBSonDocument(DSPResource resource, MongoMetadata mongoMetadata, string resourceName) { var document = new BsonDocument(); var resourceSet = mongoMetadata.ResolveResourceSet(resourceName); if (resourceSet != null) { foreach (var property in resourceSet.ResourceType.Properties) { var propertyValue = resource.GetValue(property.Name); if (propertyValue != null) { document.Set(property.Name, BsonValue.Create(propertyValue)); } } } return(document); }
private static object ConvertBsonArray(BsonArray bsonArray, ResourceType resourceType, string propertyName, MongoMetadata mongoMetadata) { if (bsonArray == null || bsonArray.Count == 0) { return(new object[0]); } bool isDocument = false; int nonNullItemCount = 0; for (int index = 0; index < bsonArray.Count; index++) { if (bsonArray[index] != BsonNull.Value) { if (bsonArray[index].GetType() == typeof(BsonDocument)) { isDocument = true; } ++nonNullItemCount; } } object[] propertyValue = isDocument ? new DSPResource[nonNullItemCount] : new object[nonNullItemCount]; int valueIndex = 0; for (int index = 0; index < bsonArray.Count; index++) { if (bsonArray[index] != BsonNull.Value) { if (isDocument) { propertyValue[valueIndex++] = CreateDSPResource(bsonArray[index].AsBsonDocument, mongoMetadata, propertyName, MongoMetadata.GetQualifiedTypePrefix(resourceType.Name)); } else { propertyValue[valueIndex++] = ConvertRawValue(bsonArray[index]); } } } return(propertyValue); }
public static DSPResource CreateDSPResource(BsonDocument document, MongoMetadata mongoMetadata, string resourceName, string ownerPrefix = null) { var resourceType = mongoMetadata.ResolveResourceType(resourceName, ownerPrefix); if (resourceType == null) throw new ArgumentException(string.Format("Unable to resolve resource type {0}", resourceName), "resourceName"); var resource = new DSPResource(resourceType); foreach (var element in document.Elements) { var resourceProperty = mongoMetadata.ResolveResourceProperty(resourceType, element); if (resourceProperty == null) continue; object propertyValue = ConvertBsonValue(element.Value, resourceType, resourceProperty, resourceProperty.Name, mongoMetadata); resource.SetValue(resourceProperty.Name, propertyValue); } AssignNullCollections(resource, resourceType); return resource; }
private static object ConvertBsonArray(BsonArray bsonArray, ResourceType resourceType, string propertyName, MongoMetadata mongoMetadata) { if (bsonArray == null || bsonArray.Count == 0) { return new object[0]; } bool isDocument = false; int nonNullItemCount = 0; for (int index = 0; index < bsonArray.Count; index++) { if (bsonArray[index] != BsonNull.Value) { if (bsonArray[index].GetType() == typeof(BsonDocument)) isDocument = true; ++nonNullItemCount; } } object[] propertyValue = isDocument ? new DSPResource[nonNullItemCount] : new object[nonNullItemCount]; int valueIndex = 0; for (int index = 0; index < bsonArray.Count; index++) { if (bsonArray[index] != BsonNull.Value) { if (isDocument) { var bsonDoc = new BsonDocument("__raw_value", bsonArray[index]); propertyValue[valueIndex++] = CreateDSPResource(bsonDoc, mongoMetadata, propertyName, MongoMetadata.GetQualifiedTypePrefix(resourceType.Name)); } else { propertyValue[valueIndex++] = ConvertRawValue(bsonArray[index]); } } } return propertyValue; }
public MongoDSPUpdateProvider(string connectionString, DSPContext dataContext, MongoMetadata mongoMetadata) : base(dataContext, mongoMetadata.CreateDSPMetadata()) { this.connectionString = connectionString; this.mongoMetadata = mongoMetadata; }
public static DSPResource CreateDSPResource(BsonDocument document, MongoMetadata mongoMetadata, string resourceName, string ownerPrefix = null) { var resourceType = mongoMetadata.ResolveResourceType(resourceName, ownerPrefix); if (resourceType == null) throw new ArgumentException(string.Format("Unable to resolve resource type {0}", resourceName), "resourceName"); var resource = new DSPResource(resourceType); foreach (var element in document.Elements) { var resourceProperty = mongoMetadata.ResolveResourceProperty(resourceType, element); if (resourceProperty == null) continue; string propertyName = null; object propertyValue = null; if (MongoMetadata.IsObjectId(element)) { propertyName = MongoMetadata.MappedObjectIdName; propertyValue = element.Value.RawValue.ToString(); } else if (element.Value.GetType() == typeof(BsonDocument)) { propertyName = element.Name; propertyValue = CreateDSPResource(element.Value.AsBsonDocument, mongoMetadata, element.Name, MongoMetadata.GetComplexTypePrefix(resourceType.Name)); } else if (element.Value.GetType() == typeof(BsonArray)) { var bsonArray = element.Value.AsBsonArray; if (bsonArray != null && bsonArray.Count > 0) { propertyName = element.Name; int nonNullItemCount = 0; for (int index = 0; index < bsonArray.Count; index++) { if (bsonArray[index] != BsonNull.Value) ++nonNullItemCount; } var valueArray = new DSPResource[nonNullItemCount]; int valueIndex = 0; for (int index = 0; index < bsonArray.Count; index++) { if (bsonArray[index] != BsonNull.Value) { valueArray[valueIndex++] = CreateDSPResource(bsonArray[index].AsBsonDocument, mongoMetadata, element.Name, MongoMetadata.GetCollectionTypePrefix(resourceType.Name)); } } propertyValue = valueArray; } } else { propertyName = element.Name; if (element.Value.RawValue != null) { switch (element.Value.BsonType) { case BsonType.DateTime: propertyValue = UnixEpoch + TimeSpan.FromMilliseconds(element.Value.AsBsonDateTime.MillisecondsSinceEpoch); break; default: propertyValue = element.Value.RawValue; break; } } else { switch (element.Value.BsonType) { case BsonType.Binary: propertyValue = element.Value.AsBsonBinaryData.Bytes; break; default: propertyValue = element.Value.RawValue; break; } } } if (propertyValue != null && element.Value.GetType() != typeof(BsonArray)) { propertyValue = Convert.ChangeType(propertyValue, resourceProperty.ResourceType.InstanceType); } resource.SetValue(propertyName, propertyValue); } return resource; }
private static object ConvertBsonValue(BsonValue bsonValue, ResourceType resourceType, ResourceProperty resourceProperty, string propertyName, MongoMetadata mongoMetadata) { if (bsonValue == null) return null; object propertyValue = null; bool convertValue; if (bsonValue.GetType() == typeof(BsonDocument)) { var bsonDocument = bsonValue.AsBsonDocument; if (IsCsharpNullDocument(bsonDocument)) { convertValue = false; } else { propertyValue = CreateDSPResource(bsonDocument, mongoMetadata, propertyName, MongoMetadata.GetQualifiedTypePrefix(resourceType.Name)); convertValue = true; } } else if (bsonValue.GetType() == typeof(BsonArray)) { var bsonArray = bsonValue.AsBsonArray; if (bsonArray != null && bsonArray.Count > 0) propertyValue = ConvertBsonArray(bsonArray, resourceType, propertyName, mongoMetadata); convertValue = false; } else if (bsonValue.GetType() == typeof(BsonNull) && resourceProperty.Kind == ResourcePropertyKind.Collection) { propertyValue = ConvertBsonArray(new BsonArray(0), resourceType, propertyName, mongoMetadata); convertValue = false; } else { propertyValue = ConvertRawValue(bsonValue); convertValue = true; } if (propertyValue != null && convertValue) { var propertyType = resourceProperty.ResourceType.InstanceType; Type underlyingNonNullableType = Nullable.GetUnderlyingType(resourceProperty.ResourceType.InstanceType); if (underlyingNonNullableType != null) { propertyType = underlyingNonNullableType; } propertyValue = Convert.ChangeType(propertyValue, propertyType); } return propertyValue; }
public MongoDSPUpdateProvider(string connectionString, DSPContext dataContext, MongoMetadata mongoMetadata) : base(dataContext, mongoMetadata.CreateDSPMetadata()) { this.connectionString = connectionString; this.mongoMetadata = mongoMetadata; }
public static void ResetDSPMetadata() { MongoMetadata.ResetDSPMetadata(); }
public ResourceProperty ResolveResourceProperty(ResourceType resourceType, BsonElement element) { var propertyName = MongoMetadata.GetResourcePropertyName(element, resourceType.ResourceTypeKind); return(ResolveResourceProperty(resourceType, propertyName)); }
public ResourceType ResolveResourceType(string resourceName, string ownerPrefix = null) { ResourceType resourceType; var qualifiedResourceName = string.IsNullOrEmpty(ownerPrefix) ? resourceName : MongoMetadata.GetQualifiedTypeName(ownerPrefix, resourceName); this.instanceMetadataCache.TryResolveResourceType(GetQualifiedPropertyName(MongoMetadata.RootNamespace, qualifiedResourceName), out resourceType); return(resourceType); }