Base solution for your next web application
Open Closed

Setting generic datetime and username for objectmapper.map #8248


User avatar
0
sumitshah created

We are using Angular and .net core, aspnetzero framework.

For all the CRUD operations Insert/ Update we are using ObjectMapper to map the entity with the dto All the insert/ update dto's are exact replica of their respective entities(which are created using the RAD tool based on their SQL tables)

Now, there are 4 common columns in all the SQL tables and also the entities and we have created the same in the dto's as well. The 4 columns are:

  • cCreatedBy
  • dCreatedDate
  • cModifiedBy
  • dModifiedDate

We need to update the first 2 columns during a create operation and the remaining 2 when we update a record. There are 2 different ways we have tried to implement this:

  1. Set the date and user in the backend during save public async Task CreateOrEdit(CreateOrEditOwnerDto input) { try { ValidateOwners(input); if (input.Id == null) { var owner = ObjectMapper.Map<Owner>(input); owner.cCreatedBy = _mySession.IDMSUserName; owner.dCreatedDate = DateTime.Now; await _customOwnerRepository.InsertAsync(owner); } else { var updateOwner = _customOwnerRepository.Get(input.Id.GetValueOrDefault()); input.cModifiedBy = _mySession.IDMSUserName; input.dModifiedDate = DateTime.Now; ObjectMapper.Map(input, updateOwner); CurrentUnitOfWork.SaveChanges(); } } catch(Exception ex) { throw new UserFriendlyException(ex.Message); } }
  2. By passing the user and date from the Angular app in the dto.

We have explored the UserAppService code where we are inheriting AbpUser class and this internally inherits multiple interface which are setting the user and datetime. But all of this is in aspnetzero core dll and we are unable to use the same.

Could you please let us know the right way to implement this without writing redundant code.


4 Answer(s)
  • User Avatar
    0
    musa.demir created

    Zero handles it automatically. What you need is add relevant interface/interfaces to your entity or inherit relevant class.

    For example

    public class Owner : FullAuditedEntity // with class
    {
    ...
    }
    

    or

    public class Owner : IModificationAudited  // with interface
    {
        public long? LastModifierUserId { get; set; }
        public DateTime? LastModificationTime { get; set; }
    }
    

    then use

    var owner = ObjectMapper.Map(input);
    await _customOwnerRepository.InsertAsync(owner);
    
  • User Avatar
    0
    sumitshah created

    Thank you for the quick response.

    We are not using abp tables, we are using our applications legacy tables, so the columns names are not the same, altough am sure we can set data annotations in our dto's to match the names.

    Abp column names: CreationTime - datetime CreatorUserId - bigint LastModificationTime - datetime? LastModifierUserId - bigint

    Legacy tables: dCreatedDate - datetime cCreatedBy - varchar(25) dModifiedDate - datetime cModifiedBy - varchar(25)

    Also, the data types are differeing and the inbuild interfaces are having CreatorUserID but in our case we need to store the UserName in the legacy tables so the inbuild interfaces would not suffice to our requirement.

    Could you please suggest a different approach.

  • User Avatar
    0
    musa.demir created

    You can create a custom mapper. From your dto to entity which fills the values you want.

    https://aspnetboilerplate.com/Pages/Documents/Object-To-Object-Mapping#custom-mapping

  • User Avatar
    0
    ismcagdas created
    Support Team

    This issue is closed because of no recent activity. Please open a new issue if you are still having this problem.