locked
Help needed in linq expression and func RRS feed

  • Question

  • User-1991255608 posted

    Hello everyone,

    I downloaded a basic e-commerce project from GitHub and trying to understand how things are working in it.
    I come across below two classes, where I am having hard time in understanding how code is working in it. Specially in BaseSpecification class which I believe is accepting tblProduct database table object and doing something with it.

    Can any expert help me in understanding BaseSpecification class line by line. 

    Thanks in advance.

    Class CatalogFilterSpecification

    //tblProducts is table in database containing ProductId, CatalogBrandId, CatalogTypeId, ProductName columns.

    public class CatalogFilterSpecification : BaseSpecification<tblProducts> { public CatalogFilterSpecification(int? brandId, int? typeId) : base(i => (!brandId.HasValue || i.CatalogBrandId == brandId) && (!typeId.HasValue || i.CatalogTypeId == typeId)) { } }

    Class BaseSpecification

        public abstract class BaseSpecification<T> : ISpecification<T>
        {
            public BaseSpecification(Expression<Func<T, bool>> criteria)
            {
                Criteria = criteria;
            }
    
            //Please help in understanding below each of below properties and methods, and what would be the 
    //possible output when tblProduct object is passed into it.
    public Expression<Func<T, bool>> Criteria { get; } public List<Expression<Func<T, object>>> Includes { get; } = new List<Expression<Func<T, object>>>(); public List<string> IncludeStrings { get; } = new List<string>(); protected virtual void AddInclude(Expression<Func<T, object>> includeExpression) { Includes.Add(includeExpression); } protected virtual void AddInclude(string includeString) { IncludeStrings.Add(includeString); } }

    Sunday, February 17, 2019 12:58 PM

All replies

  • User1520731567 posted

    Hi itHighway,

    Understanding the program needs to be based on the context,

    As I can see in your code,class CatalogFilterSpecification inherits BaseSpecification,

            public Expression<Func<T, bool>> Criteria { get; }
    
            public List<Expression<Func<T, object>>> Includes { get; } = new List<Expression<Func<T, object>>>();

    This defined two read-only type of delegate.

    More details about Expression<Func<T, bool>,you could refer to:

    https://docs.microsoft.com/en-us/dotnet/api/system.func-2?view=netframework-4.7.2

    https://docs.microsoft.com/en-us/dotnet/api/system.linq.expressions.expression-1?view=netframework-4.7.2

    protected virtual void AddInclude(Expression<Func<T, object>> includeExpression)
            {
                Includes.Add(includeExpression);//add to Includes
            }
    
            protected virtual void AddInclude(string includeString)
            {
                IncludeStrings.Add(includeString);//add a string type to List<string> IncludeStrings
            }

    These are two overloaded functions,

    According to the different parameters, enter the corresponding function to perform different operations.

    Or you could give me github link so that I can understand it better.

    Best Regards.

    Yuki Tao

    Monday, February 18, 2019 8:43 AM
  • User-1991255608 posted

    Hi Yuki,

    Thanks for your response. Below is the link to GitHub project.

    https://github.com/dotnet-architecture/eShopOnWeb

    Both CatalogFilterSpecification.cs and BaseSpecification.cs are located in ApplicationCore > Specifications folder.

    Monday, February 18, 2019 6:00 PM
  • User1520731567 posted

    Hi itHighway,

    Accorrding to the code about during define to call,

    BaseSpecification is mainly built on the underlying method, and then inherited by other classes,

    the subclass can call the method of the parent class,

    in the business logic layer Controller by means of the factory class call interface, execute the subclass or parent class method.

    Best Regards.

    Yuki Tao

    Tuesday, February 19, 2019 8:13 AM