public bool Remove <T>(T model) { try { if (model == null) { throw new ArgumentNullException(ArgumentNullException); } dynamic translator = TranslatorFactory.Create(model.GetType().Name); if (translator == null) { throw new Exception(TranslatorNullException); } var entity = translator.Translate(model); RemoveEntity(entity); return(Save() > 0 ? true : false); } catch (System.Data.UpdateException uex) { if (uex.InnerException.Message.Contains(AlreadyInUse)) { throw new Exception(AlreadyInUseMessage); } throw new System.Data.UpdateException(UpdateException); } catch (Exception) { throw; } }
public void Add <T>(T model) { try { if (model == null) { throw new ArgumentNullException(ArgumentNullException); } dynamic translator = TranslatorFactory.Create(model.GetType().Name); if (translator == null) { throw new Exception(TranslatorNullException); } var entity = translator.Translate(model); var addedEntity = AddEntity(entity); } catch (ArgumentNullException) { throw new ArgumentNullException(ArgumentNullException); } catch (Exception) { throw; } }
public int Add <T>(List <T> models) { try { if (models == null || models.Count <= 0) { throw new ArgumentNullException(ArgumentNullException); } dynamic translator = TranslatorFactory.Create(models[0].GetType().Name); if (translator == null) { throw new Exception(TranslatorNullException); } var entities = translator.Translate(models); return(AddEntities(entities)); } catch (ArgumentNullException) { throw new ArgumentNullException(ArgumentNullException); } catch (Exception) { throw; } }
public async Task <List <T> > GetModelsByAsync <T, E>(Expression <Func <E, bool> > selector = null, Func <IQueryable <E>, IOrderedQueryable <E> > orderBy = null, string includeProperties = "") where E : class { try { if (selector == null) { throw new ArgumentNullException(ArgumentNullException); } List <E> entities = await GetByAsync(selector, orderBy, includeProperties); if (entities == null) { return(default(List <T>)); } string typeName = selector.Parameters[0].Type.Name; dynamic translator = TranslatorFactory.Create(typeName); if (translator == null) { throw new Exception(TranslatorNullException); } return(translator.Translate(entities)); } catch (Exception) { throw; } }
public async Task <List <T> > GetTopByAsync <T, E>(int top, Expression <Func <E, bool> > filter = null, Func <IQueryable <E>, IOrderedQueryable <E> > orderBy = null, string includeProperties = "") where E : class { try { List <E> list = null; List <E> entities = await GetByAsync <E>(filter, orderBy, includeProperties); if (entities != null && entities.Count() > 0) { list = entities.Take(top).ToList(); } string typeName = filter.Parameters[0].Type.Name; dynamic translator = TranslatorFactory.Create(typeName); if (translator == null) { throw new Exception(TranslatorNullException); } return(translator.Translate(list)); } catch (Exception) { throw; } }
public T GetModelBy <T, E>(Func <E, bool> selector = null) where E : class { try { if (selector == null) { throw new ArgumentNullException(ArgumentNullException); } E entity = GetSingleBy(selector); if (entity == null) { return(default(T)); } string typeName = selector.GetType().GenericTypeArguments[0].Name; dynamic translator = TranslatorFactory.Create(typeName); if (translator == null) { throw new Exception(TranslatorNullException); } return(translator.Translate(entity)); } catch (Exception) { throw; } }
public List <T> GetAll <T, E>() where E : class { try { var entities = _context.Set <E>().ToList(); if (entities == null || entities.Count <= 0) { return(default(List <T>)); } Type type = entities[0].GetType().BaseType; if (type.Equals(typeof(object))) { type = entities[0].GetType(); } dynamic translator = TranslatorFactory.Create(type.Name); if (translator == null) { throw new Exception(TranslatorNullException); } return(translator.Translate(entities)); } catch (Exception) { throw; } }
//public T GetModelBy<T, E>(Func<E, bool> selector = null) where E : class //{ // try // { // if (selector == null) // { // throw new ArgumentNullException(ArgumentNullException); // } // E entity = GetSingleBy(selector); // if (entity == null) // { // return default(T); // } // string typeName = selector.GetType().GenericTypeArguments[0].Name; // dynamic translator = TranslatorFactory.Create(typeName); // if (translator == null) // { // throw new Exception(TranslatorNullException); // } // return translator.Translate(entity); // } // catch (Exception) // { // throw; // } //} public async Task <T> GetModelByAsync <T, E>(Expression <Func <E, bool> > selector = null) where E : class { try { if (selector == null) { throw new ArgumentNullException(ArgumentNullException); } E entity = await GetSingleByAsync(selector); if (entity == null) { return(default(T)); } //string typeName = selector.GetType().GenericTypeArguments[0].Name; string typeName = selector.Parameters[0].Type.Name; dynamic translator = TranslatorFactory.Create(typeName); if (translator == null) { throw new Exception(TranslatorNullException); } return(translator.Translate(entity)); } catch (Exception) { throw; } }
private Teacher Translate(TEACHER entity) { try { if (entity == null || entity.Person_Id <= 0) { return(null); } Type type = entity.GetType().BaseType; if (type.Equals(typeof(object))) { type = entity.GetType(); } dynamic translator = TranslatorFactory.Create(type.Name); if (translator == null) { throw new Exception("translator"); } return(translator.Translate(entity)); } catch (Exception) { throw; } }
private List <Teacher> Translate(List <TEACHER> entities) { try { if (entities == null || entities.Count <= 0) { return(null); } Type type = entities[0].GetType().BaseType; if (type.Equals(typeof(object))) { type = entities[0].GetType(); } dynamic translator = TranslatorFactory.Create(type.Name); if (translator == null) { throw new Exception("translator"); } return(translator.Translate(entities)); } catch (Exception) { throw; } }
public virtual T Create <T>(T model) { try { if (model == null) { throw new ArgumentNullException(ArgumentNullException); } dynamic translator = TranslatorFactory.Create(model.GetType().Name); if (translator == null) { throw new Exception(TranslatorNullException); } var entity = translator.Translate(model); var addedEntity = AddEntity(entity); Save(); return(translator.Translate(addedEntity)); } catch (ArgumentNullException) { throw new ArgumentNullException(ArgumentNullException); } catch (System.Data.UpdateException uex) { if (uex.InnerException.Message.Contains(DuplicateKeyDetected)) { throw new Exception(DuplicateKeyDetectedMessage); } throw; } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { System.Diagnostics.Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } throw; } catch (Exception) { throw; } }
public bool Modify <T>(T model) { try { if (model == null) { throw new ArgumentNullException(ArgumentNullException); } dynamic translator = TranslatorFactory.Create(model.GetType().Name); if (translator == null) { throw new Exception(TranslatorNullException); } var entity = translator.Translate(model); if (entity == null) { throw new Exception(NoItemFound); } return(Update(entity)); } catch (NullReferenceException) { throw new NullReferenceException(ArgumentNullException); } catch (System.Data.UpdateException) { throw new System.Data.UpdateException(UpdateException); } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { System.Diagnostics.Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } throw; } catch (Exception) { throw; } }
public int Create <T>(List <T> models) { try { if (models == null) { throw new ArgumentNullException(ArgumentNullException); } dynamic translator = TranslatorFactory.Create(models[0].GetType().Name); if (translator == null) { throw new Exception(TranslatorNullException); } var entities = translator.Translate(models); AddEntities(entities); return(Save()); } catch (ArgumentNullException) { throw new ArgumentNullException(ArgumentNullException); } catch (System.Data.UpdateException uex) { if (uex.InnerException.Message.Contains(DuplicateKeyDetected)) { throw new Exception(DuplicateKeyDetectedMessage); } throw; } catch (Exception) { throw; } }