Skip to content

Commit

Permalink
it was refer to reza for to resolved it
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa fbh committed May 20, 2024
1 parent 501b9ee commit 9d932b4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
22 changes: 21 additions & 1 deletion Source/BSN.Commons.AutoMapper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppServiceViewMapperProfile>());
- services.AddAutoMapper(config => config.AddProfile<AppServiceViewMapperProfile>());

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.
46 changes: 44 additions & 2 deletions Test/BSN.Commons.AutoMapper.Tests/CommonMapperProfileTests .cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<CustomViewModel>(customEntity);

// Assert
Assert.IsNotNull(result);
Assert.AreEqual(customEntity.Id, result.Id);
Assert.AreEqual(customEntity.Name, result.Name);
}

public class CustomMapperProfile : Profile
{
public CustomMapperProfile()
{
CreateMap<CustomEntity, CustomViewModel>();
}
}

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; }
}
}
}

0 comments on commit 9d932b4

Please sign in to comment.