public CheckResult SaveFile(FileBlobModel model)
        {
            if (!model.IsChanged)
                return new CheckResult(model);

            using (DSModel db = DB.GetContext())
            {
                var check = FileBlobValidator.ValidateSave(db, model);
                if (check.Failed)
                    return check;

                KeyBinder key = new KeyBinder();
                try
                {
                    FileBlobRepository.SaveBlob(db, key, model);
                    db.SaveChanges();
                    key.BindKeys();
                    model.IsChanged = false;
                    return new CheckResult(model);
                }
                catch (Exception ex)
                {
                    key.RollbackKeys();
                    return new CheckResult(ex);
                }
            }
        }
 public CheckResult SaveHolidays(List<HolidayModel> model)
 {
     try
     {
         using (var db = DB.GetContext())
         {
             KeyBinder key = new KeyBinder();
             HolidayRepository.SaveHolidays(db, key, model);
             db.SaveChanges();
             key.BindKeys();
             model.ForEach(f => f.IsChanged = false);
             return new CheckResult();
         }
     }
     catch (Exception ex)
     {
         return new CheckResult(ex);
     }
 }
        public CheckResult SaveDispatch(DispatchModel model)
        {
            try
            {
                using (var db = DB.GetContext())
                {
                    var check = DispatchValidator.ValidateSave(db, model);
                    if (check.Failed)
                        return check;

                    KeyBinder key = new KeyBinder();
                    DispatchRepository.SaveDispatch(db, key, model);
                    db.SaveChanges();
                    key.BindKeys();
                    return check;
                }
            }
            catch (Exception ex)
            {
                return new CheckResult(ex);
            }
        }
        public CheckResult SaveUser(UserModel user)
        {
            try
            {
                using (var db = DB.GetContext())
                {
                    var check = UserValidator.ValidateSave(db, user);
                    if (check.Failed)
                        return check;

                    KeyBinder key = new KeyBinder();
                    UserRepository.SaveUser(db, key, user);
                    db.SaveChanges();
                    key.BindKeys();
                    user.IsChanged = false;
                    return check;
                }
            }
            catch (Exception ex)
            {
                return new CheckResult(ex);
            }
        }
        public CheckResult SaveCompany(CompanyModel model, List<CompanyLicensePayRateModel> licenseRates, List<CompanyInvoicingPayRateModel> invoiceRates)
        {
            try
            {
                using (var db = DB.GetContext())
                {
                    var check = CompanyValidator.ValidateSave(db, model);
                    if (check.Failed)
                        return check;

                    var checkLicense = CompanyValidator.ValidateDriverRates(db, licenseRates);
                    if (checkLicense.Failed)
                        return checkLicense;

                    var checkInvoice = CompanyValidator.ValidateInvoiceRates(db, invoiceRates);
                    if (checkInvoice.Failed)
                        return checkInvoice;

                    KeyBinder key = new KeyBinder();
                    CompanyRepository.SaveCompany(db, key, model);
                    if (model.CompanyID != 0)
                    {
                        CompanyRepository.SaveLicensesPayRates(db, key, licenseRates);
                        CompanyRepository.SaveInvoicePayRates(db, key, invoiceRates);
                    }
                    db.SaveChanges();
                    key.BindKeys();
                    model.IsChanged = false;

                    return check;
                }
            }
            catch (Exception ex)
            {
                return new CheckResult(ex);
            }
        }
        public CheckResult Save()
        {
            var mod = this.ActiveModel;
            try
            {
                using (var db = DB.GetContext())
                {
                    var check = DriverMedicalValidator.ValidateSave(db, mod);
                    if (check.Failed)
                        return check;

                    KeyBinder key = new KeyBinder();
                    DriverMedicalRepository.SaveMedical(db, key, mod);
                    db.SaveChanges();
                    key.BindKeys();
                    mod.IsChanged = false;
                    return check;
                }
            }
            catch (Exception ex)
            {
                return new CheckResult(ex);
            }
        }
        public CheckResult AttachMultipleFiles(string[] files)
        {
            using (var db = DB.GetContext())
            {
                KeyBinder key = new KeyBinder();
                try
                {
                    foreach (string file in files)
                    {
                        FileBlobModel mod = new FileBlobModel();
                        mod.BlobName = Path.GetFileNameWithoutExtension(file);
                        mod.BlobExtension = Path.GetExtension(file);
                        mod.BlobData = File.ReadAllBytes(file);
                        mod.DriverID = this.CreationInfo.DriverID;

                        FileBlobRepository.SaveBlob(db, key, mod);
                    }

                    db.SaveChanges();
                    key.BindKeys();
                    return new CheckResult();
                }
                catch (Exception ex)
                {
                    key.RollbackKeys();
                    return new CheckResult(ex);
                }
            }
        }
        public CheckResult Save()
        {
            var model = this.ActiveModel;

            try
            {
                using (var db = DB.GetContext())
                {
                    KeyBinder key = new KeyBinder();

                    model.Permits.Clear();
                    var permits = this.Permits
                        .Where(p => p.IsCheck)
                        .Select(p =>
                            new DriverLicensePermitModel()
                            {
                                PermitID = p.Value
                            });
                    model.Permits.AddRange(permits);

                    var check = DriverLicenseValidator.ValidateSave(db, model);
                    if (check.Failed)
                        return check;

                    DriverLicenseRepository.SaveDriverLicense(db, key, model);
                    db.SaveChanges();
                    key.BindKeys();
                    model.IsChanged = false;
                    return check;
                }
            }
            catch (Exception ex)
            {
                return new CheckResult(ex);
            }
        }