protected override ICollection <TranslationDto> FetchByIdImpl(Guid phraseId) { var identity = (UserIdentity)Csla.ApplicationContext.User.Identity; using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var retTranslationDtos = new List <TranslationDto>(); //FIND ALL TRANSLATIONS THAT CONTAIN PHRASEDATAS WITH THE CORRESPONDING PHRASEID var translationDatas = (from translationData in ctx.ObjectContext.TranslationDatas where (from phraseData in translationData.PhraseDatas where phraseData.Id == phraseId select phraseData).Count() > 0 select translationData).ToList(); //ADD DTOS OF THOSE TRANSLATION DATAS TO RETURN LIST foreach (var translationData in translationDatas) { var dto = EfHelper.ToDto(translationData); retTranslationDtos.Add(dto); } //RETURN THE TRANSLATIONS return(retTranslationDtos); } }
protected override LanguageDto FetchImpl(string languageText) { var currentUserId = Business.BusinessHelper.GetCurrentUserId(); using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var results = from languageData in ctx.ObjectContext.LanguageDatas where languageData.Text == languageText && languageData.UserDataId == currentUserId select languageData; if (results.Count() == 1) { return(EfHelper.ToDto(results.First())); } else { if (results.Count() == 0) { throw new Exceptions.LanguageTextNotFoundException(languageText); } else { throw new Exceptions.VeryBadException( string.Format(DalResources.ErrorMsgVeryBadException, DalResources.ErrorMsgVeryBadExceptionDetail_ResultCountNotOneOrZero)); } } } }
protected override PhraseDto InsertImpl(PhraseDto dto) { var currentUserId = Business.BusinessHelper.GetCurrentUserId(); PhraseData phraseData = null; using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { //BEFORE INSERTING, WE NEED TO MAKE SURE THAT THE PHRASE.TEXT //AND PHRASE.LANGUAGE.TEXT ARE NOT ALREADY IN THE DB. IF SO, WE NEED //TO CALL AN UPDATE INSTEAD OF INSERT var results = (from data in ctx.ObjectContext.PhraseDatas where data.Text == dto.Text && data.LanguageData.Id == dto.LanguageId && data.UserDataId == currentUserId select data).FirstOrDefault(); if (results != null) { //ALREADY EXISTS IN DB. NEED TO PERFORM AN UPDATE dto.Id = results.Id; //hack: would be faster to effectively duplicate UpdateImpl code here instead of calling UpdateImpl(). (refactor and reshape code). Would be cleaner as well, I think. return(UpdateImpl(dto)); } else { //NO PHRASE EXISTS IN THE DB, SO PERFORM THE INSERT, SAVE CHANGES AND RETURN phraseData = EfHelper.AddToContext(dto, ctx.ObjectContext); ctx.ObjectContext.SaveChanges(); dto.Id = phraseData.Id; return(dto); } } }
//protected override StudyDataDto FetchImpl(Guid id) //{ // var currentUsername = ((UserIdentity)(Csla.ApplicationContext.User.Identity)).Name; // using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) // { // var results = from studyDataData in ctx.ObjectContext.StudyDataDatas // where studyDataData.Id == id && // studyDataData.Username == currentUsername // select studyDataData; // if (results.Count() == 1) // { // var fetchedStudyDataData = results.First(); // StudyDataDto studyDataDto = EfHelper.ToDto(fetchedStudyDataData); // return studyDataDto; // } // else // { // if (results.Count() == 0) // throw new Exceptions.IdNotFoundException(id); // else // { // //results.count is not one or zero. either it's negative, which would be framework absurd, or its more than one, // //which means that we have multiple studyDatas with the same id. this is very bad. // var errorMsg = string.Format(DalResources.ErrorMsgVeryBadException, // DalResources.ErrorMsgVeryBadExceptionDetail_ResultCountNotOneOrZero); // throw new Exceptions.VeryBadException(errorMsg); // } // } // } //} protected override StudyDataDto FetchForCurrentUserImpl() { using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var currentUsername = Csla.ApplicationContext.User.Identity.Name; var results = from studyDataData in ctx.ObjectContext.StudyDataDatas where studyDataData.Username == currentUsername select studyDataData; StudyDataDto dto = null; if (results.Count() == 1) { var studyDataData = results.First(); dto = EfHelper.ToDto(studyDataData); return(dto); } else { if (results.Count() == 0) { throw new Exceptions.StudyDataNotFoundForUserException(currentUsername); } else { throw new Exceptions.VeryBadException(); } } } }
protected override PhraseDto DeleteImpl(Guid id) { var currentUserId = Business.BusinessHelper.GetCurrentUserId(); using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var results = from phraseData in ctx.ObjectContext.PhraseDatas where phraseData.Id == id && phraseData.UserDataId == currentUserId select phraseData; if (results.Count() == 1) { var phraseDataToDelete = results.First(); var retDto = EfHelper.ToDto(phraseDataToDelete); //WHEN WE DELETE PHRASES, IF IT IS PART OF A TRANSLATION, AND THAT TRANSLATION //AFTER DELETE WOULD HAVE LESS THAN TWO PHRASES, WE NEED TO CASCADE THE DELETE TO THE TRANSLATION var translationsToDelete = new List <TranslationData>(); foreach (var translation in phraseDataToDelete.TranslationDatas) { //IF WE ONLY HAVE TWO OR LESS PHRASES *NOW* (WE HAVE NOT DELETED PHRASE YET), THEN AFTER DELETE //WE *WILL* HAVE ONLY ONE OR ZERO, SO MARK THIS TRANSLATION FOR DELETION. if (translation.PhraseDatas.Count <= 2) { translationsToDelete.Add(translation); } } //EXECUTE DELETE OF THOSE TRANSLATIONS foreach (var translation in translationsToDelete) { ctx.ObjectContext.TranslationDatas.DeleteObject(translation); } //DELETE THE PHRASE ITSELF ctx.ObjectContext.PhraseDatas.DeleteObject(phraseDataToDelete); //SAVE CHANGES ctx.ObjectContext.SaveChanges(); return(retDto); } else { if (results.Count() == 0) { throw new Exceptions.IdNotFoundException(id); } else { //results.count is not one or zero. either it's negative, which would be framework absurd, or its more than one, //which means that we have multiple phrases with the same id. this is very bad. var errorMsg = string.Format(DalResources.ErrorMsgVeryBadException, DalResources.ErrorMsgVeryBadExceptionDetail_ResultCountNotOneOrZero); throw new Exceptions.VeryBadException(errorMsg); } } } }
protected override PhraseBeliefDto FetchImpl(Guid id) { var currentUserId = Business.BusinessHelper.GetCurrentUserId(); using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var results = from phraseBeliefData in ctx.ObjectContext.PhraseBeliefDatas where phraseBeliefData.Id == id && phraseBeliefData.UserDataId == currentUserId select phraseBeliefData; if (results.Count() == 1) { var fetchedPhraseBeliefData = results.First(); PhraseBeliefDto phraseBeliefDto = EfHelper.ToDto(fetchedPhraseBeliefData); return(phraseBeliefDto); } else { if (results.Count() == 0) { throw new Exceptions.IdNotFoundException(id); } else { //results.count is not one or zero. either it's negative, which would be framework absurd, or its more than one, //which means that we have multiple phraseBeliefs with the same id. this is very bad. var errorMsg = string.Format(DalResources.ErrorMsgVeryBadException, DalResources.ErrorMsgVeryBadExceptionDetail_ResultCountNotOneOrZero); throw new Exceptions.VeryBadException(errorMsg); } } } }
/// <summary> /// Loads the information from the dto into the data object. Except... /// Does NOT load dto.Id. /// </summary> /// <param name="data"></param> /// <param name="dto"></param> public static void LoadDataFromDto(ref TranslationData data, TranslationDto dto, LearnLanguagesContext context) { //COPY USER INFO data.UserDataId = dto.UserId; data.UserData = EfHelper.GetUserData(dto.UserId, context); var currentPhraseIds = (from phrase in data.PhraseDatas select phrase.Id); //COPY PHRASEID INFO //ADD NEW PHRASEDATAS IN THE DTO foreach (var id in dto.PhraseIds) { if (!currentPhraseIds.Contains(id)) { PhraseData phraseData = EfHelper.GetPhraseData(id, context); data.PhraseDatas.Add(phraseData); } } //REMOVE PHRASEDATAS THAT ARE NOT IN DTO ANYMORE foreach (var phraseId in currentPhraseIds) { if (!dto.PhraseIds.Contains(phraseId)) { var dataToRemove = (from phraseData in data.PhraseDatas where phraseData.Id == phraseId select phraseData).First(); data.PhraseDatas.Remove(dataToRemove); } } }
protected override TranslationDto InsertImpl(TranslationDto dto) { using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var newTranslationData = EfHelper.AddToContext(dto, ctx.ObjectContext); ctx.ObjectContext.SaveChanges(); dto.Id = newTranslationData.Id; return(dto); } }
protected override PhraseBeliefDto InsertImpl(PhraseBeliefDto dto) { using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { PhraseBeliefData newPhraseBeliefData = EfHelper.AddToContext(dto, ctx.ObjectContext); ctx.ObjectContext.SaveChanges(); dto.Id = newPhraseBeliefData.Id; return(dto); } }
protected override MultiLineTextDto InsertImpl(MultiLineTextDto dto) { using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { MultiLineTextData newMultiLineTextData = EfHelper.AddToContext(dto, ctx.ObjectContext); ctx.ObjectContext.SaveChanges(); dto.Id = newMultiLineTextData.Id; return(dto); } }
protected override LanguageDto InsertImpl(LanguageDto dto) { var currentUserId = Business.BusinessHelper.GetCurrentUserId(); using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { //CHECK TO SEE IF LANGUAGE TEXT ALREADY EXISTS. NO DUPLICATE LANGUAGE TEXTS. var languageTextAlreadyExists = (from languageData in ctx.ObjectContext.LanguageDatas where languageData.Text == dto.Text && languageData.UserDataId == currentUserId select languageData).Count() > 0; if (languageTextAlreadyExists) { throw new Exceptions.LanguageTextAlreadyExistsException(dto.Text); } var results = from languageData in ctx.ObjectContext.LanguageDatas where languageData.Id == dto.Id && languageData.UserDataId == currentUserId select languageData; //SHOULD FIND ZERO LANGUAGEDTOS (NO DUPLICATE IDS, NO DUPLICATE DTOS) if (results.Count() == 0) { var data = EfHelper.ToData(dto, ctx.ObjectContext); ctx.ObjectContext.LanguageDatas.AddObject(data); ctx.ObjectContext.SaveChanges(); dto.Id = data.Id; return(dto); } else { if (results.Count() == 1) { //ID ALREADY EXISTS throw new Exceptions.IdAlreadyExistsException(dto.Id); } else { //MULTIPLE IDS ALREADY EXIST?? SHOULD NOT BE POSSIBLE throw new Exceptions.VeryBadException( string.Format(DalResources.ErrorMsgVeryBadException, DalResources.ErrorMsgVeryBadExceptionDetail_ResultCountNotOneOrZero)); } } } }
public static void LoadDataFromDto(ref LineData data, LineDto dto, LearnLanguagesContext context) { //USER INFO data.UserDataId = dto.UserId; data.UserData = EfHelper.GetUserData(dto.UserId, context); //PHRASE INFO data.PhraseDataId = dto.PhraseId; data.PhraseData = EfHelper.GetPhraseData(dto.PhraseId, context); //TEXT data.LineNumber = dto.LineNumber; }
protected override LanguageDto UpdateImpl(LanguageDto dto) { var currentUserId = Business.BusinessHelper.GetCurrentUserId(); using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { //CHECK TO SEE IF *NEW* LANGUAGE TEXT ALREADY EXISTS IN SOME OTHER LANGUAGE EDIT. IF WE //ARE REPLACING THE CURRENT TEXT WITH THE SAME TEXT, THAT IS FINE AS IT WILL JUST BE OVERWRITTEN. var newLanguageTextAlreadyExists = (from languageData in ctx.ObjectContext.LanguageDatas where languageData.Text == dto.Text && languageData.Id != dto.Id && languageData.UserDataId == currentUserId select languageData).Count() > 0; if (newLanguageTextAlreadyExists) { throw new Exceptions.LanguageTextAlreadyExistsException(dto.Text); } //FIND CURRENT LANGUAGEDATA TO UPDATE var results = from languageData in ctx.ObjectContext.LanguageDatas where languageData.Id == dto.Id && languageData.UserDataId == currentUserId select languageData; if (results.Count() == 1) { var languageDataToUpdate = results.First(); EfHelper.LoadDataFromDto(ref languageDataToUpdate, dto, ctx.ObjectContext); ctx.ObjectContext.SaveChanges(); dto.Id = languageDataToUpdate.Id; return(dto); } else { if (results.Count() == 0) { throw new Exceptions.IdNotFoundException(dto.Id); } else { throw new Exceptions.VeryBadException( string.Format(DalResources.ErrorMsgVeryBadException, DalResources.ErrorMsgVeryBadExceptionDetail_ResultCountNotOneOrZero)); } } } }
/// <summary> /// Loads the information from the dto into the data object. Except... /// Does NOT load dto.Id. /// </summary> /// <param name="data"></param> /// <param name="dto"></param> public static void LoadDataFromDto(ref LanguageData data, LanguageDto dto, LearnLanguagesContext context) { //USER INFO data.UserDataId = dto.UserId; data.UserData = EfHelper.GetUserData(dto.UserId, context); //MAKE SURE USERDATA USERNAME MATCHES DTO.USERNAME if (data.UserData.Username != dto.Username) { throw new Exceptions.UsernameAndUserIdDoNotMatchException(dto.Username, dto.UserId); } //TEXT data.Text = dto.Text; }
/// <summary> /// Loads the information from the dto into the data object. Except... /// Does NOT load dto.Id. /// </summary> /// <param name="data"></param> /// <param name="dto"></param> public static void LoadDataFromDto(ref PhraseData data, PhraseDto dto, LearnLanguagesContext context) { //USER INFO data.UserDataId = dto.UserId; data.UserData = EfHelper.GetUserData(dto.UserId, context); //LANGUAGE INFO data.LanguageDataId = dto.LanguageId; data.LanguageData = EfHelper.GetLanguageData(dto.LanguageId, context); //TEXT data.Text = dto.Text; //TRANSLATIONDATAS data.TranslationDatas.Load(); }
public static void LoadDataFromDto(ref PhraseBeliefData data, PhraseBeliefDto dto, LearnLanguagesContext context) { //USER INFO data.UserDataId = dto.UserId; data.UserData = EfHelper.GetUserData(dto.UserId, context); //PHRASE data.PhraseDataId = dto.PhraseId; data.PhraseData = EfHelper.GetPhraseData(dto.PhraseId, context); //SCALAR data.ReviewMethodId = dto.ReviewMethodId; data.Strength = dto.Strength; data.Text = dto.Text; data.TimeStamp = dto.TimeStamp; }
protected override StudyDataDto DeleteImpl(Guid id) { var currentUsername = ((UserIdentity)(Csla.ApplicationContext.User.Identity)).Name; using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var results = from studyDataData in ctx.ObjectContext.StudyDataDatas where studyDataData.Id == id && studyDataData.Username == currentUsername select studyDataData; if (results.Count() == 1) { var studyDataDataToDelete = results.First(); //GET DTO BEFORE DELETE var retDto = EfHelper.ToDto(studyDataDataToDelete); //DELETE THE OBJECT FROM THE CONTEXT ctx.ObjectContext.StudyDataDatas.DeleteObject(studyDataDataToDelete); //SAVE CHANGES ctx.ObjectContext.SaveChanges(); //RETURN THE DTO return(retDto); } else { if (results.Count() == 0) { throw new Exceptions.IdNotFoundException(id); } else { //results.count is not one or zero. either it's negative, which would be framework absurd, or its more than one, //which means that we have multiple studyDatas with the same id. this is very bad. var errorMsg = string.Format(DalResources.ErrorMsgVeryBadException, DalResources.ErrorMsgVeryBadExceptionDetail_ResultCountNotOneOrZero); throw new Exceptions.VeryBadException(errorMsg); } } } }
protected override ICollection <PhraseBeliefDto> GetAllImpl() { using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var allPhraseBeliefDtos = new List <PhraseBeliefDto>(); UserIdentity identity = (UserIdentity)Csla.ApplicationContext.User.Identity; var phraseBeliefDatas = (from phraseBeliefData in ctx.ObjectContext.PhraseBeliefDatas where phraseBeliefData.UserDataId == identity.UserId select phraseBeliefData);//.ToList(); foreach (var usersPhraseBeliefData in phraseBeliefDatas) { allPhraseBeliefDtos.Add(EfHelper.ToDto(usersPhraseBeliefData)); } return(allPhraseBeliefDtos); } }
protected override ICollection <MultiLineTextDto> GetAllImpl() { using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var allMultiLineTextDtos = new MobileList <MultiLineTextDto>(); UserIdentity identity = (UserIdentity)Csla.ApplicationContext.User.Identity; var datas = (from data in ctx.ObjectContext.MultiLineTextDatas where data.UserDataId == identity.UserId select data);//.ToList(); foreach (var usersMultiLineTextData in datas) { allMultiLineTextDtos.Add(EfHelper.ToDto(usersMultiLineTextData)); } return(allMultiLineTextDtos); } }
//private Guid GetDefaultLanguageId() //{ // Guid retDefaultLanguageId; // using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) // { // try // { // retDefaultLanguageId = (from defaultLanguage in ctx.ObjectContext.LanguageDatas // where defaultLanguage.Text == EfResources.DefaultLanguageText // select defaultLanguage).First().Id; // } // catch (Exception ex) // { // throw new Exceptions.GeneralDataAccessException(ex); // } // } // return retDefaultLanguageId; //} protected override ICollection <MultiLineTextDto> FetchByIdImpl(Guid lineId) { var currentUserId = Business.BusinessHelper.GetCurrentUserId(); using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var results = from mltData in ctx.ObjectContext.MultiLineTextDatas where mltData.UserDataId == currentUserId && (from lineData in mltData.LineDatas where lineData.Id == lineId select lineData).Count() > 0 select mltData; List <MultiLineTextDto> dtos = new List <MultiLineTextDto>(); foreach (var data in results) { var dto = EfHelper.ToDto(data); dtos.Add(dto); } return(dtos); //if (results.Count() == 1) //{ // var fetchedMultiLineTextData = results.First(); // MultiLineTextDto multiLineTextDto = EfHelper.ToDto(fetchedMultiLineTextData); // return multiLineTextDto; //} //else //{ // if (results.Count() == 0) // throw new Exceptions.IdNotFoundException(id); // else // { // //results.count is not one or zero. either it's negative, which would be framework absurd, or its more than one, // //which means that we have multiple multiLineTexts with the same id. this is very bad. // var errorMsg = string.Format(DalResources.ErrorMsgVeryBadException, // DalResources.ErrorMsgVeryBadExceptionDetail_ResultCountNotOneOrZero); // throw new Exceptions.VeryBadException(errorMsg); // } //} } }
protected override ICollection <RoleDto> GetRolesImpl(string username) { try { //GET USER var userDto = FetchImpl(username); if (username == null) { throw new Exceptions.UsernameNotFoundException(username); } ICollection <RoleDto> retRoles = new List <RoleDto>(); using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { //GET ROLES FOR THAT USER var results = from roleData in ctx.ObjectContext.RoleDatas where userDto.RoleIds.Contains(roleData.Id) select roleData; foreach (var roleData in results) { var dto = EfHelper.ToDto(roleData); retRoles.Add(dto); } } #if DEBUG if (retRoles == null) { System.Diagnostics.Debugger.Break(); } #endif return(retRoles); } catch (Exception) { #if DEBUG System.Diagnostics.Debugger.Break(); #endif throw; } }
protected override ICollection <StudyDataDto> GetAllImpl() { using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var allStudyDataDtos = new List <StudyDataDto>(); UserIdentity identity = (UserIdentity)Csla.ApplicationContext.User.Identity; var studyDataDatas = (from studyDataData in ctx.ObjectContext.StudyDataDatas where studyDataData.Username == identity.Name select studyDataData).ToList(); foreach (var usersStudyDataData in studyDataDatas) { allStudyDataDtos.Add(EfHelper.ToDto(usersStudyDataData)); } return(allStudyDataDtos); } }
public static void LoadDataFromDto(ref MultiLineTextData data, MultiLineTextDto dto, LearnLanguagesContext context) { //USER INFO data.UserDataId = dto.UserId; data.UserData = EfHelper.GetUserData(dto.UserId, context); //LINE IDS foreach (var id in dto.LineIds) { LineData lineData = EfHelper.GetLineData(id, context); data.LineDatas.Add(lineData); } //SCALAR data.Title = dto.Title; data.AdditionalMetadata = dto.AdditionalMetadata; }
protected override MultiLineTextDto DeleteImpl(Guid id) { var currentUserId = Business.BusinessHelper.GetCurrentUserId(); using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var results = from multiLineTextData in ctx.ObjectContext.MultiLineTextDatas where multiLineTextData.Id == id && multiLineTextData.UserDataId == currentUserId select multiLineTextData; if (results.Count() == 1) { //GET var multiLineTextDataToDelete = results.First(); var retDto = EfHelper.ToDto(multiLineTextDataToDelete); //DELETE ctx.ObjectContext.MultiLineTextDatas.DeleteObject(multiLineTextDataToDelete); //SAVE CHANGES ctx.ObjectContext.SaveChanges(); //RETURN return(retDto); } else { if (results.Count() == 0) { throw new Exceptions.IdNotFoundException(id); } else { //results.count is not one or zero. either it's negative, which would be framework absurd, or its more than one, //which means that we have multiple multiLineTexts with the same id. this is very bad. var errorMsg = string.Format(DalResources.ErrorMsgVeryBadException, DalResources.ErrorMsgVeryBadExceptionDetail_ResultCountNotOneOrZero); throw new Exceptions.VeryBadException(errorMsg); } } } }
protected override ICollection <LineDto> GetAllImpl() { using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var allLineDtos = new List <LineDto>(); UserIdentity identity = (UserIdentity)Csla.ApplicationContext.User.Identity; var lineDatas = (from lineData in ctx.ObjectContext.LineDatas where lineData.UserDataId == identity.UserId select lineData).ToList(); foreach (var usersLineData in lineDatas) { var dto = EfHelper.ToDto(usersLineData); allLineDtos.Add(dto); } return(allLineDtos); } }
protected override ICollection <TranslationDto> GetAllImpl() { var identity = (UserIdentity)Csla.ApplicationContext.User.Identity; using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var allTranslationDtos = new List <TranslationDto>(); var translationDatas = (from translationData in ctx.ObjectContext.TranslationDatas where translationData.UserDataId == identity.UserId select translationData).ToList(); foreach (var translationData in translationDatas) { allTranslationDtos.Add(EfHelper.ToDto(translationData)); } return(allTranslationDtos); } }
protected override ICollection <LanguageDto> GetAllImpl() { var currentUserId = Business.BusinessHelper.GetCurrentUserId(); var allDtos = new List <LanguageDto>(); using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var allLanguageDatas = from languageData in ctx.ObjectContext.LanguageDatas where languageData.UserDataId == currentUserId select languageData; foreach (var languageData in allLanguageDatas) { var dto = EfHelper.ToDto(languageData); allDtos.Add(dto); } } return(allDtos); }
protected override ICollection <LanguageDto> FetchImpl(ICollection <Guid> ids) { var currentUserId = Business.BusinessHelper.GetCurrentUserId(); var retLanguages = new List <LanguageDto>(); using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { foreach (var id in ids) { //old lazy way, lazy as in I'm lazy for doing it this way //var dto = FetchImpl(id); //retLanguages.Add(dto); var data = EfHelper.GetLanguageData(id, ctx.ObjectContext); var dto = EfHelper.ToDto(data); retLanguages.Add(dto); } } return(retLanguages); }
protected override TranslationDto DeleteImpl(Guid id) { var identity = (UserIdentity)Csla.ApplicationContext.User.Identity; string currentUsername = identity.Name; Guid currentUserId = identity.UserId; using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var results = from translationData in ctx.ObjectContext.TranslationDatas where translationData.Id == id && translationData.UserDataId == currentUserId select translationData; if (results.Count() == 1) { var translationDataToDelete = results.First(); var retDto = EfHelper.ToDto(translationDataToDelete); ctx.ObjectContext.TranslationDatas.DeleteObject(translationDataToDelete); ctx.ObjectContext.SaveChanges(); return(retDto); } else { if (results.Count() == 0) { throw new Exceptions.IdNotFoundException(id); } else { //results.count is not one or zero. either it's negative, which would be framework absurd, or its more than one, //which means that we have multiple phrases with the same id. this is very bad. var errorMsg = string.Format(DalResources.ErrorMsgVeryBadException, DalResources.ErrorMsgVeryBadExceptionDetail_ResultCountNotOneOrZero); throw new Exceptions.VeryBadException(errorMsg); } } } }
protected override TranslationDto UpdateImpl(TranslationDto dto) { using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var results = (from translationData in ctx.ObjectContext.TranslationDatas where translationData.Id == dto.Id && translationData.UserDataId == dto.UserId select translationData); if (results.Count() == 1) { //UPDATE THE TRANSLATIONDATA IN THE CONTEXT var translationData = results.First(); EfHelper.LoadDataFromDto(ref translationData, dto, ctx.ObjectContext); //SAVE TO MAKE SURE AFFECTED USERS ARE UPDATED TO REMOVE THIS PHRASE ctx.ObjectContext.SaveChanges(); var updatedDto = EfHelper.ToDto(translationData); return(updatedDto); } else { if (results.Count() == 0) { throw new Exceptions.IdNotFoundException(dto.Id); } else { //results.count is not one or zero. either it's negative, which would be framework absurd, or its more than one, //which means that we have multiple phrases with the same id. this is very bad. var errorMsg = string.Format(DalResources.ErrorMsgVeryBadException, DalResources.ErrorMsgVeryBadExceptionDetail_ResultCountNotOneOrZero); throw new Exceptions.VeryBadException(errorMsg); } } } }