简体   繁体   中英

Generic Class to initiate few of my audit properties

I have to map few audit properties every now and then in m methods so , I have written the below generic method.

private static T MapAuditFields<T>(long id , bool isNew ) where T : new()
{
    dynamic result = new T();
    if (isNew)
    {
        result.xxxxxx = DateTimeHelper.GetCurrentUTCDateTime();
        result.xxxxxx = DateTimeHelper.GetCurrentUTCDateTime();
        result.xx= id;
        result.xx= id;
    }
    else
    {
        result.xxxxx= id;
        result.xxxxxx= DateTimeHelper.GetCurrentUTCDateTime();
    }
    return (T)result;
}

But every instance is giving me a new object . So , while mapping using my auto Mapper for efcore , I was doing the mapping two times .

Can some one help me avoiding mapping here .

Ex :

public bool Save(Request request)
{
   var ob = MapAuditFields<Request>(3 , true); // Getting the audit fields // Creates a new request object
  _mapper.Map<Entity>(request);  // Mapping request body with entity 

}

Thanks in advance for inputs . Sorry if I am not clear

MapAuditFields method can be implemented with Reflection. Please also keep in mind that the id and xxxxxx property is needed to be exist in your object type T .

private static T MapAuditFields<T>(T requestObj, long id , bool isNew )
{

    if (isNew)
    {
        typeof(T).GetProperty("id").SetValue(request, DateTimeHelper.GetCurrentUTCDateTime());
        typeof(T).GetProperty("xxxxx").SetValue(request, DateTimeHelper.GetCurrentUTCDateTime());

        ...
    }
    else
    {
        typeof(T).GetProperty("id").SetValue(request, DateTimeHelper.GetCurrentUTCDateTime());
        typeof(T).GetProperty("xxxxxx").SetValue(request, DateTimeHelper.GetCurrentUTCDateTime());
    }

    return (T)result;
}

I suggest you to have Base object. In that case,

private static T MapAuditFields<T>(T requestObj, long id , bool isNew ) where T : MyBaseObject
{

    if (isNew)
    {
        requestObj.id = id;
        requestObj.xxxxx = DateTimeHelper.GetCurrentUTCDateTime()

......

you can also use Automapper for mapping

public class SetTimestampMappingAction: IMappingAction<object, MyBaseObject>
{
    public SetTraceIdentifierAction(/*if you are using dependency injection use it here*/)
    {

    }

    public void Process(SomeModel source, SomeOtherModel destination, ResolutionContext context)
    {
        destination.xxxxxx = DateTimeHelper.GetCurrentUTCDateTime()

    }
}

public class SomeProfile : Profile
{
    public SomeProfile()
    {
        CreateMap<object, MyBaseObject>()
            .AfterMap<SetTimestampMappingAction>();
    }
}

Passing the object which i want to map audit fields has been passed as an argument to avoid new instance has resolved my issue . This will return you the same object with the values populated .

private static T MapAuditFields<T>(long id , bool isNew, T t ) where T : new()
{
    dynamic result = t;
    if (isNew)
    {
        result.xxxxxx = DateTimeHelper.GetCurrentUTCDateTime();
        result.xxxxxx = DateTimeHelper.GetCurrentUTCDateTime();
        result.xx= id;
        result.xx= id;
    }
    else
    {
        result.xxxxx= id;
        result.xxxxxx= DateTimeHelper.GetCurrentUTCDateTime();
    }
    return (T)result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM