public virtual ActionResult <TView> Default() { using (TDbContext context = UnitOfWorkHelper <TDbContext> .InitContext()) { var transaction = context.Database.BeginTransaction(); TView data = ReflectionHelper.InitModel <TView>(); ReflectionHelper.SetPropertyValue(data, new JProperty("Specificulture", _lang)); ReflectionHelper.SetPropertyValue(data, new JProperty("Status", MixService.GetConfig <string>("DefaultContentStatus"))); data.ExpandView(context, transaction); return(Ok(data)); } }
/// <summary> /// Removes the model asynchronous. /// </summary> /// <param name="isRemoveRelatedModels">if set to <c>true</c> [is remove related models].</param> /// <param name="_context">The context.</param> /// <param name="_transaction">The transaction.</param> /// <returns></returns> public virtual async Task <RepositoryResponse <TModel> > RemoveModelAsync(bool isRemoveRelatedModels = false, TDbContext _context = null, IDbContextTransaction _transaction = null) { UnitOfWorkHelper <TDbContext> .InitTransaction(_context, _transaction, out TDbContext context, out IDbContextTransaction transaction, out bool isRoot); RepositoryResponse <TModel> result = new RepositoryResponse <TModel>() { IsSucceed = true }; try { ParseModel(_context, _transaction); if (isRemoveRelatedModels) { var removeRelatedResult = await RemoveRelatedModelsAsync((TView)this, context, transaction).ConfigureAwait(false); if (removeRelatedResult.IsSucceed) { result = await Repository.RemoveModelAsync(Model, context, transaction).ConfigureAwait(false); } else { result.IsSucceed = result.IsSucceed && removeRelatedResult.IsSucceed; result.Errors.AddRange(removeRelatedResult.Errors); result.Exception = removeRelatedResult.Exception; } } else { result = await Repository.RemoveModelAsync(Model, context, transaction).ConfigureAwait(false); } UnitOfWorkHelper <TDbContext> .HandleTransaction(result.IsSucceed, isRoot, transaction); _ = this.SendNotifyAsync(this, RepositoryAction.Delete, result.IsSucceed); return(result); } catch (Exception ex) { return(UnitOfWorkHelper <TDbContext> .HandleException <TModel>(ex, isRoot, transaction)); } finally { if (result.IsSucceed && IsCache) { await RemoveCache(Model, context, transaction); } if (isRoot) { //if current Context is Root UnitOfWorkHelper <TDbContext> .CloseDbContext(ref context, ref transaction); } } }
/// <summary> /// Step 3 /// - Init Languages for translate /// </summary> /// <param name="culture"></param> /// <param name="_context"></param> /// <param name="_transaction"></param> /// <returns></returns> public async Task <RepositoryResponse <bool> > InitLanguagesAsync(string specificulture, List <MixLanguage> languages , MixCmsContext _context = null, IDbContextTransaction _transaction = null) { /* Init Languages */ UnitOfWorkHelper <MixCmsContext> .InitTransaction(_context, _transaction, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot); var result = await ViewModels.MixLanguages.ReadMvcViewModel.ImportLanguages(languages, specificulture, context, transaction); UnitOfWorkHelper <MixCmsContext> .HandleTransaction(result.IsSucceed, isRoot, transaction); return(result); }
public void LoadData(int?pageSize = null, int?pageIndex = null , MixCmsContext _context = null, IDbContextTransaction _transaction = null) { UnitOfWorkHelper <MixCmsContext> .InitTransaction(_context, _transaction, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot); try { pageSize = pageSize > 0 ? pageSize : PageSize; pageIndex = pageIndex > 0 ? pageIndex : 0; Expression <Func <MixPageModule, bool> > dataExp = null; Expression <Func <MixPagePost, bool> > postExp = null; foreach (var item in Modules) { item.Module.LoadData(pageSize: pageSize, pageIndex: pageIndex, _context: context, _transaction: transaction); } switch (Type) { case MixPageType.ListPost: postExp = n => n.PageId == Id && n.Specificulture == Specificulture; break; default: dataExp = m => m.PageId == Id && m.Specificulture == Specificulture; postExp = n => n.PageId == Id && n.Specificulture == Specificulture; break; } if (postExp != null) { var getPosts = MixPagePosts.ReadViewModel.Repository .GetModelListBy(postExp , MixService.GetConfig <string>(MixConstants.ConfigurationKeyword.OrderBy), 0 , pageSize, pageIndex , _context: context, _transaction: transaction); if (getPosts.IsSucceed) { Posts = getPosts.Data; } } } catch (Exception ex) { UnitOfWorkHelper <MixCmsContext> .HandleException <PaginationModel <ReadMvcViewModel> >(ex, isRoot, transaction); } finally { if (isRoot) { //if current Context is Root context.Database.CloseConnection(); transaction.Dispose(); context.Dispose(); } } }
public virtual ActionResult <TView> Default() { using (TDbContext context = UnitOfWorkHelper <TDbContext> .InitContext()) { var transaction = context.Database.BeginTransaction(); TView data = ReflectionHelper.InitModel <TView>(); ReflectionHelper.SetPropertyValue(data, new JProperty(MixQueryColumnName.Specificulture, _lang)); ReflectionHelper.SetPropertyValue(data, new JProperty(MixQueryColumnName.Status, MixService.GetAppSetting <string>(MixAppSettingKeywords.DefaultContentStatus))); data.ExpandView(context, transaction); return(Ok(data)); } }
public override async Task <RepositoryResponse <FormViewModel> > SaveModelAsync(bool isSaveSubModels = false, MixCmsContext _context = null, IDbContextTransaction _transaction = null) { UnitOfWorkHelper <MixCmsContext> .InitTransaction(_context, _transaction, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot); try { var result = await base.SaveModelAsync(isSaveSubModels, context, transaction); if (result.IsSucceed && !string.IsNullOrEmpty(ParentId)) { var getNav = MixRelatedAttributeDatas.UpdateViewModel.Repository.CheckIsExists( m => m.DataId == Id && m.ParentId == ParentId && m.ParentType == ParentType.ToString() && m.Specificulture == Specificulture , context, transaction); if (!getNav) { var nav = new MixRelatedAttributeDatas.UpdateViewModel() { DataId = Id, Specificulture = Specificulture, AttributeSetId = AttributeSetId, AttributeSetName = AttributeSetName, ParentType = ParentType, ParentId = ParentId, Status = MixEnums.MixContentStatus.Published, Priority = Priority }; var saveResult = await nav.SaveModelAsync(false, context, transaction); if (!saveResult.IsSucceed) { result.IsSucceed = false; result.Exception = saveResult.Exception; result.Errors = saveResult.Errors; } } } UnitOfWorkHelper <MixCmsContext> .HandleTransaction(result.IsSucceed, isRoot, transaction); return(result); } catch (Exception ex) { return(UnitOfWorkHelper <MixCmsContext> .HandleException <FormViewModel>(ex, isRoot, transaction)); } finally { if (isRoot) { ParseData(); context.Dispose(); } } }
public async Task <RepositoryResponse <bool> > ImportAsync(string destCulture, MixCmsContext _context = null, IDbContextTransaction _transaction = null) { var result = new RepositoryResponse <bool>() { IsSucceed = true }; UnitOfWorkHelper <MixCmsContext> .InitTransaction(_context, _transaction, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot); try { if (Pages != null) { result = await ImportPagesAsync(destCulture, context, transaction); } if (result.IsSucceed && Modules != null) { result = await ImportModulesAsync(destCulture, context, transaction); } if (result.IsSucceed && AttributeSets != null) { result = await ImportAttributeSetsAsync(context, transaction); } if (result.IsSucceed && AttributeSetDatas.Count > 0) { result = await ImportAttributeSetDatas(destCulture, context, transaction); } if (result.IsSucceed && RelatedData.Count > 0) { result = await ImportRelatedDatas(destCulture, context, transaction); } UnitOfWorkHelper <MixCmsContext> .HandleTransaction(result.IsSucceed, isRoot, transaction); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { var error = UnitOfWorkHelper <MixCmsContext> .HandleException <MixPages.ImportViewModel>(ex, isRoot, transaction); result.IsSucceed = false; result.Errors = error.Errors; result.Exception = error.Exception; } finally { //if current Context is Root if (isRoot) { context.Database.CloseConnection(); transaction.Dispose(); context.Dispose(); } } return(result); }
public static RepositoryResponse <List <MixPagePosts.ReadViewModel> > GetPagePostNavAsync(int postId, string specificulture , MixCmsContext _context = null, IDbContextTransaction _transaction = null) { UnitOfWorkHelper <MixCmsContext> .InitTransaction(_context, _transaction, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot); try { var navCategoryPostViewModels = context.MixPage.Include(cp => cp.MixPagePost).Where(a => a.Specificulture == specificulture && (a.Type == MixEnums.MixPageType.ListPost.ToString()) ) .AsEnumerable() .Select(p => new MixPagePosts.ReadViewModel( new MixPagePost() { PostId = postId, PageId = p.Id, Specificulture = specificulture }, _context, _transaction) { IsActived = p.MixPagePost.Any(cp => cp.PostId == postId && cp.Specificulture == specificulture), Description = p.Title }); return(new RepositoryResponse <List <ReadViewModel> >() { IsSucceed = true, Data = navCategoryPostViewModels.ToList() }); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { if (isRoot) { transaction.Rollback(); } return(new RepositoryResponse <List <MixPagePosts.ReadViewModel> >() { IsSucceed = true, Data = null, Exception = ex }); } finally { if (isRoot) { //if current Context is Root transaction.Dispose(); UnitOfWorkHelper <MixCmsContext> .CloseDbContext(ref context, ref transaction); } } }
public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) { if (context.HandlerMethod == null || !context.ActionDescriptor.IsPageAction()) { await next(); return; } var methodInfo = context.HandlerMethod.MethodInfo; var unitOfWorkAttr = UnitOfWorkHelper.GetUnitOfWorkAttributeOrNull(methodInfo); context.HttpContext.Items["_AbpActionInfo"] = new AbpActionInfoInHttpContext { IsObjectResult = ActionResultHelper.IsObjectResult(context.HandlerMethod.MethodInfo.ReturnType, typeof(void)) }; if (unitOfWorkAttr?.IsDisabled == true) { await next(); return; } var options = CreateOptions(context, unitOfWorkAttr); var unitOfWorkManager = context.GetRequiredService <IUnitOfWorkManager>(); //Trying to begin a reserved UOW by AbpUnitOfWorkMiddleware if (unitOfWorkManager.TryBeginReserved(UnitOfWork.UnitOfWorkReservationName, options)) { var result = await next(); if (!Succeed(result)) { await RollbackAsync(context, unitOfWorkManager); } return; } //Begin a new, independent unit of work using (var uow = unitOfWorkManager.Begin(options)) { var result = await next(); if (Succeed(result)) { await uow.CompleteAsync(context.HttpContext.RequestAborted); } } }
V_1 = Expression.Parameter(Type.GetTypeFromHandle(// // Current member / type: System.Void Mix.Cms.Lib.ViewModels.MixAttributeSetDatas.ExportViewModel::ExpandView(Mix.Cms.Lib.Models.Cms.MixCmsContext,Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction) // Exception in: System.Void ExpandView(Mix.Cms.Lib.Models.Cms.MixCmsContext,Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction) // Specified method is not supported. // // mailto: [email protected] public static Task<RepositoryResponse<List<Mix.Cms.Lib.ViewModels.MixAttributeSetDatas.UpdateViewModel>>> FilterByValueAsync(string culture, string attributeSetName, Dictionary<string, StringValues> queryDictionary, MixCmsContext _context = null, IDbContextTransaction _transaction = null) { V_0 = new ExportViewModel.u003cu003ec__DisplayClass70_0(); V_0.culture = culture; V_0.attributeSetName = attributeSetName; UnitOfWorkHelper<MixCmsContext>.InitTransaction(_context, _transaction, ref V_1, ref V_2, ref V_3); try { try { V_4 = new ExportViewModel.u003cu003ec__DisplayClass70_1(); V_4.CSu0024u003cu003e8__locals1 = V_0; V_7 = Expression.Parameter(Type.GetTypeFromHandle(// // Current member / type: System.Threading.Tasks.Task`1<Mix.Domain.Core.ViewModels.RepositoryResponse`1<System.Collections.Generic.List`1<Mix.Cms.Lib.ViewModels.MixAttributeSetDatas.UpdateViewModel>>> Mix.Cms.Lib.ViewModels.MixAttributeSetDatas.ExportViewModel::FilterByValueAsync(System.String,System.String,System.Collections.Generic.Dictionary`2<System.String,Microsoft.Extensions.Primitives.StringValues>,Mix.Cms.Lib.Models.Cms.MixCmsContext,Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction) // Exception in: System.Threading.Tasks.Task<Mix.Domain.Core.ViewModels.RepositoryResponse<System.Collections.Generic.List<Mix.Cms.Lib.ViewModels.MixAttributeSetDatas.UpdateViewModel>>> FilterByValueAsync(System.String,System.String,System.Collections.Generic.Dictionary<System.String,Microsoft.Extensions.Primitives.StringValues>,Mix.Cms.Lib.Models.Cms.MixCmsContext,Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction) // Specified method is not supported. // // mailto: [email protected] private void ParseData() { this.set_Data(new JObject()); stackVariable3 = this.get_Values(); stackVariable4 = ExportViewModel.u003cu003ec.u003cu003e9__71_0; if (stackVariable4 == null) { dummyVar0 = stackVariable4; stackVariable4 = new Func<Mix.Cms.Lib.ViewModels.MixAttributeSetValues.UpdateViewModel, int>(ExportViewModel.u003cu003ec.u003cu003e9.u003cParseDatau003eb__71_0); ExportViewModel.u003cu003ec.u003cu003e9__71_0 = stackVariable4; } V_0 = stackVariable3.OrderBy<Mix.Cms.Lib.ViewModels.MixAttributeSetValues.UpdateViewModel, int>(stackVariable4).GetEnumerator(); try { while (V_0.MoveNext()) { V_1 = V_0.get_Current(); V_1.set_AttributeFieldName(V_1.get_Field().get_Name()); this.get_Data().Add(this.ParseValue(V_1)); } } finally { if (V_0 != null) { V_0.Dispose(); } } this.get_Data().Add(new JProperty("createdDateTime", (object)this.get_CreatedDateTime())); return; }
public static async Task <bool> InitPositionsAsync(MixCmsContext _context = null, IDbContextTransaction _transaction = null) { /* Init Positions */ UnitOfWorkHelper <MixCmsContext> .InitTransaction(_context, _transaction, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot); var positions = FileRepository.Instance.GetFile(MixConstants.CONST_FILE_POSITIONS, "data", true, "{}"); var obj = JObject.Parse(positions.Content); var arrPosition = obj["data"].ToObject <List <MixPosition> >(); var result = await ViewModels.MixPositions.ReadViewModel.ImportPositions(arrPosition, context, transaction); UnitOfWorkHelper <MixCmsContext> .HandleTransaction(result.IsSucceed, isRoot, transaction); return(result.IsSucceed); }
/// <summary> /// This method is called on ComponentRegistered event of Castle Windsor. /// </summary> public static void ComponentRegistered(string key, Castle.MicroKernel.IHandler handler) { if (UnitOfWorkHelper.IsRepositoryClass(handler.ComponentModel.Implementation)) { //Intercept all methods of all repositories. handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(NhUnitOfWorkInterceptor))); } else if (handler.ComponentModel.Implementation.GetMethods().Any(UnitOfWorkHelper.HasUnitOfWorkAttribute)) { //Intercept all methods of classes those have at least one method that has UnitOfWork attribute. //TODO: Intecept only UnitOfWork methods, not other methods! handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(NhUnitOfWorkInterceptor))); } }
public override async Task <RepositoryResponse <FormViewModel> > SaveModelAsync(bool isSaveSubModels = false, MixCmsContext _context = null, IDbContextTransaction _transaction = null) { var result = new RepositoryResponse <FormViewModel>() { IsSucceed = true }; UnitOfWorkHelper <MixCmsContext> .InitTransaction(_context, _transaction, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot); try { if (AttributeData != null && string.IsNullOrEmpty(AttributeData.Id)) { var saveData = await AttributeData.SaveModelAsync(true, context, transaction); if (!saveData.IsSucceed) { result.IsSucceed = false; result.Errors = saveData.Errors; result.Exception = saveData.Exception; } else { DataId = saveData.Data.Id; } } else { DataId = AttributeData.Id; } if (result.IsSucceed) { result = await base.SaveModelAsync(true, context, transaction); } UnitOfWorkHelper <MixCmsContext> .HandleTransaction(result.IsSucceed, isRoot, transaction); return(result); } catch (Exception ex) { return(UnitOfWorkHelper <MixCmsContext> .HandleException <FormViewModel>(ex, isRoot, transaction)); } finally { if (isRoot) { context.Dispose(); } } }
private static bool RequiresDbConnection(MethodInfo methodInfo) { if (UnitOfWorkHelper.HasUnitOfWorkAttribute(methodInfo)) { return(true); } if (UnitOfWorkHelper.IsRepositoryMethod(methodInfo)) { return(true); } return(false); }
private void KernelComponentRegistered(string key, IHandler handler) { if (UnitOfWorkHelper.IsRepositoryClass(handler.ComponentModel.Implementation)) { handler.ComponentModel.Interceptors.Add( new InterceptorReference(typeof(NHibernateUnitOfWorkInterceptor))); } if (handler.ComponentModel.Implementation.GetMethods().Any(UnitOfWorkHelper.HasUnitOfWorkAttribute)) { handler.ComponentModel.Interceptors.Add( new InterceptorReference(typeof(NHibernateUnitOfWorkInterceptor))); } }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { if (!context.ActionDescriptor.IsControllerAction()) { await next(); return; } var methodInfo = context.ActionDescriptor.GetMethodInfo(); var unitOfWorkAttr = UnitOfWorkHelper.GetUnitOfWorkAttributeOrNull(methodInfo); context.HttpContext.Items["_AbpActionInfo"] = new AbpActionInfoInHttpContext { IsObjectResult = context.ActionDescriptor.HasObjectResult() }; if (unitOfWorkAttr?.IsDisabled == true) { await next(); return; } var options = CreateOptions(context, unitOfWorkAttr); //Trying to begin a reserved UOW by AbpUnitOfWorkMiddleware if (_unitOfWorkManager.TryBeginReserved(UnitOfWork.UnitOfWorkReservationName, options)) { var result = await next(); if (!Succeed(result)) { await RollbackAsync(context); } return; } //Begin a new, independent unit of work using (var uow = _unitOfWorkManager.Begin(options)) { var result = await next(); if (Succeed(result)) { await uow.CompleteAsync(context.HttpContext.RequestAborted); } } }
internal async Task <RepositoryResponse <UpdateViewModel> > DuplicateAsync( MixCmsContext _context = null, IDbContextTransaction _transaction = null) { UnitOfWorkHelper <MixCmsContext> .InitTransaction(_context, _transaction, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot); try { var newId = Guid.NewGuid().ToString(); foreach (var item in Values) { if (item.Column.DataType == MixDataType.Reference) { var getSubData = await MixDatabaseDataAssociations.UpdateViewModel.Repository.GetModelListByAsync( m => m.ParentId == Id && m.MixDatabaseId == item.Column.ReferenceId && m.ParentType == MixDatabaseParentType.Set && m.Specificulture == Specificulture , context, transaction); foreach (var subNav in getSubData.Data) { subNav.ParentId = newId; await subNav.DuplicateAsync(context, transaction); } } else { item.Id = null; item.DataId = newId; } } Id = newId; return(await SaveModelAsync(true, context, transaction)); } catch (Exception ex) { return(UnitOfWorkHelper <MixCmsContext> .HandleException <UpdateViewModel>(ex, isRoot, transaction)); } finally { if (isRoot) { await transaction.CommitAsync(); await context.SaveChangesAsync(); } } }
public static async Task <RepositoryResponse <bool> > ImportConfigurations(List <MixConfiguration> arrConfiguration, string destCulture, MixCmsContext _context = null, IDbContextTransaction _transaction = null) { var result = new RepositoryResponse <bool>() { IsSucceed = true }; bool isRoot = _context == null; var context = _context ?? new MixCmsContext(); var transaction = _transaction ?? context.Database.BeginTransaction(); try { foreach (var item in arrConfiguration) { var lang = new ReadMvcViewModel(item, context, transaction); lang.CreatedDateTime = DateTime.UtcNow; lang.Specificulture = destCulture; var saveResult = await lang.SaveModelAsync(false, context, transaction); result.IsSucceed = result.IsSucceed && saveResult.IsSucceed; if (!result.IsSucceed) { result.Exception = saveResult.Exception; result.Errors = saveResult.Errors; break; } } result.Data = true; UnitOfWorkHelper <MixCmsContext> .HandleTransaction(result.IsSucceed, isRoot, transaction); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { var error = UnitOfWorkHelper <MixCmsContext> .HandleException <ReadMvcViewModel>(ex, isRoot, transaction); result.IsSucceed = false; result.Errors = error.Errors; result.Exception = error.Exception; } finally { //if current Context is Root if (isRoot) { context?.Dispose(); } } return(result); }
public static void LoadFromDatabase(SioCmsContext _context = null, IDbContextTransaction _transaction = null) { UnitOfWorkHelper<SioCmsContext>.InitTransaction(_context, _transaction , out SioCmsContext context, out IDbContextTransaction transaction, out bool isRoot); try { Instance.Translator = new JObject(); var ListLanguage = context.SioLanguage; foreach (var culture in context.SioCulture) { JObject arr = new JObject(); foreach (var lang in ListLanguage.Where(l => l.Specificulture == culture.Specificulture)) { JProperty l = new JProperty(lang.Keyword, lang.Value ?? lang.DefaultValue); arr.Add(l); } Instance.Translator.Add(new JProperty(culture.Specificulture, arr)); } Instance.LocalSettings = new JObject(); var listLocalSettings = context.SioConfiguration; foreach (var culture in context.SioCulture) { JObject arr = new JObject(); foreach (var cnf in listLocalSettings.Where(l => l.Specificulture == culture.Specificulture)) { JProperty l = new JProperty(cnf.Keyword, cnf.Value); arr.Add(l); } Instance.LocalSettings.Add(new JProperty(culture.Specificulture, arr)); } UnitOfWorkHelper<SioCmsContext>.HandleTransaction(true, isRoot, transaction); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { var error = UnitOfWorkHelper<SioCmsContext>.HandleException<SioLanguage>(ex, isRoot, transaction); } finally { //if current Context is Root if (isRoot) { context?.Dispose(); } } }
private static void Kernel_ComponentRegistered(string key, IHandler handler) { if (UnitOfWorkHelper.IsRepositoryClass(handler.ComponentModel.Implementation)) { handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(NHUnitOfWorkInterceptor))); } if ( !handler.ComponentModel.Implementation.GetMethods() .Any(method => UnitOfWorkHelper.HasUnitOfWorkAttribute(method))) { return; } handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(NHUnitOfWorkInterceptor))); }
public static void RegisterUnitOfWorkInterceptor(string key, IHandler handler) { foreach (var method in handler.ComponentModel.Implementation.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { if (UnitOfWorkHelper.HasUnitOfWorkAttribute(method)) { handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(UnitOfWorkInterceptor))); return; } } if (UnitOfWorkHelper.IsRepositoryClass(handler.ComponentModel.Implementation)) { handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(UnitOfWorkInterceptor))); return; } }
void Kernel_ComponentRegistered(string key, IHandler handler) { if (UnitOfWorkHelper.IsRepositoryClass(handler.ComponentModel.Implementation)) { handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(UoWInterceptor))); } foreach (var method in handler.ComponentModel.Implementation.GetMethods()) { if (UnitOfWorkHelper.HasUnitOfWorkAttribute(method)) { handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(UoWInterceptor))); return; } } }
public static async Task <RepositoryResponse <List <TView> > > FilterByOtherValueAsync <TView>( string culture, string attributeSetName , string filterType, Dictionary <string, string> queries , string responseName , MixCmsContext _context = null, IDbContextTransaction _transaction = null) where TView : ViewModelBase <MixCmsContext, MixAttributeSetValue, TView> { UnitOfWorkHelper <MixCmsContext> .InitTransaction(_context, _transaction, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot); try { Expression <Func <MixAttributeSetValue, bool> > valPredicate = m => m.AttributeSetName == attributeSetName; RepositoryResponse <List <TView> > result = new RepositoryResponse <List <TView> >() { IsSucceed = true, Data = new List <TView>() }; foreach (var fieldQuery in queries) { Expression <Func <MixAttributeSetValue, bool> > pre = GetValueFilter(filterType, fieldQuery.Key, fieldQuery.Value); valPredicate = valPredicate.AndAlso(pre); } var query = context.MixAttributeSetValue.Where(valPredicate).Select(m => m.DataId).Distinct(); var dataIds = query.ToList(); if (query != null) { Expression <Func <MixAttributeSetValue, bool> > predicate = m => dataIds.Any(id => m.DataId == id) && m.AttributeFieldName == responseName; result = await DefaultRepository <MixCmsContext, MixAttributeSetValue, TView> .Instance.GetModelListByAsync( predicate, context, transaction); } return(result); } catch (Exception ex) { return(UnitOfWorkHelper <MixCmsContext> .HandleException <List <TView> >(ex, isRoot, transaction)); } finally { if (isRoot) { //if current Context is Root UnitOfWorkHelper <MixCmsContext> .CloseDbContext(ref context, ref transaction); } } }
public override async Task <RepositoryResponse <UpdateViewModel> > SaveModelAsync(bool isSaveSubModels = false, MixCmsContext _context = null, IDbContextTransaction _transaction = null) { UnitOfWorkHelper <MixCmsContext> .InitTransaction(_context, _transaction, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot); try { var result = await base.SaveModelAsync(isSaveSubModels, context, transaction); // if save current data success and there is related parent data if (result.IsSucceed && !string.IsNullOrEmpty(ParentId)) { MixDatabaseDataAssociations.UpdateViewModel nav = new MixDatabaseDataAssociations.UpdateViewModel() { DataId = result.Data.Id, Specificulture = Specificulture, MixDatabaseId = result.Data.MixDatabaseId, MixDatabaseName = result.Data.MixDatabaseName, ParentId = ParentId, ParentType = ParentType, IsClone = IsClone, Cultures = Cultures }; var saveNav = await nav.SaveModelAsync(true, context, transaction); if (IsClone) { } result.IsSucceed = result.IsSucceed && saveNav.IsSucceed; result.Errors = saveNav.Errors; result.Exception = saveNav.Exception; } UnitOfWorkHelper <MixCmsContext> .HandleTransaction(result.IsSucceed, isRoot, transaction); return(result); } catch (Exception ex) { return(UnitOfWorkHelper <MixCmsContext> .HandleException <UpdateViewModel>(ex, isRoot, transaction)); } finally { if (isRoot) { UnitOfWorkHelper <MixCmsContext> .CloseDbContext(ref context, ref transaction); } } }
public static async Task <RepositoryResponse <bool> > ImportLanguages(List <MixLanguage> arrLanguage, string destCulture , MixCmsContext _context = null, IDbContextTransaction _transaction = null) { var result = new RepositoryResponse <bool>() { IsSucceed = true }; UnitOfWorkHelper <MixCmsContext> .InitTransaction(_context, _transaction, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot); try { foreach (var item in arrLanguage) { var lang = new UpdateViewModel(item, context, transaction); lang.Specificulture = destCulture; lang.CreatedDateTime = DateTime.UtcNow; var saveResult = await lang.SaveModelAsync(false, context, transaction); result.IsSucceed = result.IsSucceed && saveResult.IsSucceed; if (!result.IsSucceed) { result.Exception = saveResult.Exception; result.Errors = saveResult.Errors; break; } } UnitOfWorkHelper <MixCmsContext> .HandleTransaction(result.IsSucceed, isRoot, transaction); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { var error = UnitOfWorkHelper <MixCmsContext> .HandleException <ReadMvcViewModel>(ex, isRoot, transaction); result.IsSucceed = false; result.Errors = error.Errors; result.Exception = error.Exception; } finally { if (isRoot) { context.Dispose(); } } return(result); }
private static void KernelOnComponentRegistered(string key, IHandler handler) { if (UnitOfWorkHelper.IsApplicationBasedConventionalUowClass(handler.ComponentModel.Implementation)) { handler.ComponentModel.Interceptors.AddFirst(new InterceptorReference(typeof(ExceptionInterceptor))); handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(UnitOfWorkInterceptor))); } else if ( handler.ComponentModel.Implementation.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) .Any(UnitOfWorkHelper.HasUnitOfWorkAttribute)) { //Intercept all methods of classes those have at least one method that has UnitOfWork attribute. //TODO: Intecept only UnitOfWork methods, not other methods! handler.ComponentModel.Interceptors.AddFirst(new InterceptorReference(typeof(ExceptionInterceptor))); handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(UnitOfWorkInterceptor))); } }
public override void ExpandView(MixCmsContext _context = null, IDbContextTransaction _transaction = null) { UnitOfWorkHelper <MixCmsContext> .InitTransaction( _context, _transaction, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot); if (Obj == null) { Obj = Helper.ParseData(Id, Specificulture, context, transaction); } if (isRoot) { transaction.Dispose(); context.Dispose(); } }
public static async System.Threading.Tasks.Task <RepositoryResponse <List <ReadViewModel> > > UpdateInfosAsync(List <ReadViewModel> data) { MixCmsContext context = new MixCmsContext(); var transaction = context.Database.BeginTransaction(); var result = new RepositoryResponse <List <ReadViewModel> >(); try { foreach (var item in data) { var model = context.MixModuleData.FirstOrDefault(m => m.Id == item.Id && m.Specificulture == item.Specificulture); if (model != null) { model.Priority = item.Priority; context.Entry(model).State = Microsoft.EntityFrameworkCore.EntityState.Modified; } } result.IsSucceed = (await context.SaveChangesAsync()) > 0; if (!result.IsSucceed) { result.Errors.Add("Nothing changed"); } UnitOfWorkHelper <MixCmsContext> .HandleTransaction(result.IsSucceed, true, transaction); return(result); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { UnitOfWorkHelper <MixCmsContext> .HandleException <ReadViewModel>(ex, true, transaction); return(new RepositoryResponse <List <ReadViewModel> >() { IsSucceed = false, Data = null, Exception = ex }); } finally { //if current Context is Root transaction.Dispose(); context.Dispose(); } #endregion Expands }
public static async Task <RepositoryResponse <bool> > ActivedThemeAsync( int themeId, string themeName, string culture, MixCmsContext _context = null, IDbContextTransaction _transaction = null) { UnitOfWorkHelper <MixCmsContext> .InitTransaction(_context, _transaction, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot); try { var result = new RepositoryResponse <bool>() { IsSucceed = true }; var saveResult = await SaveNewConfigAsync(MixAppSettingKeywords.ThemeName, themeName, culture, context, transaction); if (saveResult.IsSucceed) { saveResult = await SaveNewConfigAsync(MixAppSettingKeywords.ThemeFolder, themeName, culture, context, transaction); } ViewModelHelper.HandleResult(saveResult, ref result); if (result.IsSucceed) { saveResult = await SaveNewConfigAsync(MixAppSettingKeywords.ThemeId, themeId.ToString(), culture, context, transaction); ViewModelHelper.HandleResult(saveResult, ref result); } UnitOfWorkHelper <MixCmsContext> .HandleTransaction(result.IsSucceed, isRoot, transaction); return(result); } catch (Exception ex) { return(UnitOfWorkHelper <MixCmsContext> .HandleException <bool>(ex, isRoot, transaction)); } finally { if (isRoot) { context.Dispose(); } } }
public static async Task <RepositoryResponse <bool> > ImportLanguages(List <MixLanguage> arrLanguage, string destCulture) { UnitOfWorkHelper <MixCmsContext> .InitTransaction(null, null, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot); var result = new RepositoryResponse <bool>() { IsSucceed = true }; try { foreach (var item in arrLanguage) { var lang = new SystemLanguageViewModel(item, context, transaction); lang.Specificulture = destCulture; var saveResult = await lang.SaveModelAsync(false, context, transaction); result.IsSucceed = result.IsSucceed && saveResult.IsSucceed; if (!result.IsSucceed) { result.Exception = saveResult.Exception; result.Errors = saveResult.Errors; break; } } UnitOfWorkHelper <MixCmsContext> .HandleTransaction(result.IsSucceed, true, transaction); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { var error = UnitOfWorkHelper <MixCmsContext> .HandleException <SystemLanguageViewModel>(ex, true, transaction); result.IsSucceed = false; result.Errors = error.Errors; result.Exception = error.Exception; } finally { //if current Context is Root if (isRoot) { context.Database.CloseConnection(); transaction.Dispose(); context.Dispose(); } } return(result); }