ecengel
9/27/2018 - 2:22 AM

ServiceV8

public class ContactService
    {
        private readonly SampleContext _dbContext;

        public ContactService(SampleContext dbContext)
        {
            _dbContext = dbContext;
        }

        public List<Contact> Contacts(Action<IQueryOption<Contact>> optionAction = null)
        {
            var option = new QueryOption<Contact>();
            optionAction?.Invoke(option);

            var dbSet = _dbContext.Contacts;
            foreach (var eagerload in option?.EagerLoads ?? new List<Expression<Func<Contact, object>>>())
                dbSet.Include(eagerload);

            return dbSet.Where(option?.Filter ?? (x => true)).ToList();
        }

        public List<EmailAddress> EmailAddresses(Action<IQueryOption<EmailAddress>> optionAction = null)
        {
            var option = new QueryOption<EmailAddress>();
            optionAction?.Invoke(option);

            var dbSet = _dbContext.EmailAddresses;
            foreach (var eagerload in option?.EagerLoads ?? new List<Expression<Func<EmailAddress, object>>>())
                dbSet.Include(eagerload);

            return dbSet.Where(option?.Filter ?? (x => true)).ToList();
        }
    }