using System.Collections.Generic;
public static void Main()
List<IYourEntity> lstYourEntitiesModifiedSince = GetAllModifiedSince(DateTime.UtcNow);
foreach (var item in lstYourEntitiesModifiedSince)
var myItem = item as YourEntity1;
Console.WriteLine(string.Format("{0}: {1}", myItem.Id, myItem.YourEntityName));
List<EntityAdapter<IYourEntity, IMyEntity>> lstMyEntitiesModifiedSince = lstYourEntitiesModifiedSince.ToEntityAdapterList();
foreach (var item in lstMyEntitiesModifiedSince)
var myItem = item.CastToMyEntityType() as MyEntity1;
Console.WriteLine(string.Format("{0}: {1}", myItem.Id, myItem.MyEntityName));
public static List<IYourEntity> GetAllModifiedSince(DateTime modifiedSince)
List<IYourEntity> yourEntityList = new List<IYourEntity>();
yourEntityList.Add(new YourEntity1()
{Id = 1, YourEntityName = "Charlie"});
yourEntityList.Add(new YourEntity1()
{Id = 2, YourEntityName = "Sarah"});
return yourEntityList as List<IYourEntity>;
public interface IYourEntity
public class YourEntity1 : IYourEntity
public string YourEntityName
public interface IMyEntity
public class MyEntity1 : IMyEntity
public string MyEntityName
public DateTime CreatedOn
public interface IMyEntityConverter
IMyEntity Convert(IYourEntity yourEntity);
public class MyEntity1Converter : IMyEntityConverter
public IMyEntity Convert(IYourEntity yourEntity)
var castedYourEntity = yourEntity as YourEntity1;
Id = castedYourEntity.Id,
MyEntityName = castedYourEntity.YourEntityName,
CreatedOn = DateTime.UtcNow
public class EntityAdapter<YourType, MyType>
where YourType : IYourEntity
protected YourType wrappedEntity;
protected IMyEntityConverter converter;
public EntityAdapter(YourType wrappedEntity, IMyEntityConverter converter)
this.wrappedEntity = wrappedEntity;
this.converter = converter;
public static implicit operator YourType(EntityAdapter<YourType, MyType> entityAdapter) => entityAdapter.wrappedEntity;
public static explicit operator MyType(EntityAdapter<YourType, MyType> entityAdapter) =>
(MyType) entityAdapter.converter.Convert(entityAdapter.wrappedEntity);
public MyType CastToMyEntityType()
public static class EntityAdapterExtensions
public static List<IMyEntity> ToIMyEntityList(this List<EntityAdapter<IYourEntity, IMyEntity>> lstEntityAdapters)
return lstEntityAdapters.ConvertAll(e => e.CastToMyEntityType());
public static List<EntityAdapter<IYourEntity, IMyEntity>> ToEntityAdapterList(this List<IYourEntity> lstYourEntities)
return lstYourEntities.Select(e =>
case YourEntity1 yourEntity1:
return new EntityAdapter<IYourEntity, IMyEntity>(yourEntity1, new MyEntity1Converter());
throw new NotSupportedException("You forgot to map " + e.GetType());