jeudi 18 juillet 2019

I want to create a reflection on a generic TEntity class and ienumerating on its properties to eagerly fetch them

I have various models in my code one is MenuEntry which is like-

public class MenuEntry : CorporationMetadata, IEntity
    {
        public virtual int MenuEntryId { get; set; }
        public virtual MenuEntryType MenuEntryType { get; set; }
        public virtual string Name { get; set; }
        public virtual Category Category { get; set; }
        public virtual TaxGroup TaxGroup { get; set; }
        public virtual PrinterGroup PrinterGroup { get; set; }
        public virtual bool OpenPrice { get; set; }
        public virtual bool OpenName { get; set; }
        public virtual bool Active { get; set; }
        public virtual IList<Barcode> Barcodes { get; set; }
     }   

When I created only MenuEntry Repository then I am fetching all its details eagerly like this

public override IQueryable<MenuEntry> GetEager(Expression<Func<MenuEntry, bool>> filter)
        {
            var menuEntries = _unitOfWork.Session.Query<MenuEntry>().Where(filter)
                .FetchMany(x => x.Barcodes).ToFuture();           

            _unitOfWork.Session.Query<MenuEntry>()
                .Fetch(x => x.MenuEntryType).ToFuture();

            _unitOfWork.Session.Query<MenuEntry>()
                .Fetch(x => x.CreatedUser).ToFuture();

            _unitOfWork.Session.Query<MenuEntry>()
                .Fetch(x => x.LastModifiedUser).ToFuture();

            _unitOfWork.Session.Query<MenuEntry>()
                .Fetch(x => x.Category).ToFuture();

            _unitOfWork.Session.Query<MenuEntry>()
                .Fetch(x => x.TaxGroup).ToFuture();           

            _unitOfWork.Session.Query<MenuEntry>()
                .Fetch(x => x.PrinterGroup).ToFuture();

            return menuEntries.AsQueryable();
        }

Now if I am using a generic repository I am fetching properties with getproperties() method like:

public virtual IQueryable<TEntity> GetEager(Expression<Func<TEntity, bool>> filter)
        {
            var result = _unitOfWork.Session.Query<TEntity>().Where(filter).ToFuture();
            PropertyInfo[] properties = typeof(TEntity).GetProperties();

            foreach (PropertyInfo property in properties)
            {                
                if (property.PropertyType.GetInterface("IEntity") != null)
                {
                    _unitOfWork.Session.Query<TEntity>()
                        .Fetch(x => x.property).ToFuture();
                }
                else
                {
                    if (property.PropertyType != typeof(string) && typeof(IEnumerable<TEntity>).IsAssignableFrom(property.PropertyType))
                    {
                        _unitOfWork.Session.Query<TEntity>()
                            .FetchMany(x => x.propert).ToFuture();
                    }
                }
            }

            return result.AsQueryable();
        }

but this is not working and x => x.property is giving error

How could I eagerly fetch its all properties?





Aucun commentaire:

Enregistrer un commentaire