/// <summary> /// Deletes the proveded conversion method, but does not touch the source and target units /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool DeleteConversionMethod(ConversionMethod entity) { Contract.Requires(entity != null, "provided unit can not be null"); Contract.Requires(entity.Id >= 0, "Id can not be empty"); using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository<ConversionMethod> repoCM = uow.GetRepository<ConversionMethod>(); entity = repoCM.Reload(entity); // remove all associations entity.Source = null; entity.Target = null; //delete the entity repoCM.Delete(entity); // commit changes uow.Commit(); } // if any problem was detected during the commit, an exception will be thrown! return (true); }
public ConversionMethod UpdateConversionMethod(ConversionMethod entity) { Contract.Requires(entity != null, "provided entity can not be null"); Contract.Requires(entity.Id >= 0, "provided entity must have a permant ID"); Contract.Ensures(Contract.Result<ConversionMethod>() != null && Contract.Result<ConversionMethod>().Id >= 0, "No entity is persisted!"); using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository<ConversionMethod> repoCM = uow.GetRepository<ConversionMethod>(); repoCM.Put(entity); // Merge is required here!!!! uow.Commit(); } return (entity); }
/// <summary> /// if there is no conversion method between source and target units, creates one otherwise fails /// </summary> /// <param name="description"></param> /// <param name="formula"></param> /// <param name="source"></param> /// <param name="target"></param> /// <returns></returns> public ConversionMethod CreateConversionMethod(string formula, string description, Unit source, Unit target) { Contract.Requires(!string.IsNullOrWhiteSpace(formula), "formula can not be empty"); Contract.Requires(formula.Contains("s"), "Formula must use \'s\' as the representative for the source unit"); Contract.Requires(source != null, "source unit can not be null"); Contract.Requires(source.Id >= 0, "source unit must be persisted before this call"); Contract.Requires(target != null, "target unit can not be null"); Contract.Requires(target.Id >= 0, "target unit must be persisted before this call"); Contract.Ensures(Contract.Result<ConversionMethod>() != null && Contract.Result<ConversionMethod>().Id >= 0, "No Conversion Method persisted!"); ConversionMethod cm = new ConversionMethod() { Formula = formula, Description = description, Source = source, Target = target, }; source.ConversionsIamTheSource.Add(cm); target.ConversionsIamTheTarget.Add(cm); using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository<ConversionMethod> repoCM = uow.GetRepository<ConversionMethod>(); ConversionMethod temp = repoCM.Get(c => c.Source.Id == source.Id && c.Target.Id == target.Id).FirstOrDefault(); // change it to Count instead of Get if (temp != null) throw new Exception(string.Format("There is already a conversion method between {0} and {1} having [{2}] formula", cm.Source.Name, cm.Target.Name, cm.Formula)); repoCM.Put(cm); uow.Commit(); } return (cm); }