public static Client MapClient(XpoClient xpoClient) { return(new Client { id = xpoClient.Oid, Name = xpoClient.Name, PreNom = xpoClient.PreName, Address = xpoClient.Address }); }
public static XpoClient MapClient(Client client, UnitOfWork uow) { XpoClient clientReturned; if (client.newObject) { clientReturned = new XpoClient(uow) { Oid = client.id }; } else { clientReturned = uow.GetObjectByKey <XpoClient>(client.id); } clientReturned.Name = client.Name; clientReturned.PreName = client.PreNom; return(clientReturned); }
/// <summary> /// Maps an entity to a model. /// </summary> /// <param name="entity">The entity.</param> /// <returns></returns> public static Client ToModel(this XpoClient entity) => Mapper.Map <Client>(entity);
public static void Tests() => Describe(nameof(ClientMappers), () => { It("Automapper Configuration is valid", () => ClientMappers.Mapper.ConfigurationProvider.AssertConfigurationIsValid <ClientMapperProfile>()); It("Can map", () => { using var session = new Session(); var model = new Client(); var mappedEntity = model.ToEntity(session); var mappedModel = mappedEntity.ToModel(); mappedModel.Should().NotBeNull(); mappedEntity.Should().NotBeNull(); }); It("Properties map", () => { using var session = new Session(); var model = new Client() { Properties = { { "foo1", "bar1" }, { "foo2", "bar2" }, } }; var mappedEntity = model.ToEntity(session); mappedEntity.Properties.Count.Should().Be(2); var foo1 = mappedEntity.Properties.FirstOrDefault(x => x.Key == "foo1"); foo1.Should().NotBeNull(); foo1.Value.Should().Be("bar1"); var foo2 = mappedEntity.Properties.FirstOrDefault(x => x.Key == "foo2"); foo2.Should().NotBeNull(); foo2.Value.Should().Be("bar2"); var mappedModel = mappedEntity.ToModel(); mappedModel.Properties.Count.Should().Be(2); mappedModel.Properties.ContainsKey("foo1").Should().BeTrue(); mappedModel.Properties.ContainsKey("foo2").Should().BeTrue(); mappedModel.Properties["foo1"].Should().Be("bar1"); mappedModel.Properties["foo2"].Should().Be("bar2"); }); It("duplicate properties in db map", () => { using var session = new Session(); var entity = new XpoClient(session) { Properties = { new XpoClientProperty(session) { Key = "foo1", Value = "bar1" }, new XpoClientProperty(session) { Key = "foo1", Value = "bar2" }, } }; Action modelAction = () => entity.ToModel(); modelAction.Should().Throw <Exception>(); }); It("missing values should use defaults", () => { using var session = new Session(); var entity = new XpoClient(session) { ClientSecrets = { new XpoClientSecret(session) } }; var def = new Client { ClientSecrets = { new Secret("foo") } }; var model = entity.ToModel(); model.ProtocolType.Should().Be(def.ProtocolType); model.ClientSecrets.First().Type.Should().Be(def.ClientSecrets.First().Type); }); });