public static CachedMetadata GetInitializedMetadata(BaseDomainService domainService) { return(_metadataCache.GetOrAdd(domainService.GetType(), (svcType) => { AsyncState asyncState = new AsyncState() { domainService = domainService }; ExecuteOnSTA(state => { AsyncState data = (AsyncState)state; try { data.cachedMetadata = InitMetadata(data.domainService); } catch (Exception ex) { data.domainService._OnError(ex); throw new DummyException(ex.Message, ex); } }, asyncState); return asyncState.cachedMetadata; })); }
public async Task DeleteAsyncSucceeds() { // Arrange var options = GetTestDbContextOptions("Delete_ScrapeJob_Database"); var testEntity = GenerateTestEntity(); using (var context = new WaasDbContext(options)) { await context.AddAsync(testEntity); await context.SaveChangesAsync(); } // Act bool result; using (var context = new WaasDbContext(options)) { var testRepository = new BaseDomainService <ScrapeJob, long>(context); await testRepository.DeleteAsync(testEntity.Id); result = await context.CommitAsync(); } // Assert Assert.True(result); using (var context = new WaasDbContext(options)) { Assert.Null(await context.ScrapeJobs.FindAsync(testEntity.Id)); } }
public static RunTimeMetadata GetInitializedMetadata(BaseDomainService domainService, IDataHelper dataHelper, IValueConverter valueConverter) { RunTimeMetadata result = _metadataCache.GetOrAdd(domainService.GetType(), (svcType) => { RunTimeMetadata runTimeMetadata = null; try { DesignTimeMetadata designTimeMetadata = ((IMetaDataProvider)domainService).GetDesignTimeMetadata(false); IDataManagerContainer dataManagerContainer = domainService.ServiceContainer.DataManagerContainer; runTimeMetadata = _InitMetadata(domainService, designTimeMetadata, dataHelper, valueConverter, dataManagerContainer); } catch (Exception ex) { domainService._OnError(ex); throw new DummyException(ex.Message, ex); } return(runTimeMetadata); }); return(result); }
public async Task GetAllSucceeds() { // Arrange var options = GetTestDbContextOptions("GetAll_ScrapeJob_Database"); const int count = 100; var testScrapeJobs = Enumerable.Range(1, count).Select(e => GenerateTestEntity(e)).ToArray(); using (var context = new WaasDbContext(options)) { foreach (ScrapeJob scrapeJob in testScrapeJobs) { await context.AddAsync(scrapeJob); } await context.SaveChangesAsync(); } // Act using (var context = new WaasDbContext(options)) { var testRepository = new BaseDomainService <ScrapeJob, long>(context); var result = testRepository.GetAll(); Assert.Equal(count, await result.CountAsync()); Assert.Equal(testScrapeJobs.AsQueryable(), result); } }
public IDataManager <TModel> GetDataManager <TModel>(BaseDomainService dataService) where TModel : class { var res = GetDataManager(dataService, typeof(TModel)); return((IDataManager <TModel>)res); }
public async Task AddAsyncSucceeds() { // Arrange var options = GetTestDbContextOptions("Add_ScrapeJob_Database"); var testEntity = GenerateTestEntity(); // Act bool result; using (var context = new WaasDbContext(options)) { var testRepository = new BaseDomainService <ScrapeJob, long>(context); await testRepository.AddAsync(testEntity); result = await context.CommitAsync(); } // Assert Assert.True(result); using (var context = new WaasDbContext(options)) { var check = await context.ScrapeJobs.FindAsync(testEntity.Id); Assert.Equal(testEntity, check); } }
private static CachedMetadata InitMetadata(BaseDomainService domainService) { EventHandler <RegisteredDMEventArgs> fn_regDM = (sender, e) => { ProcessMethodDescriptions(domainService.ServiceContainer, e.DataManagerType, (CachedMetadata)sender); }; CachedMetadata cachedMetadata = new CachedMetadata(); //called on every data manager registered while bootstrapping cachedMetadata.RegisteredDM += fn_regDM; try { InitCachedMetadata(domainService, cachedMetadata); } catch (Exception ex) { domainService._OnError(ex); throw new DummyException(ex.Message, ex); } finally { cachedMetadata.RegisteredDM -= fn_regDM; cachedMetadata.InitCompleted(); } return(cachedMetadata); }
public object GetDataManager(BaseDomainService dataService, Type modelType) { Func <BaseDomainService, object> factory; if (_managers.TryGetValue(modelType, out factory)) { return(factory(dataService)); } return(null); }
public static CachedMetadata EnsureMetadataInitialized(BaseDomainService domainService) { CachedMetadata cachedMetadata = null; Func<CachedMetadata> factory = () => { Type thisType = domainService.GetType(); CachedMetadata result = null; if (MetadataHelper._metadataCache.TryGetValue(thisType, out result)) return result; MetadataHelper.ExecuteOnSTA((state) => { MetadataHelper.InitMetadata((BaseDomainService)state); }, domainService); MetadataHelper._metadataCache.TryGetValue(thisType, out result); return result; }; System.Threading.LazyInitializer.EnsureInitialized<CachedMetadata>(ref cachedMetadata, factory); return cachedMetadata; }
public ICodeGenProvider GetCodeGen(BaseDomainService dataService, string lang) { if (!IsCodeGenEnabled) { throw new InvalidOperationException(ErrorStrings.ERR_CODEGEN_DISABLED); } System.Collections.Generic.IEnumerable <ICodeGenProviderFactory <TService> > factories = dataService.ServiceContainer.GetServices <ICodeGenProviderFactory <TService> >(); ICodeGenProviderFactory <TService> providerFactory = factories.Where(c => c.Lang == lang).FirstOrDefault(); if (providerFactory == null) { throw new InvalidOperationException(string.Format(ErrorStrings.ERR_CODEGEN_NOT_IMPLEMENTED, lang)); } return(providerFactory.Create(dataService)); }
private static RunTimeMetadata _InitMetadata(BaseDomainService domainService, DesignTimeMetadata designTimeMetadata, IDataHelper dataHelper, IValueConverter valueConverter, IDataManagerContainer dataManagerContainer) { RunTimeMetadataBuilder runTimeMetadataBuilder = new RunTimeMetadataBuilder(domainService.GetType(), designTimeMetadata, dataHelper, valueConverter, dataManagerContainer); try { return(runTimeMetadataBuilder.Build()); } catch (Exception ex) { domainService._OnError(ex); throw new DummyException(ex.Message, ex); } }
private static void InitMetadata(BaseDomainService domainService) { Type thisType = domainService.GetType(); CachedMetadata cachedMetadata = null; if (MetadataHelper._metadataCache.TryGetValue(thisType, out cachedMetadata)) { return; } try { cachedMetadata = CreateCachedMetadata(domainService); } catch(Exception ex) { domainService.OnError(ex); throw; } MetadataHelper._metadataCache.TryAdd(thisType, cachedMetadata); }
public async Task UpdateAsyncSucceeds() { // Arrange var options = GetTestDbContextOptions("Update_ScrapeJob_Database"); var testEntity = GenerateTestEntity(); using (var context = new WaasDbContext(options)) { await context.AddAsync(testEntity); await context.SaveChangesAsync(); } // Act bool result; using (var context = new WaasDbContext(options)) { var testRepository = new BaseDomainService <ScrapeJob, long>(context); await testRepository.UpdateAsync(testEntity.Id, job => { job.Pattern = "updatedPattern"; job.Url = "updatedUrl"; job.Enabled = !testEntity.Enabled; }); result = await context.CommitAsync(); } // Assert Assert.True(result); using (var context = new WaasDbContext(options)) { var updatedEntity = await context.ScrapeJobs.FindAsync(testEntity.Id); Assert.NotEqual(testEntity.Pattern, updatedEntity.Pattern); Assert.NotEqual(testEntity.Url, updatedEntity.Url); Assert.NotEqual(testEntity.Enabled, updatedEntity.Enabled); } }
public async Task GetAsyncSucceeds() { // Arrange var options = GetTestDbContextOptions("Get_ScrapeJob_Database"); var testEntity = GenerateTestEntity(); using (var context = new WaasDbContext(options)) { await context.AddAsync(testEntity); await context.SaveChangesAsync(); } // Act using (var context = new WaasDbContext(options)) { var testRepository = new BaseDomainService <ScrapeJob, long>(context); var result = await testRepository.GetAsync(testEntity.Id); // Assert Assert.Equal(testEntity, result); } }
public BaseCsharpProvider(BaseDomainService owner) { this._owner = owner; }
public static void CheckMethod(BaseDomainService domainService, DbSetInfo dbSetInfo, MethodType methodType) { Type thisType = domainService.GetType(); string methodName = dbSetInfo.getOperationMethodName(methodType); if (string.IsNullOrWhiteSpace(methodName)) return; MethodInfo minfo = DataHelper.GetMethodInfo(thisType, methodName); if (minfo != null) { switch (methodType) { case MethodType.Refresh: dbSetInfo._refreshDataMethod = minfo; break; case MethodType.Insert: dbSetInfo._insertDataMethod = minfo; break; case MethodType.Update: dbSetInfo._updateDataMethod = minfo; break; case MethodType.Delete: dbSetInfo._deleteDataMethod = minfo; break; case MethodType.Validate: dbSetInfo._validateDataMethod = minfo; break; default: throw new DomainServiceException(string.Format("Invalid Method Type {0}", methodType)); } } else { throw new DomainServiceException(string.Format("The DataService: {0} has no method: {2} specified for DbSet: {1} and operation: {3}", thisType.Name, dbSetInfo.dbSetName, methodName, Enum.GetName(typeof(MethodType), methodType))); } }
private static void CheckDataServiceMethods(BaseDomainService domainService, CachedMetadata metadata) { StringBuilder sb = new StringBuilder(); foreach (var dbSet in metadata.dbSets.Values) { try { MetadataHelper.CheckMethod(domainService, dbSet, MethodType.Insert); MetadataHelper.CheckMethod(domainService, dbSet, MethodType.Update); MetadataHelper.CheckMethod(domainService, dbSet, MethodType.Delete); MetadataHelper.CheckMethod(domainService, dbSet, MethodType.Validate); MetadataHelper.CheckMethod(domainService, dbSet, MethodType.Refresh); } catch (DomainServiceException ex) { sb.AppendLine(ex.Message); } } if (sb.Length > 0) throw new DomainServiceException(sb.ToString()); }
public BaseController(BaseDomainService <T> service) { Service = service; }
public XamlProvider(BaseDomainService owner) { this._owner = owner; }
public TypeScriptProvider(BaseDomainService owner, IEnumerable <Type> clientTypes = null) { this._owner = owner; this._clientTypes = clientTypes ?? Enumerable.Empty <Type>(); }
private static CachedMetadata CreateCachedMetadata(BaseDomainService domainService) { Metadata metadata = domainService.GetMetadata(false); CachedMetadata cachedMetadata = new CachedMetadata(); foreach (var dbSetInfo in metadata.DbSets) { dbSetInfo.Initialize(domainService.ServiceContainer); //indexed by dbSetName cachedMetadata.dbSets.Add(dbSetInfo.dbSetName, dbSetInfo); } MetadataHelper.ProcessMethodDescriptions(domainService, cachedMetadata); MetadataHelper.CheckDataServiceMethods(domainService, cachedMetadata); foreach (var assoc in metadata.Associations) { if (string.IsNullOrWhiteSpace(assoc.name)) { throw new DomainServiceException(ErrorStrings.ERR_ASSOC_EMPTY_NAME); } if (!cachedMetadata.dbSets.ContainsKey(assoc.parentDbSetName)) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_PARENT, assoc.name, assoc.parentDbSetName)); } if (!cachedMetadata.dbSets.ContainsKey(assoc.childDbSetName)) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_CHILD, assoc.name, assoc.childDbSetName)); } var childDb = cachedMetadata.dbSets[assoc.childDbSetName]; var parentDb = cachedMetadata.dbSets[assoc.parentDbSetName]; var parentDbFields = parentDb.GetFieldByNames(); var childDbFields = childDb.GetFieldByNames(); //check navigation field //dont allow to define it explicitly, the association adds the field by itself (implicitly) if (!string.IsNullOrEmpty(assoc.childToParentName) && childDbFields.ContainsKey(assoc.childToParentName)) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_NAV_FIELD, assoc.name, assoc.childToParentName)); } //check navigation field //dont allow to define it explicitly, the association adds the field by itself (implicitly) if (!string.IsNullOrEmpty(assoc.parentToChildrenName) && parentDbFields.ContainsKey(assoc.parentToChildrenName)) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_NAV_FIELD, assoc.name, assoc.parentToChildrenName)); } if (!string.IsNullOrEmpty(assoc.parentToChildrenName) && !string.IsNullOrEmpty(assoc.childToParentName) && assoc.childToParentName == assoc.parentToChildrenName) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_NAV_FIELD, assoc.name, assoc.parentToChildrenName)); } foreach (var frel in assoc.fieldRels) { if (!parentDbFields.ContainsKey(frel.parentField)) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_PARENT_FIELD, assoc.name, frel.parentField)); } if (!childDbFields.ContainsKey(frel.childField)) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_CHILD_FIELD, assoc.name, frel.childField)); } } //indexed by Name cachedMetadata.associations.Add(assoc.name, assoc); if (!string.IsNullOrEmpty(assoc.childToParentName)) { var sb = new System.Text.StringBuilder(120); var dependentOn = assoc.fieldRels.Aggregate(sb, (a, b) => a.Append((a.Length == 0 ? "" : ",") + b.childField), a => a).ToString(); //add navigation field to dbSet's field collection childDb.fieldInfos.Add(new Field() { fieldName = assoc.childToParentName, fieldType = FieldType.Navigation, dataType = DataType.None, dependentOn = dependentOn, _TypeScriptDataType = TypeScriptHelper.GetEntityTypeName(parentDb.dbSetName) }); } if (!string.IsNullOrEmpty(assoc.parentToChildrenName)) { var sb = new System.Text.StringBuilder(120); //add navigation field to dbSet's field collection parentDb.fieldInfos.Add(new Field() { fieldName = assoc.parentToChildrenName, fieldType = FieldType.Navigation, dataType = DataType.None, _TypeScriptDataType = string.Format("{0}[]", TypeScriptHelper.GetEntityTypeName(childDb.dbSetName)) }); } } //foreach (var assoc in metadata.Associations) return cachedMetadata; }
public Model2Controller(BaseDomainService <Model2> service) : base(service) { }
public AuthorizationContext(BaseDomainService service, IUserProvider userProvider, IServiceFactory serviceFactory) { Service = service; _userProvider = userProvider ?? throw new ArgumentNullException(nameof(userProvider), ErrorStrings.ERR_NO_USER); ServiceFactory = serviceFactory ?? throw new ArgumentNullException(nameof(serviceFactory)); }
private static void InitCachedMetadata(BaseDomainService domainService, CachedMetadata cachedMetadata) { var metadata = domainService.GetMetadata(false); foreach (var dbSetInfo in metadata.DbSets) { dbSetInfo.Initialize(domainService.ServiceContainer); //indexed by dbSetName cachedMetadata.dbSets.Add(dbSetInfo.dbSetName, dbSetInfo); } //bootstrapping domainService.Bootstrap(new ServiceConfig(cachedMetadata)); ProcessMethodDescriptions(domainService.ServiceContainer, domainService.GetType(), cachedMetadata); foreach (var assoc in metadata.Associations) { if (string.IsNullOrWhiteSpace(assoc.name)) { throw new DomainServiceException(ErrorStrings.ERR_ASSOC_EMPTY_NAME); } if (!cachedMetadata.dbSets.ContainsKey(assoc.parentDbSetName)) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_PARENT, assoc.name, assoc.parentDbSetName)); } if (!cachedMetadata.dbSets.ContainsKey(assoc.childDbSetName)) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_CHILD, assoc.name, assoc.childDbSetName)); } var childDb = cachedMetadata.dbSets[assoc.childDbSetName]; var parentDb = cachedMetadata.dbSets[assoc.parentDbSetName]; var parentDbFields = parentDb.GetFieldByNames(); var childDbFields = childDb.GetFieldByNames(); //check navigation field //dont allow to define it explicitly, the association adds the field by itself (implicitly) if (!string.IsNullOrEmpty(assoc.childToParentName) && childDbFields.ContainsKey(assoc.childToParentName)) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_NAV_FIELD, assoc.name, assoc.childToParentName)); } //check navigation field //dont allow to define it explicitly, the association adds the field by itself (implicitly) if (!string.IsNullOrEmpty(assoc.parentToChildrenName) && parentDbFields.ContainsKey(assoc.parentToChildrenName)) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_NAV_FIELD, assoc.name, assoc.parentToChildrenName)); } if (!string.IsNullOrEmpty(assoc.parentToChildrenName) && !string.IsNullOrEmpty(assoc.childToParentName) && assoc.childToParentName == assoc.parentToChildrenName) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_NAV_FIELD, assoc.name, assoc.parentToChildrenName)); } foreach (var frel in assoc.fieldRels) { if (!parentDbFields.ContainsKey(frel.parentField)) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_PARENT_FIELD, assoc.name, frel.parentField)); } if (!childDbFields.ContainsKey(frel.childField)) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_CHILD_FIELD, assoc.name, frel.childField)); } } //indexed by Name cachedMetadata.associations.Add(assoc.name, assoc); if (!string.IsNullOrEmpty(assoc.childToParentName)) { var sb = new StringBuilder(120); var dependentOn = assoc.fieldRels.Aggregate(sb, (a, b) => a.Append((a.Length == 0 ? "" : ",") + b.childField), a => a).ToString(); //add navigation field to dbSet's field collection childDb.fieldInfos.Add(new Field { fieldName = assoc.childToParentName, fieldType = FieldType.Navigation, dataType = DataType.None, dependentOn = dependentOn, _TypeScriptDataType = TypeScriptHelper.GetEntityInterfaceName(parentDb.dbSetName) }); } if (!string.IsNullOrEmpty(assoc.parentToChildrenName)) { var sb = new StringBuilder(120); //add navigation field to dbSet's field collection parentDb.fieldInfos.Add(new Field { fieldName = assoc.parentToChildrenName, fieldType = FieldType.Navigation, dataType = DataType.None, _TypeScriptDataType = string.Format("{0}[]", TypeScriptHelper.GetEntityInterfaceName(childDb.dbSetName)) }); } } //foreach (var assoc in metadata.Associations) }
/// <summary> /// Test if public methods on the service has Invoke or Query Attribute /// and generates from this methods their invocation method descriptions /// </summary> /// <returns></returns> private static void ProcessMethodDescriptions(BaseDomainService domainService, CachedMetadata metadata) { Type thisType= domainService.GetType(); IServiceContainer services = domainService.ServiceContainer; Func<MethodInfo, MethodType> fn_getMethodType = (m) => { if (m.IsDefined(typeof(QueryAttribute), false)) return MethodType.Query; else if (m.IsDefined(typeof(InvokeAttribute), false)) return MethodType.Invoke; else if (m.IsDefined(typeof(InsertAttribute), false)) return MethodType.Insert; else if (m.IsDefined(typeof(UpdateAttribute), false)) return MethodType.Update; else if (m.IsDefined(typeof(DeleteAttribute), false)) return MethodType.Delete; else if (m.IsDefined(typeof(ValidateAttribute), false)) return MethodType.Validate; else if (m.IsDefined(typeof(RefreshAttribute), false)) return MethodType.Refresh; else return MethodType.None; }; var dbsetsLookUp = metadata.dbSets.Values.ToLookup(v => v.EntityType); var dbsetsByType = dbsetsLookUp.ToDictionary(v => v.Key); var methodInfos = thisType.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public); var allList = methodInfos.Select(m => new { method = m, methodType = fn_getMethodType(m) }); var queryAndInvokes = allList.Where((info) => info.methodType == MethodType.Query || info.methodType == MethodType.Invoke).ToArray(); MethodsList methodList = new MethodsList(); Array.ForEach(queryAndInvokes, (info) => { methodList.Add(MethodDescription.FromMethodInfo(info.method, info.methodType, services)); }); metadata.InitMethods(methodList); var otherMethods = allList.Where((info) => !(info.methodType == MethodType.Query || info.methodType == MethodType.Invoke || info.methodType == MethodType.None)).ToArray(); Array.ForEach(otherMethods, (info) => { Type entityType = null; if (info.methodType == MethodType.Refresh) { entityType = RemoveTaskFromType(info.method.ReturnType); } else { entityType = info.method.GetParameters().First().ParameterType; } IGrouping<Type, DbSetInfo> dbSets = null; if (!dbsetsByType.TryGetValue(entityType, out dbSets)) return; foreach (var dbSet in dbSets) { switch (info.methodType) { case MethodType.Insert: dbSet.insertDataMethod = info.method.Name; break; case MethodType.Update: dbSet.updateDataMethod = info.method.Name; break; case MethodType.Delete: dbSet.deleteDataMethod = info.method.Name; break; case MethodType.Validate: dbSet.validateDataMethod = info.method.Name; break; case MethodType.Refresh: dbSet.refreshDataMethod = info.method.Name; break; default: throw new DomainServiceException(string.Format("Unknown Method Type: {0}", info.methodType)); } } }); }
public SubResultsGenerator(BaseDomainService domainService) { this._domainService = domainService; this._dataHelper = this._domainService.ServiceContainer.DataHelper; }
public ICodeGenProvider Create(BaseDomainService owner) { return(this.Create((TService)owner)); }