Scott Harrison - The golden apples
Henry Parker's Robot Wars

ASP.Net, ADO, MS Access
C# How to return a query data set from fluent Api

C# How to return a query data set from fluent Api

(35 hits)
Overview: Hits=19 This code snippet demonstrates how to invoke an IQueryable using fluent Api. The FluentAddressBook class is encapsulated within the AddressBookModule class. The AddressModule class may contain one or more Fluent classes. Each Fluent class can represent a domain of work. When I call the Query api of the fluentaddressbook class, the object is instantiated.

It is important for you to remember that you can not call Apply() within a foreach. Entity Framework conflicts with the IEnumerable thread of the foreach and an error will be thrown.

Hits=18 The fluentAddressBookQuery has an method called GetAddressBooksByExpression which accepts an delegate function of type AddressBook. Linq expressions can be passed as a delegate function to the repository dataset through unit of work. AddressBookRepository inherits from the generic parent class repository of type T as addressBook. This allows you to call GetObjectsQueryable of the parent class repository from the derived class addressBookRepository. The delegate function is applied as a predicate to the dataset of data context.

Unit Test


AddressBookModule abMod = new AddressBookModule();

IQueryable<AddressBook> query=abMod.AddressBook.Query().GetAddressBooksByExpression(a => a.Name.Contains("Test"));
 foreach (var item in query)
            {
                Assert.Equal("James Dean",item.Name );

                abMod.AddressBook.DeleteAddressBook(item);
            }
            abMod.AddressBook.Apply(); //outside the foreach to avoid thread conflict of the foreach

Address Module


class AddressBookModule : AbstractModule
    {

        public FluentAddressBook AddressBook = new FluentAddressBook();
   
    }

FluentAddressBook


  public class FluentAddressBook : AbstractModule, IAddressBook
    {
        protected UnitOfWork unitOfWork = new UnitOfWork();

        public FluentAddressBook() { }

        public FluentAddressBookQuery _query;
        //= new FluentAddressBookQuery();

        public IAddressBookQuery Query()
        {
            if (_query == null)
            {
                _query = new FluentAddressBookQuery(unitOfWork);
            }
            return _query as IAddressBookQuery;
        }
}

FluentAddressBookQuery


 public class FluentAddressBookQuery : AbstractModule, IAddressBookQuery
    {
        protected UnitOfWork _unitOfWork;
        public FluentAddressBookQuery(UnitOfWork unitOfWork) { _unitOfWork = unitOfWork; }

 public IQueryable<AddressBook> GetAddressBooksByExpression(Expression<Func<AddressBook, bool>> predicate)
        {
            try
            {
                //queryableAddressBook = 
                return _unitOfWork.addressBookRepository.GetObjectsQueryable(predicate) as IQueryable<AddressBook>;
                //return this as IAddressBookQuery;
            }
            catch (Exception ex)
            {
                throw new Exception(GetMyMethodName(), ex);
            }

        }
}

addressBookRepository


 public class AddressBookRepository: Repository<AddressBook>
{

}

public class Repository<T> where T : class
    {
        private DbContext _dbContext;

        public Repository(DbContext dbContext)
        {
             _dbContext=dbContext;  
        }
 
 public IQueryable<T> GetObjectsQueryable(Expression<Func<T, bool>> predicate,string includeTable="")
        {
            IQueryable<T> result = _dbContext.Set<T>().Where(predicate);
            if (includeTable != "")
            {
                result=result.Include(includeTable);
            }
                
            
                return result;
        }
}
....

...<<<Register to correspond>>> ...

Members : 152
Name:
Email:

Register to View

Help