UIOMatic.Controllers.PetaPocoObjectController.PostCreate C# (CSharp) Method

PostCreate() public method

public PostCreate ( System.Dynamic.ExpandoObject objectToCreate ) : object
objectToCreate System.Dynamic.ExpandoObject
return object
        public object PostCreate(ExpandoObject objectToCreate)
        {
            var typeOfObject = objectToCreate.FirstOrDefault(x => x.Key == "typeOfObject").Value.ToString();
            objectToCreate = (ExpandoObject)objectToCreate.FirstOrDefault(x => x.Key == "objectToCreate").Value;

            var ar = typeOfObject.Split(',');
            var currentType = Type.GetType(ar[0] + ", " + ar[1]);

            object ob = Activator.CreateInstance(currentType, null);

            foreach (var prop in objectToCreate)
            {
                if (prop.Value != null)
                {

                    var propKey = prop.Key;

                    PropertyInfo propI = currentType.GetProperty(propKey);
                    Helper.SetValue(ob, propI.Name, prop.Value);

                }
            }

            var uioMaticAttri = (UIOMaticAttribute)Attribute.GetCustomAttribute(currentType, typeof(UIOMaticAttribute));

            var db = (Database)DatabaseContext.Database;
            if (!string.IsNullOrEmpty(uioMaticAttri.ConnectionStringName))
                db = new Database(uioMaticAttri.ConnectionStringName);

            var tableName = ((TableNameAttribute)Attribute.GetCustomAttribute(currentType, typeof(TableNameAttribute))).Value;

            var primaryKeyColum = string.Empty;
            var autoIncrement = true;

            var primKeyAttri = currentType.GetCustomAttributes().Where(x => x.GetType() == typeof(PrimaryKeyAttribute));
            if (primKeyAttri.Any())
            {
                primaryKeyColum = ((PrimaryKeyAttribute)primKeyAttri.First()).Value;
                autoIncrement = ((PrimaryKeyAttribute)primKeyAttri.First()).autoIncrement;
            }

            foreach (var prop in currentType.GetProperties())
            {
                foreach (var attri in prop.GetCustomAttributes(true))
                {
                    if (attri.GetType() == typeof(PrimaryKeyColumnAttribute))
                    {
                        primaryKeyColum = ((PrimaryKeyColumnAttribute)attri).Name ?? prop.Name;
                        autoIncrement = ((PrimaryKeyColumnAttribute)attri).AutoIncrement;
                    }
                }

            }

            var temp = CreatingObject;
            if (temp != null)
                temp(this, new ObjectEventArgs(ob));

            if (autoIncrement)
                db.Insert(tableName,primaryKeyColum,ob);
            else
                db.Insert(ob);
            var tmp = CreatedObject;
            if (tmp != null)
                tmp(this, new ObjectEventArgs(ob));

            return ob;
        }