Esempio n. 1
0
        /// <summary>
        /// Get context object associated with the service of the specified type.
        /// </summary>
        /// <param name="typeFullName"></param>
        /// <returns></returns>
        public IActionContext GetContext(string typeFullName)
        {
            EntityRegistrationContext ctx           = EntityRegistration.GetRegistrationContext(typeFullName);
            IActionContext            actionContext = Activator.CreateInstance(ctx.ActionContextType) as IActionContext;

            return(actionContext);
        }
Esempio n. 2
0
        /// <summary>
        /// Initialize the service, repository creators.
        /// </summary>
        /// <param name="serviceCreator">The service creator.</param>
        /// <param name="repoCreator">The repository creator.</param>
        /// <param name="configureRepository">Whether or not to configure the reposiory.</param>
        public static void Init(Func <IEntityService <T> > serviceCreator, Func <IRepository <T> > repoCreator, Func <IEntityValidator> validatorCreator,
                                IEntitySettings settings, IEntityResources resources, bool configureRepository, string connId)
        {
            EntityRegistration.Register <T>(serviceCreator, repoCreator, validatorCreator, settings, resources, configureRepository, connId);
            var        ctx      = EntityRegistration.GetRegistrationContext(typeof(T).FullName);
            MethodInfo callback = typeof(T).GetMethod("OnAfterInit");

            if (callback != null)
            {
                callback.Invoke(null, null);
            }

            // Setup flags for entity life-cycle callbacks.
            ctx.HasOnBeforeValidate       = HasMethod("OnBeforeValidate");
            ctx.HasOnBeforeValidateCreate = HasMethod("OnBeforeValidateCreate");
            ctx.HasOnBeforeValidateUpdate = HasMethod("OnBeforeValidateUpdate");
            ctx.HasOnBeforeCreate         = HasMethod("OnBeforeCreate");
            ctx.HasOnBeforeUpdate         = HasMethod("OnBeforeUpdate");
            ctx.HasOnBeforeSave           = HasMethod("OnBeforeSave");
            ctx.HasOnBeforeDelete         = HasMethod("OnBeforeDelete");

            ctx.HasOnAfterValidate       = HasMethod("OnAfterValidate");
            ctx.HasOnAfterValidateCreate = HasMethod("OnAfterValidateCreate");
            ctx.HasOnAfterValidateUpdate = HasMethod("OnAfterValidateUpdate");
            ctx.HasOnAfterCreate         = HasMethod("OnAfterCreate");
            ctx.HasOnAfterUpdate         = HasMethod("OnAfterUpdate");
            ctx.HasOnAfterSave           = HasMethod("OnAfterSave");
            ctx.HasOnAfterDelete         = HasMethod("OnAfterDelete");
        }
Esempio n. 3
0
        /// <summary>
        /// Get a new entity of the specified type.
        /// </summary>
        /// <param name="typeFullName"></param>
        /// <returns></returns>
        public object GetEntity(string typeFullName)
        {
            EntityRegistrationContext ctx = EntityRegistration.GetRegistrationContext(typeFullName);
            object entity = Activator.CreateInstance(ctx.EntityType);

            return(entity);
        }
Esempio n. 4
0
        /// <summary>
        /// Get the entity Service(supporting CRUD operations).
        /// </summary>
        /// <param name="typeFullName"></param>
        /// <returns></returns>
        public object GetService(string typeFullName)
        {
            EntityRegistrationContext ctx = EntityRegistration.GetRegistrationContext(typeFullName);
            string serviceName            = ctx.Name + EntityRegistrationConstants.SuffixService;
            object obj = Ioc.GetObject <object>(serviceName);

            return(obj);
        }
Esempio n. 5
0
        /// <summary>
        /// Get an object from the IocContainer using the type's full name and a suffix.
        /// </summary>
        /// <param name="typeFullName"></param>
        /// <param name="suffix"></param>
        /// <returns></returns>
        public object GetObject(string typeFullName, string suffix)
        {
            EntityRegistrationContext ctx = EntityRegistration.GetRegistrationContext(typeFullName);
            string entryName = ctx.Name + suffix;
            object obj       = Ioc.GetObject <object>(entryName);

            return(obj);
        }
Esempio n. 6
0
        /// <summary>
        /// Deletes the model from the repository.
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public virtual void Delete(IActionContext ctx)
        {
            var entityCtx = EntityRegistration.GetRegistrationContext(typeof(T).FullName);
            T   entity    = default(T);

            if (ctx.Item != null)
            {
                entity = (T)ctx.Item;
            }
            else
            {
                entity   = Get(ctx.Id);
                ctx.Item = entity;
            }

            // already deleted
            if (entity == null)
            {
                return;
            }

            var result = PerformAction(ctx, delegate(IActionContext context)
            {
                if (entityCtx.HasOnBeforeDelete)
                {
                    entity.OnBeforeDelete(ctx);
                }

                if (context.Item == null)
                {
                    _repository.Delete(context.Id);
                }
                else
                {
                    _repository.Delete((T)context.Item);
                }

                if (entityCtx.HasOnAfterDelete)
                {
                    entity.OnAfterDelete();
                }
            },
                                       EntityAction.Delete);
        }
