public int CreateHero(Hero hero, IEnumerable<IPostedFile> photos)
 {
     var heroId = 0;
     using (var tran = new DbTransactionManager())
     {
         tran.BeginTransaction();
         using (var db = new DataAccess(tran))
         {
             db.CreateStoredProcCommand("dbo.CreateHero");
             db.AddInputParameter("@FirstName", DbType.String, hero.FirstName);
             db.AddInputParameter("@LastName", DbType.String, hero.LastName);
             db.AddInputParameter("@MiddleName", DbType.String, hero.MiddleName);
             db.AddInputParameter("@Dob", DbType.DateTime, hero.Dob);
             db.AddInputParameter("@Died", DbType.DateTime, hero.Died);
             db.AddInputParameter("@MilitaryGroupId", DbType.Int32, hero.MilitaryGroupId);
             db.AddInputParameter("@MilitaryRankId", DbType.Int32, hero.MilitaryRankId);
             db.AddInputParameter("@MilitaryAwardId", DbType.Int32, hero.MilitaryAwardId);
             db.AddInputParameter("@Biography", DbType.String, hero.Biography);
             db.AddInputParameter("@LanguageId", DbType.Int32, hero.LanguageId);
             db.AddInputParameter("@UpdatedBy", DbType.Int32, hero.UpdatedBy);
             db.AddInputParameter("@Photos", DbType.Xml, photos.ToDbXml());
             db.AddOutputParameter("@HeroId", DbType.Int32);
             try
             {
                 db.ExecuteNonQuery();
                 heroId = db.GetParameterValue<int>("@HeroId");
                 SaveBlobs(heroId, photos);
                 tran.Commit();
             }
             catch (SqlException ex)
             {
                 tran.Rollback();
                 Logger.Error("Error while creating hero", ex);
                 throw new RepositoryException("Oops! Something went wrong... :(", ex);
             }
             catch (Exception ex)
             {
                 tran.Rollback();
                 Logger.Error("Error while creating hero", ex);
                 throw new RepositoryException("Oops! Something went wrong... :(", ex);
             }
             return heroId;
         }
     }
 }
        public JsonResult Save(HeroModel model, IEnumerable<HttpPostedFileBase> images) 
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var hero = new Hero();
                    var photos = new List<IPostedFile>();
                    Mapper.Map(model, hero);

                    if (!images.IsNullOrEmpty())
                    {
                        foreach (var image in images)
                        {
                            if (image.ContentLength > 0)
                            {
                                var file = new PostedFile(image);
                                if (image.FileName.Equals(model.Thumbnail, StringComparison.OrdinalIgnoreCase))
                                {
                                    file.Attributes.Add("IsThumbnail", "1");
                                }
                                file.Attributes.Add("FileName", string.Concat(Guid.NewGuid(), Path.GetExtension(image.FileName)));
                                photos.Add(file);
                            }
                        }
                    }
                    hero.UpdatedBy = AppUser.UserId;
                    if (model.IsNew)
                    {
                        hero.LanguageId = AppUser.LanguageId;
                        hero.HeroId = _heroRepository.CreateHero(hero, photos);
                    }
                    else
                    {
                        _heroRepository.UpdateHero(hero, photos);
                    }
                    return Json(new { Ok = true, HeroId = hero.HeroId });
                }
                catch (Exception)
                {                    
                }
            }
            return Json(new {Ok = false});
        }
 public Hero GetRandomHero(int languageId)
 {
     using (var db = new DataAccess())
     {
         db.CreateStoredProcCommand("dbo.GetRandomHero");
         db.AddInputParameter("@LanguageId", DbType.Int32, languageId);
         var hero = new Hero();
         try
         {
             db.ReadInto(hero, hero.MilitaryGroup, hero.MilitaryRank, hero.MilitaryAward, hero.Photos);
             GetBlobs(hero, new CloudDataAccess());
         }
         catch (Exception ex)
         {
             Logger.Error("Error while getting random hero", ex);
             throw;
         }
         return hero;
     }
 }
 private void GetBlobs(Hero hero, CloudDataAccess cloud)
 {
     if (hero.HeroId.HasValue)
     {
         var baseUri = cloud.GetBaseUri("images/hero", hero.HeroId.ToString());
         GetBlobs(baseUri, hero.Photos);
     }
 }
        public void UpdateHero(Hero hero, IEnumerable<IPostedFile> photos)
        {
            using (var tran = new DbTransactionManager())
            {
                try
                {
                    tran.BeginTransaction();
                    using (var db = new DataAccess(tran))
                    {
                        db.CreateStoredProcCommand("dbo.UpdateHero");
                        db.AddInputParameter("@HeroId", DbType.Int32, hero.HeroId);
                        db.AddInputParameter("@FirstName", DbType.String, hero.FirstName);
                        db.AddInputParameter("@LastName", DbType.String, hero.LastName);
                        db.AddInputParameter("@MiddleName", DbType.String, hero.MiddleName);
                        db.AddInputParameter("@Dob", DbType.DateTime, hero.Dob);
                        db.AddInputParameter("@Died", DbType.DateTime, hero.Died);
                        db.AddInputParameter("@MilitaryGroupId", DbType.Int32, hero.MilitaryGroupId);
                        db.AddInputParameter("@MilitaryRankId", DbType.Int32, hero.MilitaryRankId);
                        db.AddInputParameter("@MilitaryAwardId", DbType.Int32, hero.MilitaryAwardId);
                        db.AddInputParameter("@Biography", DbType.String, hero.Biography);
                        db.AddInputParameter("@LanguageId", DbType.Int32, hero.LanguageId);
                        db.AddInputParameter("@UpdatedBy", DbType.Int32, hero.UpdatedBy);
                        db.AddInputParameter("@Photos", DbType.Xml, photos.ToDbXml());

                        db.ExecuteNonQuery();
                        SaveBlobs(hero.HeroId.Value, photos);
                        tran.Commit();
                    }
                }
                catch (Exception ex)
                {                    
                    tran.Rollback();
                    Logger.Error("Error while updating hero", ex);
                    throw;
                }
            }
        }