public async Task<OpObject> SendMail(CMail Mail)
 {
     IsolatedContext IsoContext = new IsolatedContext();
     return await Execute(new Func<Task>(async () =>
     {
         /* Oh no, problem is STService Town, contact the organization repo */
         using (MaintienceRepository MainRepo = new MaintienceRepository())
         {
             /* Well then, lets try to create this organization */
             IsoContext.RetObj = await MainRepo.InsertMail(Mail);
         }
     }), IsoContext);
 }
        /* Get User Info for User */
        public async Task<OpObject> GetUserInformationForUser(String UserGuid)
        {
            /* Create return object */
            IsolatedContext IsoContext = new IsolatedContext();

            /* Execute function */
            return await Execute(new Func<Task>(async () =>
            {
                using (UserRepository UserRepo = new UserRepository())
                {
                    /* Done */
                    IsoContext.RetObj = await UserRepo.Get(UserGuid);
                }
            }), IsoContext);
        }
        /* Create User */
        public async Task<OpObject> CreateUser(CCustomer CustomerInfo)
        {
            /* Create return object */
            IsolatedContext IsoContext = new IsolatedContext();

            /* Execute function */
            return await Execute(new Func<Task>(async () =>
            {
                using (UserRepository UserRepo = new UserRepository())
                {
                    /* Done */
                    IsoContext.RetObj = await UserRepo.Create(CustomerInfo, true);

                    /* Sanity */
                    if (IsoContext.RetObj.Code != StatusCode.Ok)
                        return;

                    /* Create a confirmation code and send the email 
                     * to the given user */
                    using (MaintienceRepository MainRepo = new MaintienceRepository()) {
                        /* Create a new confcode */
                        String uId = (String)IsoContext.RetObj.Data;
                        String ConfCode = await MainRepo.CreateCode(CodeType.Confirmation, uId);

                        /* Send a new mail */
                        IsoContext.RetObj = await MainRepo.InsertMail(new CMail() {
                            MailTo = CustomerInfo.Email,
                            Type = MailType.NoReply,
                            Subject = "NRG Models Confirmation",
                            Body = "",
                            IsHtml = false
                        });
                    }
                }
            }), IsoContext);
        }
        private async Task<OpObject> Execute(Func<Task> a, IsolatedContext IsoContext)
        {
            try
            {
                /* Convert session -> guid? */
                if (IsoContext.SessionId != "0")
                {
                    using (MaintienceRepository MainRepo = new MaintienceRepository())
                    {
                        String Result = await MainRepo.IsSessionValid(IsoContext.SessionId);

                        /* Sanity */
                        if (Result == "0")
                            return new OpObject(StatusCode.InvalidSession, "SessionId was invalid");
                        else
                            IsoContext.uGuid = Result;
                    }
                }

                /* Run function */
                await a.Invoke();

                /* Done */
                return IsoContext.RetObj;
            }
            catch (DbEntityValidationException Ex)
            {
                /* Get all validation errors */
                var ErrMsgs = Ex.EntityValidationErrors
                        .SelectMany(x => x.ValidationErrors)
                        .Select(x => x.ErrorMessage);

                /* Combine them */
                String FullErrMsgs = String.Join("; ", ErrMsgs);

                /* Create a new exception message */
                String ExMsg = String.Concat(Ex.Message, " The validation errors are: ", FullErrMsgs);

                /* Return it */
                return new OpObject(StatusCode.InvalidParameters, ExMsg);
            }
            catch (OptimisticConcurrencyException Ex)
            {
                /* Return a try-again */
                return new OpObject(StatusCode.TryAgain, Ex.ToString());
            }
            catch (DbUpdateException Ex)
            {
                return new OpObject(StatusCode.TryAgain, Ex.ToString());
            }
            catch (Exception Ex)
            {
                /* Invalid Params? */
                if (Ex is NullReferenceException ||
                    Ex is ArgumentNullException ||
                    Ex is ArgumentException ||
                    Ex is ArgumentOutOfRangeException)
                {
                    /* Return a arg-exception */
                    return new OpObject(StatusCode.InvalidParameters, Ex.ToString());
                }

                /* Return fault */
                return new OpObject(StatusCode.FuckedUp, Ex.ToString());
            }
        }
        /* Delete User */
        public async Task<OpObject> DeleteUser(String SessionId, String eMail, String Password)
        {
            /* Create return object */
            IsolatedContext IsoContext = new IsolatedContext(SessionId);

            /* Execute function */
            return await Execute(new Func<Task>(async () =>
            {
                /* We need user repository for this */
                using (UserRepository UserRepo = new UserRepository())
                {
                    /* Does password and email match? */
                    IsoContext.RetObj = await UserRepo.TestPassword(eMail, Password);

                    /* Sanity */
                    if (IsoContext.RetObj.Code != StatusCode.Ok)
                        return;

                    /* Try to delete */
                    IsoContext.RetObj = await UserRepo.Delete(IsoContext.uGuid);
                }
            }), IsoContext);
        }
        /* Login */
        public async Task<OpObject> Login(String eMail, String Password)
        {
            /* Create return object */
            IsolatedContext IsoContext = new IsolatedContext();

            /* Execute function */
            return await Execute(new Func<Task>(async () =>
            {
                using (UserRepository UserRepo = new UserRepository())
                {
                    /* Does password and email match? */
                    IsoContext.RetObj = await UserRepo.TestPassword(eMail, Password);

                    /* Return ConfCode */
                    using (MaintienceRepository MainRepo = new MaintienceRepository())
                    {
                        /* Very Sanity */
                        if (IsoContext.RetObj.Code == StatusCode.UserNotConfirmed)
                        {
                            /* Lookup */
                            String ConfCode =
                                await MainRepo.GetCode(CodeType.Confirmation, (String)IsoContext.RetObj.Data);

                            /* Sanity */
                            if (ConfCode == "0")
                            {
                                ConfCode = await MainRepo.CreateCode(CodeType.Confirmation,
                                    (String)IsoContext.RetObj.Data);
                            }

                            /* Set */
                            IsoContext.RetObj.Data = ConfCode;
                        }

                        /* Sanity */
                        if (IsoContext.RetObj.Code != StatusCode.Ok)
                            return;

                        /* Are we logged in already? */
                        String SessionId = await MainRepo.HasSession((String)IsoContext.RetObj.Data);

                        if (SessionId != "0")
                        {
                            IsoContext.RetObj = new OpObject(StatusCode.Ok, SessionId);
                            return;
                        }

                        /* No, create a session */
                        IsoContext.RetObj.Data = await MainRepo.CreateSessionId((String)IsoContext.RetObj.Data);
                    }
                }
            }), IsoContext);
        }
        /* Confirm User */
        public async Task<OpObject> ConfirmUser(String ConfirmationId)
        {
            /* Create return object */
            IsolatedContext IsoContext = new IsolatedContext();

            /* Execute function */
            return await Execute(new Func<Task>(async () =>
            {
                /* Get user guid */
                String uGuid = "0";
                using (MaintienceRepository MainRepo = new MaintienceRepository())
                {
                    /* Delete */
                    uGuid = await MainRepo.DeleteCode(ConfirmationId);
                }

                /* Sanity */
                if (uGuid == "0")
                {
                    IsoContext.RetObj = new OpObject(StatusCode.InvalidParameters);
                    return;
                }

                /* Access user repo */
                using (UserRepository UserRepo = new UserRepository())
                {
                    /* Confirm the user */
                    IsoContext.RetObj = await UserRepo.Confirm(uGuid);
                }
            }), IsoContext);
        }
        /* Forgot Password */
        public async Task<OpObject> ForgotPassword(String eMail)
        {
            /* Create return object */
            IsolatedContext IsoContext = new IsolatedContext();

            /* Execute function */
            return await Execute(new Func<Task>(async () =>
            {
                using (UserRepository UserRepo = new UserRepository())
                {
                    /* Done */
                    IsoContext.RetObj = await UserRepo.GetGuid(eMail);

                    /* Sanity */
                    if (IsoContext.RetObj.Code != StatusCode.Ok)
                        return;

                    /* Get confirmation code */
                    using (MaintienceRepository MainRepo = new MaintienceRepository())
                    {
                        String ConfCode = await MainRepo.CreateCode(CodeType.Recovery,
                            (String)IsoContext.RetObj.Data);

                        /* Save it */
                        IsoContext.RetObj.Data = ConfCode;
                    }
                }
            }), IsoContext);
        }
        /* Change Password */
        public async Task<OpObject> ChangePassword(String SessionId, String OldPassword, String NewPassword)
        {
            /* Create return object */
            IsolatedContext IsoContext = new IsolatedContext(SessionId);

            /* Execute function */
            return await Execute(new Func<Task>(async () =>
            {
                /* We need user repository for this */
                using (UserRepository UserRepo = new UserRepository())
                {
                    /* Does old password match? */
                    IsoContext.RetObj = await UserRepo.EditPassword(IsoContext.uGuid, OldPassword, NewPassword);
                }
            }), IsoContext);
        }