public void Visit_SalesPerson_Customer_Orders_Should_Return_Customer_Orders_As_Path() { Expression<Func<SalesPerson, object>> expression = x => x.PrimaryCustomer.Orders; var visitor = new MemberAccessPathVisitor(); visitor.Visit(expression); Assert.That(visitor.Path, Is.EqualTo("PrimaryCustomer.Orders")); }
public virtual void Expand <T, TTarget>(Expression <Func <TTarget, object> > path) { Guard.ArgumentNotNull(path, "path"); string pathExpression = String.Empty; var visitor = new MemberAccessPathVisitor(); visitor.Visit(path); if (typeof(T) == typeof(TTarget)) { _expands.Add(visitor.Path); } else { //The path represents a collection association. Find the property on the target type that //matches an IEnumerable<TTarget> property. pathExpression = visitor.Path; var rootType = typeof(T); var targetType = typeof(IEnumerable <TTarget>); var targetProperty = (from property in rootType.GetProperties() where targetType.IsAssignableFrom(property.PropertyType) select property).FirstOrDefault(); if (targetProperty != null) { pathExpression = String.Format("{0}.{1}", targetProperty.Name, pathExpression); } } if (!String.IsNullOrEmpty(pathExpression)) { _expands.Add(pathExpression); } }
/// <summary> /// Instructs the repository to eager load entities that may be in the repository's association path. /// </summary> /// <param name="path">The path of the child entities to eager load.</param> /// <remarks>Implementors should throw a <see cref="NotSupportedException"/> if the underling provider /// does not support eager loading of entities</remarks> public override IRepository <TEntity> With <T>(Expression <Func <T, object> > path) { Guard.Against <ArgumentNullException>(path == null, "Expected a non null Expression representing a path to be eger loaded."); var visitor = new MemberAccessPathVisitor(); visitor.Visit(path); if (typeof(T) == typeof(TEntity)) { _expands.Add(visitor.Path); } else { //The path represents an collection association. Find the property on the target type that //matches a IEnumerable<T> property. var pathExpression = visitor.Path; var targetType = typeof(TEntity); var matchesType = typeof(IEnumerable <T>); var targetProperty = (from property in targetType.GetProperties() where matchesType.IsAssignableFrom(property.PropertyType) select property).FirstOrDefault(); if (targetProperty != null) { pathExpression = string.Format("{0}.{1}", targetProperty.Name, pathExpression); } _expands.Add(pathExpression); } return(this); }
public void Visit_Throws_NotSupportedException_When_Expression_Contains_Method_Call() { Expression <Func <SalesPerson, object> > expression = x => x.MethodAccess(); var visitor = new MemberAccessPathVisitor(); Assert.Throws <NotSupportedException>(() => visitor.Visit(expression)); }
public void Visit_SalesPerson_Customer_Orders_Should_Return_Customer_Orders_As_Path() { Expression <Func <SalesPerson, object> > expression = x => x.PrimaryCustomer.Orders; var visitor = new MemberAccessPathVisitor(); visitor.Visit(expression); Assert.That(visitor.Path, Is.EqualTo("PrimaryCustomer.Orders")); }
protected override void ApplyFetchingStrategy(Expression[] paths) { Guard.Against <ArgumentNullException>((paths == null) || (paths.Length == 0), "Expected a non-null and non-empty array of Expression instances representing the paths to eagerly load."); string currentPath = string.Empty; paths.ForEach <System.Linq.Expressions.Expression>(delegate(System.Linq.Expressions.Expression path) { MemberAccessPathVisitor visitor = new MemberAccessPathVisitor(); visitor.Visit(path); currentPath = !string.IsNullOrEmpty(currentPath) ? (currentPath + "." + visitor.Path) : visitor.Path; ((EFCoreRepository <TEntity>) this)._includes.Add(currentPath); }); }
public static IEFFetchingRepository <TEntity, TReleated> ThenFetchMany <TEntity, TFetch, TReleated>(this IEFFetchingRepository <TEntity, TFetch> repository, Expression <Func <TFetch, IEnumerable <TReleated> > > selector) where TEntity : class { Guard.Against <ArgumentNullException>(repository == null, "Expected a non-null IEFFetchingRepository<> instance."); var visitor = new MemberAccessPathVisitor(); visitor.Visit(selector); var includePath = repository.FetchingPath + "." + visitor.Path; repository.RootRepository.AddInclude(includePath); return((IEFFetchingRepository <TEntity, TReleated>) Activator.CreateInstance(typeof(EFFetchingRepository <TEntity, TReleated>), repository.RootRepository, includePath)); }
public static IEFFetchingRepository <TEntity, TReleated> Fetch <TEntity, TReleated>(this IRepository <TEntity> repository, Expression <Func <TEntity, TReleated> > selector) where TEntity : class { Guard.Against <ArgumentNullException>(repository == null, "Expected a non-null IRepository<> instance."); var efRepository = repository as EFRepository <TEntity>; Guard.Against <InvalidOperationException>(efRepository == null, "Cannot use Entity Framework's Fetch extension on the underlying repository. The repository " + "does not inherit or is not a EFRepository<> instance. The Entity Framework's fetching extensions can " + "only be used by entity framework's repository EFRepository<>."); var visitor = new MemberAccessPathVisitor(); visitor.Visit(selector); efRepository.AddInclude(visitor.Path); return((IEFFetchingRepository <TEntity, TReleated>) Activator.CreateInstance(typeof(EFFetchingRepository <TEntity, TReleated>), efRepository, visitor.Path)); }
public void Visit_Throws_NotSupportedException_When_Expression_Contains_Method_Call() { Expression<Func<SalesPerson, object>> expression = x => x.MethodAccess(); var visitor = new MemberAccessPathVisitor(); Assert.Throws<NotSupportedException>(() => visitor.Visit(expression)); }