From 9d932b4be76e6208bc49eac34d46ec6f648ecf4f Mon Sep 17 00:00:00 2001 From: mostafa fbh Date: Mon, 20 May 2024 09:23:56 +0330 Subject: [PATCH] it was refer to reza for to resolved it --- Source/BSN.Commons.AutoMapper/README.md | 22 ++++++++- .../CommonMapperProfileTests .cs | 46 ++++++++++++++++++- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/Source/BSN.Commons.AutoMapper/README.md b/Source/BSN.Commons.AutoMapper/README.md index d1a8a80..9b4490e 100644 --- a/Source/BSN.Commons.AutoMapper/README.md +++ b/Source/BSN.Commons.AutoMapper/README.md @@ -10,4 +10,24 @@ To use this package, you need to first install it in your client service. You ca - Install-Package BSN.Commons.AutoMapper Once you've installed the package, you need to add the following code to your Startup.cs file: -- services.AddAutoMapper(config => config.AddProfile()); \ No newline at end of file +- services.AddAutoMapper(config => config.AddProfile()); + +1.AppServiceViewMapperProfile: This profile maps objects related to App Services, including: +- AppService to AppServiceViewModel +- AppServiceDto to AppServiceViewModel +- AppServiceViewModel to AppService +- AppServiceViewModel to AppServiceDto + +2.UserViewMapperProfile: This profile maps objects related to Users, including: +- User to UserViewModel +- UserDto to UserViewModel +- UserViewModel to User +- UserViewModel to UserDto + +3.RoleViewMapperProfile: This profile maps objects related to Roles, including: +- Role to RoleViewModel +- RoleDto to RoleViewModel +- RoleViewModel to Role +- RoleViewModel to RoleDto + +These profiles cover some of the most common use cases for mapping objects in enterprise applications. However, if you need to add support for additional complex types, you can create your own profile and add it to the configuration in the Startup.cs file. \ No newline at end of file diff --git a/Test/BSN.Commons.AutoMapper.Tests/CommonMapperProfileTests .cs b/Test/BSN.Commons.AutoMapper.Tests/CommonMapperProfileTests .cs index 5d6369b..0d43e81 100644 --- a/Test/BSN.Commons.AutoMapper.Tests/CommonMapperProfileTests .cs +++ b/Test/BSN.Commons.AutoMapper.Tests/CommonMapperProfileTests .cs @@ -1,8 +1,7 @@ using AutoMapper; -using BSN.Commons.AutoMapper; using BSN.Commons.Responses; -namespace BSN.Commons.Tests +namespace BSN.Commons.AutoMapper.Tests { [TestFixture] public class CommonMapperProfileTests @@ -47,5 +46,48 @@ public void GenericIEnumerableToCollectionViewModelConverter_ConvertsCorrectly() Assert.IsNotNull(result); Assert.AreEqual(items.Count, result.Items.Count()); } + + [Test] + public void CustomProfileConverter_ConvertsCorrectly() + { + // Arrange + var customProfile = new CustomMapperProfile(); + var profile = new CommonMapperProfile(); + var configuration = new MapperConfiguration(cfg => + { + cfg.AddProfile(profile); + cfg.AddProfile(customProfile); + }); + var mapper = new Mapper(configuration); + var customEntity = new CustomEntity { Id = 1, Name = "Custom Entity" }; + + // Act + var result = mapper.Map(customEntity); + + // Assert + Assert.IsNotNull(result); + Assert.AreEqual(customEntity.Id, result.Id); + Assert.AreEqual(customEntity.Name, result.Name); + } + + public class CustomMapperProfile : Profile + { + public CustomMapperProfile() + { + CreateMap(); + } + } + + public class CustomEntity + { + public int Id { get; set; } + public string Name { get; set; } + } + + public class CustomViewModel + { + public int Id { get; set; } + public string Name { get; set; } + } } } \ No newline at end of file