protected virtual TENTITY DoSaveToDatabase( TENTITY entity, List <string> includes, IObjectsDataSet context, Parameters parameters) { // Ensure got a dataset if (entity.ObjectsDataSet == null) { entity.ObjectsDataSet = ApplicationSettings.Resolve <IObjectsDataSet>(); } // Ensure entity is in its own dataset var self = entity.ObjectsDataSet.GetObject(entity); if (self == null) { entity.ObjectsDataSet.AddObject(entity); } // Note: We can skip objects marked for deletion, because before arriving here, the delete resolution algorithm will already have processed them // Wee just need to remove the marked-for-deletion objects so that they aren't mapped or returned to caller foreach (var deletedObj in entity.ObjectsDataSet.GetObjectsMarkedForDeletion().ToArray()) { entity.ObjectsDataSet.RemoveObject(deletedObj); // Remove from the context too if (context != null) { context.RemoveObject(deletedObj); } } // Get data mapping setting from the parameters (deep is the default) // Deep mapping means we'll explore all relations, else will only treat the specified entity (i.e. a 'flat' single entity save) bool deepMapping = parameters == null || !parameters.ContainsKey(ParameterKeys.DeepDataMapping) || (bool)parameters[ParameterKeys.DeepDataMapping] == true; // entity returned from SaveToDatabaseThroughORM can be null (e.g. if nothing dirty/new in the saveset, or if deleted) entity = SaveToDatabaseThroughORM(entity, deepMapping); // for the refetch, security can be skipped if no related data requested (since we just wrote it, it's just a flat re-sync) bool canRefetchSkipSecurity = includes == null || !includes.Any() || ParameterKeys.IsOptionEnabled(parameters, ParameterKeys.ORMSaveRefetchSkipSecurity); return(entity == null ? null : Get(entity, includes: includes, parameters: parameters, skipSecurity: canRefetchSkipSecurity)); }
// filterExpression is used to filter data, when filter is statically known. dynamicFilterExpression is used for dynamic filtering, when filter is not known at compile time. Both can be used at the same time protected override DataObjectCollection <UserProfileDataObject> DoGetCollectionFromDatabase( LambdaExpression filterExpression, string dynamicFilterExpression, object[] dynamicFilterArguments, string orderByPredicate, int pageNumber, int pageSize, List <string> includes, IObjectsDataSet context, Parameters parameters) { var query = Query(dynamicFilterExpression, dynamicFilterArguments, filterExpression); if (!String.IsNullOrEmpty(orderByPredicate)) { query = query.OrderBy(orderByPredicate); } // Normal (default generated) behaviour is: // includes not treated by orm but with dispatcher mechanism to allow for proper security and extension calls on all objects // But the following allows custom implementations to prefetch associations ApplicationSettings.TryResolve <IPrefetch <ORMUserProfile> >()?.Fetch(query, includes, parameters); // Do Paging (unless late-paging option is enabled) if (!ParameterKeys.IsOptionEnabled(parameters, ParameterKeys.DataProviderGetCollectionLatePaging)) { if (pageNumber != 0 && pageSize > 0) { query = query.Skip((pageNumber - 1) * pageSize).Take(pageSize); } } var dataset = ApplicationSettings.Resolve <IObjectsDataSet>(); var collection = query.Select(x => x.ToDataObject(dataset)).Cast <UserProfileDataObject>().ToList(); return(new DataObjectCollection <UserProfileDataObject>(collection)); }