score:2

Accepted answer

It sounds like you need to parameterize the property name in DataContext. One way to do it is to pass in a function that takes a DataContext and returns your enumerable (a Func<DataContext, IEnumerable<TSource>>) and then you would pass in a lambda like dc => dc.Document as that parameter.

Another option, which would work (but without type-safety) is to pass in the property's name as a string and then use Reflection to access it.

public void ProcessImportObjects<TSource, TDestination>
    (Func<DataContext, IEnumerable<TSource>> dcProperty,
     Action<TDestination, ImportDataSource, int> processMethod,
     ImportDataSource importSource,
     int resultId)
{
    using (DataContext dc = new DataContext())
    {
        dc.ObjectTrackingEnabled = false;
        Mapper.CreateMap<TSource, TDestination>();

        foreach (TSource d in dcProperty(dc))
        {
            TDestination doc = Mapper.Map<TSource, TDestination>(d);
            processMethod(doc, importSource, resultId);
        }
    }
}

Related Articles