简体   繁体   中英

challenges with .net generics and AutoMapper

I have an interface below

public interface IEntity<T>
{
     T Id { get; set; }
    DateTime Created { get; set; }
    DateTime Updated { get; set; }
    [Timestamp] byte[] RowVersion { get; set; }

}

This interface was implemented by a class

  public partial class UrlRec: IEntity<Int64>
{
    public int EntityId { get; set; } 
    public string EntityName { get; set; }
    public string Slug { get; set; }
    public bool IsActive { get; set; }
    public int LanguageId { get; set; }
    public long Id { get; set; }
    public DateTime Created { get; set; }
    public DateTime Updated { get; set; }
   [Timestamp] public byte[] RowVersion { get; set; }
}

I created a ViewModel for the class UrlRecModel. How do I write a generic extension method to the ViewModel to the class and vice versal using AutoMapper.

here is my viewmodel

 public partial class UrlRecordModel : BaseIbileHubEntityModel
{
    #region Properties

    [IbileHubResourceDisplayName("Admin.System.SeNames.Name")]
    public string Name { get; set; }

    [IbileHubResourceDisplayName("Admin.System.SeNames.EntityId")]
    public int EntityId { get; set; }

    [IbileHubResourceDisplayName("Admin.System.SeNames.EntityName")]
    public string EntityName { get; set; }

    [IbileHubResourceDisplayName("Admin.System.SeNames.IsActive")]
    public bool IsActive { get; set; }

    [IbileHubResourceDisplayName("Admin.System.SeNames.Language")]
    public string Language { get; set; }

    [IbileHubResourceDisplayName("Admin.System.SeNames.Details")]
    public string DetailsUrl { get; set; }

    #endregion
}

I have created these two extension methods.

 public static IEntity<T> ToModel<T>(this IEntityViewModel<T> viewModel)
    {
        return (IEntity<T>)Mapper.Map(viewModel, viewModel.GetType(), typeof(IEntity<T>));
    }

    public static IEntityViewModel<T> ToViewModel<T>(this IEntity<T> entity)
    {
        return (IEntityViewModel<T>)Mapper.Map(entity, entity.GetType(), typeof(IEntityViewModel<T>));
    }

and i have used it here.

IEntityViewModel<Int64> entityViewModel = urlRec.ToViewModel<Int64>();
        IEntity<Int64> entity = entityViewModel.ToModel<Int64>();

I don't know how does your viewmodel looks like so i have created one test viewmodel and interface.

 public interface IEntityViewModel<T>
{
    T Id { get; set; }
    DateTime Created { get; set; }
    DateTime Updated { get; set; }
    byte[] RowVersion { get; set; }

}

public class UrlRecViewModel : IEntityViewModel<Int64>
{
    public int EntityId { get; set; }
    public string EntityName { get; set; }
    public string Slug { get; set; }
    public bool IsActive { get; set; }
    public int LanguageId { get; set; }
    public long Id { get; set; }
    public DateTime Created { get; set; }
    public DateTime Updated { get; set; }
    public byte[] RowVersion { get; set; }
}

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