Skip to content

Instantly share code, notes, and snippets.

@ahmad2x4
Created September 28, 2014 07:55
Show Gist options
  • Save ahmad2x4/63b6c37597c105bfccbe to your computer and use it in GitHub Desktop.
Save ahmad2x4/63b6c37597c105bfccbe to your computer and use it in GitHub Desktop.
Blog Automapper Expression 6
public class SourceRepositorySql : IRepository<Source, Destination>, IDisposable
{
private readonly MyContext _context;
public SourceRepositorySql(MyContext context)
{
_context = context;
}
public void Add(Source source)
{
_context.Users.Add(Mapper.Map<Destination>(source));
}
public IQueryable<Destination> Find(Expression<Func<Source, bool>> predicate)
{
Expression<Func<Destination, Source>> mapper = Mapper.Engine.CreateMapExpression<Destination, Source>();
Expression<Func<Destination, bool>> mappedSelector = predicate.Compose(mapper);
return _context.Users.Where(mappedSelector);
}
private bool _disposed;
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_context.Dispose();
}
}
_disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment