public void CopyTestSimilar() { var copier = new ObjectMapper<A, B>(); A a1 = new A(); B b1 = copier.Copy(a1); Assert.IsNotNull(b1); Assert.AreEqual(a1.PropA, b1.PropA); Assert.AreEqual(B.PropBValue, b1.GetB()); Assert.AreEqual(B.PropDValue, b1.GetD()); Assert.AreEqual((int)a1.PropC, (int)b1.PropC); B b2 = new B(); A a2 = copier.Copy(b2); Assert.IsNotNull(a2); Assert.AreEqual(b2.PropA, a2.PropA); Assert.AreEqual(A.PropBValue, a2.GetB()); Assert.AreEqual(A.PropDValue, a2.GetD()); Assert.AreEqual((int)b2.PropC, (int)a2.PropC); A a3 = new A {PropC = null}; B b3 = copier.Copy(a3); Assert.IsFalse(b3.PropC.HasValue); }
public void CopyTestSimilar() { var copier = new ObjectMapper <A, B>(); A a1 = new A(); B b1 = copier.Copy(a1); Assert.IsNotNull(b1); Assert.AreEqual(a1.PropA, b1.PropA); Assert.AreEqual(B.PropBValue, b1.GetB()); Assert.AreEqual(B.PropDValue, b1.GetD()); Assert.AreEqual((int)a1.PropC, (int)b1.PropC); B b2 = new B(); A a2 = copier.Copy(b2); Assert.IsNotNull(a2); Assert.AreEqual(b2.PropA, a2.PropA); Assert.AreEqual(A.PropBValue, a2.GetB()); Assert.AreEqual(A.PropDValue, a2.GetD()); Assert.AreEqual((int)b2.PropC, (int)a2.PropC); A a3 = new A { PropC = null }; B b3 = copier.Copy(a3); Assert.IsFalse(b3.PropC.HasValue); }
//////////////////////////////////////////////////////////////// /// <summary> /// Creates new module. /// </summary> public DTO.Module CreateModule(DTO.Module moduleDTO) { EDM.Module moduleEDM = s_moduleCopier.Copy(moduleDTO); using (DbManager db = new DbManager()) { db.SetCommand( @"INSERT INTO Modules (app_id, module_title) VALUES (@app_id, @module_title) SELECT Cast(SCOPE_IDENTITY() as int) module_id", db.CreateParameters(moduleEDM, new[] { "module_id" }, null, null)). ExecuteObject(moduleEDM); } return(s_moduleCopier.Copy(moduleEDM)); }
//////////////////////////////////////////////////////////////// /// <summary> /// Creates new sub module. /// </summary> public DTO.SubModule CreateSubModule(DTO.SubModule subModuleDTO) { EDM.SubModule subModuleEDM = s_subModuleCopier.Copy(subModuleDTO); using (DbManager db = new DbManager()) { db.SetCommand( @"INSERT INTO Submodules (module_id, submodule_title) VALUES (@module_id, @submodule_title) SELECT Cast(SCOPE_IDENTITY() as int) submodule_id", db.CreateParameters(subModuleEDM, new[] { "submodule_id" }, null, null)). ExecuteObject(subModuleEDM); } return(s_subModuleCopier.Copy(subModuleEDM)); }
/// <summary> /// Iterates over the keys of the source and tries to write a compatible value to a public, /// instance, writable property in the destination. /// </summary> /// <param name="source">The source.</param> /// <param name="target">The target.</param> /// <param name="ignoreKeys">The ignore keys.</param> /// <returns>Number of properties that was copied successful.</returns> public static int CopyKeyValuePairTo( this IDictionary <string, object> source, object?target, params string[] ignoreKeys) => source == null ? throw new ArgumentNullException(nameof(source)) : ObjectMapper.Copy(source, target, null, ignoreKeys);
//////////////////////////////////////////////////////////////// /// <summary> /// Creates new person. /// </summary> public DTO.Person CreatePerson(DTO.Person personDTO) { EDM.Person personEDM = s_personCopier.Copy(personDTO); using (DbManager db = new DbManager()) { db.SetCommand( @"INSERT INTO Staff (person_login, person_title) VALUES (@person_login, @person_title) SELECT Cast(SCOPE_IDENTITY() as int) person_id", db.CreateParameters(personEDM, new[] { "person_id" }, null, null)). ExecuteObject(personEDM); } return(s_personCopier.Copy(personEDM)); }
//////////////////////////////////////////////////////////////// /// <summary> /// Creates new revision. /// </summary> public Revision CreateRevision(Revision revisionDTO) { // Map DTO to EDM EDM.Revision relEDM = s_revisionCopier.Copy(revisionDTO); // Insert application using (DbManager db = new DbManager()) { db.SetSpCommand("Revision_Create", db.CreateParameters(relEDM)). ExecuteObject(relEDM); } // Map EDM to DTO revisionDTO = s_revisionCopier.Copy(relEDM); return(revisionDTO); }
public void CopyTestDifferent() { var copier = new ObjectMapper <C, D>(); C c1 = new C(); D d1 = copier.Copy(c1); Assert.IsNotNull(d1); Assert.AreEqual(c1.PropA, d1.PropA); Assert.AreEqual(c1.PropB, d1.PropB); }
public void CopyTestDifferent() { var copier = new ObjectMapper<C, D>(); C c1 = new C(); D d1 = copier.Copy(c1); Assert.IsNotNull(d1); Assert.AreEqual(c1.PropA, d1.PropA); Assert.AreEqual(c1.PropB, d1.PropB); }
//////////////////////////////////////////////////////////////// /// <summary> /// Creates new release. /// </summary> public DTO.Release CreateRelease(DTO.Release relDTO) { // Map DTO to EDM EDM.Release relEDM = s_relCopier.Copy(relDTO); // Insert application using (DbManager db = new DbManager()) { db.SetCommand( @" INSERT INTO Releases (release_title, app_id) VALUES (@release_title, @app_id) SELECT Cast(SCOPE_IDENTITY() as int) release_id", db.CreateParameters(relEDM, new[] { "release_id" }, null, null)). ExecuteObject(relEDM); } // Map EDM to DTO relDTO = s_relCopier.Copy(relEDM); return(relDTO); }
//////////////////////////////////////////////////////////////// /// <summary> /// Creates new application. /// </summary> public DTO.Application CreateApplicaton(DTO.Application appDTO) { // Map DTO to EDM EDM.Application appEDM = s_appCopier.Copy(appDTO); // Insert application using (DbManager db = new DbManager()) { db.SetCommand( @" INSERT INTO Applications (app_title) VALUES (@app_title) SELECT Cast(SCOPE_IDENTITY() as int) app_id", db.CreateParameters(appEDM, new[] { "app_id" }, null, null)). ExecuteObject(appEDM); } // Map EDM to DTO appDTO = s_appCopier.Copy(appEDM); return(appDTO); }
public void SourceAndTargetNotNull_ReturnsCopy() { var target = new UserDto(); var propertiesToCopy = new[] { "Name", "Email" }; var ignoreProperties = new[] { "Role" }; ObjectMapper.Copy(SourceDict, target, propertiesToCopy, ignoreProperties); Assert.AreEqual(SourceDict["Name"].ToString(), target.Name); Assert.AreEqual(SourceDict["Email"].ToString(), target.Email); }
/// <summary> /// Copies the only properties to new instance of T. /// </summary> /// <typeparam name="T">Object Type.</typeparam> /// <param name="source">The source.</param> /// <param name="propertiesToCopy">The properties to copy.</param> /// <returns> /// The specified type with properties copied. /// </returns> /// <exception cref="ArgumentNullException">source.</exception> public static T CopyOnlyPropertiesToNew <T>(this object source, params string[] propertiesToCopy) where T : class { if (source == null) { throw new ArgumentNullException(nameof(source)); } var target = Activator.CreateInstance <T>(); ObjectMapper.Copy(source, target, propertiesToCopy); return(target); }
/// <summary> /// Copies the properties to new instance of T. /// </summary> /// <typeparam name="T">The new object type.</typeparam> /// <param name="source">The source.</param> /// <param name="ignoreProperties">The ignore properties.</param> /// <returns> /// The specified type with properties copied. /// </returns> /// <exception cref="ArgumentNullException">source.</exception> public static T CopyPropertiesToNew <T>(this object source, string[]?ignoreProperties = null) where T : class { if (source == null) { throw new ArgumentNullException(nameof(source)); } var target = Activator.CreateInstance <T>(); ObjectMapper.Copy(source, target, GetCopyableProperties(target), ignoreProperties); return(target); }
//////////////////////////////////////////////////////////////// /// <summary> /// Returns all bugs. /// </summary> public DTO.Bug[] GetBugs() { List <EDM.Bug> bugEDMs; using (DbManager db = new DbManager()) { bugEDMs = db.SetCommand(@"SELECT * FROM Bugs ORDER BY bug_number"). ExecuteList <EDM.Bug>(); } List <DTO.Bug> bugDTOs = new List <DTO.Bug>(bugEDMs.Count); // Copy EDMs to DTOs bugEDMs.ForEach(bugEDM => bugDTOs.Add(s_bugCopier.Copy(bugEDM))); return(bugDTOs.ToArray()); }
/// <summary> /// Iterates over the public, instance, readable properties of the source and /// tries to write a compatible value to a public, instance, writable property in the destination. /// </summary> /// <param name="source">The source.</param> /// <param name="target">The destination.</param> /// <param name="propertiesToCopy">Properties to copy.</param> /// <returns> /// Number of properties that were successfully copied. /// </returns> public static int CopyOnlyPropertiesTo(this object source, object target, params string[]?propertiesToCopy) => ObjectMapper.Copy(source, target, propertiesToCopy);
/// <summary> /// Iterates over the public, instance, readable properties of the source and /// tries to write a compatible value to a public, instance, writable property in the destination. /// </summary> /// <param name="source">The source.</param> /// <param name="target">The destination.</param> /// <param name="propertiesToCopy">Properties to copy.</param> /// <returns> /// Number of properties that were successfully copied. /// </returns> public static int CopyOnlyPropertiesTo(this object source, object target, string[] propertiesToCopy) { return(ObjectMapper.Copy(source, target, propertiesToCopy)); }
/// <summary> /// Iterates over the public, instance, readable properties of the source and /// tries to write a compatible value to a public, instance, writable property in the destination. /// </summary> /// <param name="source">The source.</param> /// <param name="target">The destination.</param> /// <param name="ignoreProperties">The ignore properties.</param> /// <returns> /// Number of properties that were successfully copied. /// </returns> public static int CopyPropertiesTo(this object source, object target, params string[] ignoreProperties) => ObjectMapper.Copy(source, target, null, ignoreProperties);
public void SourceNull_ThrowsArgumentNullException() { Assert.Throws <ArgumentNullException>(() => ObjectMapper.Copy(NullObj, new UserDto())); }
/// <summary> /// Iterates over the public, instance, readable properties of the source and /// tries to write a compatible value to a public, instance, writable property in the destination. /// </summary> /// <param name="source">The source.</param> /// <param name="target">The destination.</param> /// <param name="ignoreProperties">The ignore properties.</param> /// <returns> /// Number of properties that were successfully copied. /// </returns> public static int CopyPropertiesTo(this object source, object target, string[] ignoreProperties = null) { return(ObjectMapper.Copy(source, target, null, ignoreProperties)); }
public void TargetNull_ThrowsArgumentNullException() { Assert.Throws <ArgumentNullException>(() => ObjectMapper.Copy(new UserDto(), null)); }
public void TargetDictionaryNull_ThrowsArgumentNullException() { Assert.Throws <ArgumentNullException>(() => ObjectMapper.Copy(SourceDict, null)); }
public void SourceDictionaryNull_ThrowsArgumentNullException() { var target = new UserDto(); Assert.Throws <ArgumentNullException>(() => ObjectMapper.Copy(null, target)); }
/// <summary> /// Iterates over the public, instance, readable properties of the source and /// tries to write a compatible value to a public, instance, writable property in the destination. /// </summary> /// <typeparam name="T">The type of the source.</typeparam> /// <param name="source">The source.</param> /// <param name="target">The target.</param> /// <param name="ignoreProperties">The ignore properties.</param> /// <returns> /// Number of properties that was copied successful. /// </returns> public static int CopyPropertiesTo <T>(this T source, object target, params string[]?ignoreProperties) where T : class => ObjectMapper.Copy(source, target, GetCopyableProperties(target), ignoreProperties);
/// <summary> /// Iterates over the keys of the source and tries to write a compatible value to a public, /// instance, writable property in the destination. /// </summary> /// <param name="source">The source.</param> /// <param name="target">The target.</param> /// <param name="ignoreKeys">The ignore keys.</param> /// <returns>Number of properties that was copied successful.</returns> public static int CopyKeyValuePairTo(this IDictionary <string, object> source, object target, string[] ignoreKeys = null) { return(ObjectMapper.Copy(source, target, null, ignoreKeys)); }