예제 #1
0
        protected ActionResult DeleteInstanceOf(Type modelType, object key, Func <ActionResult> onSuccess, Func <Exception, ActionResult> onException, Func <ActionResult> onNotFound)
        {
            using (var context = ModelAssemblies.GetContext(modelType))
            {
                try
                {
                    var set        = context.Set(modelType);
                    var descriptor = new ModelDescriptor(ModelMappingManager.FindByType(modelType));
                    var instance   = set.Find(Convert.ChangeType(key, descriptor.KeyProperty.PropertyType));

                    if (instance == null)
                    {
                        return(onNotFound());
                    }

                    set.Remove(instance);
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                    return(onException(ex));
                }
                return(onSuccess());
            }
        }
예제 #2
0
        protected ActionResult EditInstanceOf(Type modelType, Model model, Func <object, ActionResult> onSuccess, Func <Exception, ActionResult> onException, Func <ActionResult> onNotFound)
        {
            using (var context = ModelAssemblies.GetContext(modelType))
            {
                try
                {
                    if (model.Instance == null)
                    {
                        return(onNotFound());
                    }

                    var set      = context.Set(modelType);
                    var attached = set.Attach(model.Instance);

                    if (attached == null)
                    {
                        return(onNotFound());
                    }

                    context.Entry(attached).State = EntityState.Modified;
                    context.SaveChanges();
                    return(onSuccess(attached));
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                    return(onException(ex));
                }
            }
        }
예제 #3
0
        protected static object GetInstanceOf(Type modelType, object key, ModelDescriptor descriptor)
        {
            var context  = ModelAssemblies.GetContext(modelType);
            var set      = context.Set(modelType);
            var instance = set.Find(Convert.ChangeType(key, descriptor.KeyProperty.PropertyType));

            return(instance);
        }
예제 #4
0
        protected Query.Query GetDefaultQueryOf(Type modelType)
        {
            var context = ModelAssemblies.GetContext(modelType);
            var source  = context.Set(modelType);
            var query   = QueryParser.Parse(modelType, source);

            return(query);
        }
예제 #5
0
        protected Query.Query GetQueryById(Type modelType)
        {
            var context = ModelAssemblies.GetContext(modelType);
            var source  = context.Set(modelType);
            var query   = QueryParser.Parse(modelType, source, Request["q"]);

            return(query);
        }
예제 #6
0
        protected ActionResult ExecuteMethodOf(Type modelType, Method model, Func <ActionResult> onSuccess, Func <object, ActionResult> onSuccessWithReturn, Func <Exception, ActionResult> onException, Func <ActionResult> onNotFound, string key = null)
        {
            using (var context = ModelAssemblies.GetContext(modelType))
            {
                try
                {
                    object @return;
                    if (!model.Descriptor.Method.IsStatic)
                    {
                        var descriptor = new ModelDescriptor(ModelMappingManager.FindByType(modelType));
                        var set        = context.Set(modelType);
                        var instance   = set.Find(Convert.ChangeType(key, descriptor.KeyProperty.PropertyType));

                        if (instance == null)
                        {
                            return(onNotFound());
                        }

                        @return = model.Invoke(instance);
                        context.SaveChanges();

                        if (@return == instance)
                        {
                            @return = null;
                        }
                    }
                    else
                    {
                        @return = model.Invoke(null);
                    }

                    if (@return != null)
                    {
                        return(onSuccessWithReturn(@return));
                    }
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                    return(onException(ex));
                }

                return(onSuccess());
            }
        }
예제 #7
0
 protected ActionResult CreateInstanceOf(Type modelType, Method method, Func <object, ActionResult> onSuccess, Func <Exception, ActionResult> onException)
 {
     using (var context = ModelAssemblies.GetContext(modelType))
     {
         try
         {
             var instance = method.Invoke(null);
             var set      = context.Set(modelType);
             set.Add(instance);
             context.SaveChanges();
             return(onSuccess(instance));
         }
         catch (Exception ex)
         {
             HandleException(ex);
             return(onException(ex));
         }
     }
 }