Esempio n. 7
0
        /// <summary>
        /// Updates the model in the repository.
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="executor">The action to execute</param>
        /// <returns></returns>
        protected virtual void InternalUpdate(IActionContext ctx, Action <IActionContext> executor)
        {
            var     entityCtx = EntityRegistration.GetRegistrationContext(typeof(T).FullName);
            IEntity entity    = ctx.Item as IEntity;

            // Do the action(create)
            var result = PerformAction(ctx, context =>
            {
                // 1. Clear errors first?
                if (entityCtx.IsEntityErrorCollectionClearedBeforePopulation)
                {
                    entity.Errors.Clear();
                }

                // 2. Life-cycle callbacks before validation.
                if (entityCtx.HasOnBeforeValidate)
                {
                    entity.OnBeforeValidate(ctx);
                }
                if (entityCtx.HasOnBeforeValidateUpdate)
                {
                    entity.OnBeforeValidateUpdate(ctx);
                }

                // 3: Validate the entity.
                var validResult = PerformValidation(ctx, EntityAction.Update);
                if (!validResult.Success)
                {
                    return;
                }

                // 4. Life-cycle callbacks after validation.
                if (entityCtx.HasOnAfterValidateUpdate)
                {
                    entity.OnAfterValidateUpdate();
                }
                if (entityCtx.HasOnAfterValidate)
                {
                    entity.OnAfterValidate();
                }
                if (entityCtx.HasOnBeforeSave)
                {
                    entity.OnBeforeSave(ctx);
                }
                if (entityCtx.HasOnBeforeUpdate)
                {
                    entity.OnBeforeUpdate(ctx);
                }

                // 5. Persist the entity.
                if (executor == null)
                {
                    _repository.Update((T)context.Item);
                }
                else
                {
                    executor(context);
                }

                // 6. Life-cycle callbacks after persistance.
                if (entityCtx.HasOnAfterUpdate)
                {
                    entity.OnAfterUpdate();
                }
                if (entityCtx.HasOnAfterSave)
                {
                    entity.OnAfterSave();
                }
            }, EntityAction.Update);
        }
Esempio n. 8
0
        /// <summary>
        /// Creates the entity.
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="executor">The executor to call to create the entity.
        /// If empty, _repository.Create(entity) is automatically called.</param>
        /// <returns></returns>
        protected virtual void InternalCreate(IActionContext ctx, Action <IActionContext> executor)
        {
            // If the ctx errors are null, then use the errors from the entity.errors collection.
            // This is used for the following use cases:
            // 1. Error collecting into separate unified error collection.
            // 2. Import / Export
            // 3. Batch processing
            var     entityCtx = EntityRegistration.GetRegistrationContext(typeof(T).FullName);
            IEntity entity    = ctx.Item as IEntity;

            // Do the action(create)
            var result = PerformAction(ctx, context =>
            {
                // 1. Clear errors first?
                if (entityCtx.IsEntityErrorCollectionClearedBeforePopulation)
                {
                    entity.Errors.Clear();
                }

                // 2. Life-cycle callbacks before validation.
                if (entityCtx.HasOnBeforeValidate)
                {
                    entity.OnBeforeValidate(ctx);
                }
                if (entityCtx.HasOnBeforeValidateCreate)
                {
                    entity.OnBeforeValidateCreate(ctx);
                }

                // 3: Validate the entity.
                var validResult = PerformValidation(ctx, EntityAction.Create);
                if (!validResult.Success)
                {
                    return;
                }

                // 4. Life-cycle callbacks after validation.
                if (entityCtx.HasOnAfterValidateCreate)
                {
                    entity.OnAfterValidateCreate();
                }
                if (entityCtx.HasOnAfterValidate)
                {
                    entity.OnAfterValidate();
                }
                if (entityCtx.HasOnBeforeSave)
                {
                    entity.OnBeforeSave(ctx);
                }
                if (entityCtx.HasOnBeforeCreate)
                {
                    entity.OnBeforeCreate(ctx);
                }

                // 5. Persist the entity.
                if (executor == null)
                {
                    _repository.Create((T)context.Item);
                }
                else
                {
                    executor(context);
                }

                // 6. Life-cycle callbacks after persistance.
                if (entityCtx.HasOnAfterCreate)
                {
                    entity.OnAfterCreate();
                }
                if (entityCtx.HasOnAfterSave)
                {
                    entity.OnAfterSave();
                }
            }, EntityAction.Create);
        }