public IQueryable <TCollection> QueryRelation <TCollection>(Expression <Func <TEntity, ICollection <TCollection> > > expr) { var path = PropertyPath <TEntity> .Create(expr); EntityEntry node = entity; foreach (var prop in path.Properties.Take(path.Properties.Count - 1)) { var isCollection = typeof(IEnumerable).IsAssignableFrom(prop.PropertyType); if (isCollection && !ReferenceEquals(prop, path.Properties.Last())) { throw new ArgumentException("Invalid path: " + expr); } var refEntity = node.Reference(prop.Name); refEntity.Load(); if (refEntity.CurrentValue == null) { throw new NullReferenceException(); } node = entity.Context.Entry(refEntity.CurrentValue); } return((IQueryable <TCollection>)node.Collection(path.Properties.Last().Name).Query()); }
public IEntry <TEntity> LoadRelations <TProperty>(Expression <Func <TEntity, TProperty> > expr) { var path = PropertyPath <TEntity> .Create(expr); LoadRelations(path); return(this); }
public void Implicit() { var path = PropertyPath.Create <IReadOnlyObservableCollection <Fake>, int>(x => x.Count); var source = Mock.Of <IReadOnlyObservableCollection <Fake> >(); Assert.DoesNotThrow(() => new PropertyPathObservable <IReadOnlyObservableCollection <Fake>, int>(source, path)); }
public void TrySetValue_NullBranch() { var path = PropertyPath <Root> .Create(r => r.Branch.Value); var root = new Root(); Assert.False(path.TrySetValue(root, new object())); }
public IVMPropertyBuilder <TSource> GetPropertyBuilder <TSource>( Expression <Func <TVM, TSource> > sourceObjectSelector ) { var path = PropertyPath.Create(sourceObjectSelector); return(new VMPropertyBuilder <TVM, TSource>(path, _configuration)); }
public override IEnumerable <BAL.Business.DataContextParam> GetParams() { return(new DataContextParam[] { new DataContextParam("przedstawicielParam", "Przedstawiciel:", PropertyPath.Create <Enova.Business.Old.DB.Web.Kontrahent>("Przedstawiciel")) { ControlType = typeof(System.Windows.Forms.ComboBox) } }); }
public void GetSetValue() { Employee person = new Employee { FirstName = "Franz", LastName = "Huber", Address = new Address { City = "NY" } }; var path1 = PropertyPath.Create <Person, string>(p => p.FirstName); Assert.AreEqual("Franz", path1.GetValue(person)); path1.SetValue(person, "Hans"); Assert.AreEqual("Hans", person.FirstName); var path2 = PropertyPath.Create <Employee, string>(p => p.LastName); Assert.AreEqual("Huber", path2.GetValue(person)); path2.SetValue(person, "Maier"); Assert.AreEqual("Maier", person.LastName); var path3 = PropertyPath.Create <Employee, string>(p => p.Address.City); Assert.AreEqual("NY", path3.GetValue(person)); path3.SetValue(person, "AZ"); Assert.AreEqual("AZ", person.Address.City); person.Address = null; AssertHelper.Throws <NullReferenceException>(() => path3.GetValue(person) ) .Containing("'[Employee].Address'") .Containing("'[Employee].Address.City'"); var path4 = PropertyPath.CreateWithDefaultValue <Employee, string>(p => p.Address.City, "-"); Assert.AreEqual("-", path4.GetValue(person)); // SetValue should be ignored... path4.SetValue(person, "Test"); Assert.AreEqual(null, person.Address); var path5 = PropertyPath.Create <Person, Person>(p => p); Assert.AreEqual(person, path5.GetValue(person)); AssertHelper.Throws <InvalidOperationException>( () => path5.SetValue(person, person) ).Containing("empty"); path5 = PropertyPath.Empty <Person>(); Assert.AreEqual(person, path5.GetValue(person)); AssertHelper.Throws <InvalidOperationException>( () => path5.SetValue(person, person) ).Containing("empty"); }
public override System.Collections.Generic.IEnumerable <BAL.Business.DataContextParam> GetParams() { return(new DataContextParam[] { new DataContextParam("kategoriaParam", "Kategoria:", PropertyPath.Create <Enova.Business.Old.DB.Web.StatusDokumentu>("Kategoria")) { ControlType = typeof(System.Windows.Forms.ComboBox) } }); }
public static TValue ValueOrDefault <TValue>(Expression <Func <TValue> > path, TValue @default = default(TValue)) { var valuePath = PropertyPath.Create(path); if (valuePath.HasValue) { return(valuePath.Value); } return(@default); }
/// <summary> /// Observe propertychanges with values. /// </summary> /// <param name="source"> /// The source. /// </param> /// <param name="property"> /// The property. /// </param> /// <param name="signalInitial"> /// If true OnNext is called immediately on subscribe /// </param> /// <typeparam name="TNotifier"> /// </typeparam> /// <typeparam name="TProperty"> /// </typeparam> /// <returns> /// The <see cref="IObservable"/>. /// </returns> public static IObservable <EventPattern <PropertyChangedAndValueEventArgs <TProperty> > > ObservePropertyChangedWithValue <TNotifier, TProperty>( this TNotifier source, Expression <Func <TNotifier, TProperty> > property, bool signalInitial = true) where TNotifier : class, INotifyPropertyChanged { Ensure.NotNull(source, nameof(source)); Ensure.NotNull(property, nameof(property)); var propertyPath = PropertyPath.Create(property); return(source.ObservePropertyChangedWithValue(propertyPath, signalInitial)); }
public void TrySetValue_Branch() { var path = PropertyPath <Root> .Create(r => r.Branch.Value); var root = new Root { Branch = new Branch() }; Assert.Null(root.Branch.Value); Assert.True(path.TrySetValue(root, new object())); Assert.NotNull(root.Branch.Value); }
public override IEnumerable <DataContextParam> GetParams() { return(new DataContextParam[] { new DataContextParam("okresParam", "Okres:", PropertyPath.Create <Enova.Business.Old.DB.Web.Reklamacja>("DataDodania")) { ControlType = typeof(Enova.Business.Old.Controls.DateTimeSpanControl) } , new DataContextParam("przedstawicielParam", "Przedstawiciel:", null) { ControlType = typeof(System.Windows.Forms.ComboBox) } //,new DataContextParam("kontrahentParam", "Kontrahent:", PropertyPath.Create<Enova.Business.Old.DB.Web.Reklamacja>("Kontrahent")) }); }
public void Setup() { Configuration = new VMDescriptorConfiguration(new BehaviorChainConfiguration()); SourceObjectFactory = new VMPropertyBuilder <EmployeeVM, Employee>( PropertyPath.Create((EmployeeVM x) => x.SourcePerson), Configuration ); RootFactory = new VMPropertyBuilder <EmployeeVM, EmployeeVM>( PropertyPath.Empty <EmployeeVM>(), Configuration ); }
public static TValue ValueOrDefault <TSource, TValue>( TSource source, Expression <Func <TSource, TValue> > path, TValue @default = default(TValue)) { var valuePath = PropertyPath.Create(path); var maybe = valuePath.GetValue(source); if (maybe.HasValue) { return(maybe.Value); } return(@default); }
public void AddNested(int n) { var source = new ObservableCollection <Fake>(); var path = PropertyPath.Create <Fake, int>(x => x.Next.Next.Value); var view = source.AsMappingView(x => x.ObservePropertyChanged(path, true)); var sw = Stopwatch.StartNew(); for (int i = 0; i < n; i++) { var fake = new Fake(); source.Add(fake); } sw.Stop(); Console.WriteLine("// source.ObserveItemPropertyChanged(x => x.Next.Next.Value): {0} Adds took {1} ms {2:F3} ms each. {3}", n, sw.ElapsedMilliseconds, sw.Elapsed.TotalMilliseconds / n, DateTime.Now.ToShortDateString()); }
public void Parse() { var path1 = PropertyPath <Root> .Parse("Branch.Value"); var path2 = PropertyPath <Root> .Create(r => r.Branch.Value); Assert.Equal(path1.Properties, path2.Properties); Assert.Equal(path1.PropertyType, path2.PropertyType); var prop1 = typeof(Root).GetProperty(nameof(Root.Branch)); var prop2 = typeof(Branch).GetProperty(nameof(Branch.Value)); Assert.Equal(path1.Properties, new[] { prop1, prop2 }); Assert.Equal(typeof(object), path1.PropertyType); }
public void Concat() { var path1 = PropertyPath.Create <Employee, Address>(p => p.Address); var path2 = PropertyPath.Create <Address, string>(a => a.City); var path3 = PropertyPath.Concat(path1, path2); TestAddressPath(path3); var emptyEmployeePath = PropertyPath.Empty <Employee>(); var emptyStringPath = PropertyPath.Create <string, string>(s => s); var path4 = PropertyPath.Concat(emptyEmployeePath, path3); var path5 = PropertyPath.Concat(path3, emptyStringPath); TestAddressPath(path4); TestAddressPath(path5); }
/// <summary> /// Extension method for listening to property changes. /// Handles nested x => x.Level1.Level2.Level3 /// Unsubscribes & subscribes when each level changes. /// Handles nulls. /// </summary> /// <typeparam name="TNotifier"> /// </typeparam> /// <typeparam name="TProperty"> /// </typeparam> /// <param name="source"> /// </param> /// <param name="property"> /// </param> /// <param name="signalInitial"> /// If true OnNext is called immediately on subscribe /// </param> /// <returns> /// The <see cref="IObservable"/>. /// </returns> public static IObservable <EventPattern <PropertyChangedEventArgs> > ObservePropertyChanged <TNotifier, TProperty>( this TNotifier source, Expression <Func <TNotifier, TProperty> > property, bool signalInitial = true) where TNotifier : INotifyPropertyChanged { var me = (MemberExpression)property.Body; var pe = me.Expression as ParameterExpression; if (pe == null) { var path = PropertyPath.Create(property); return(source.ObservePropertyChanged(path, signalInitial)); } string name = me.Member.Name; return(source.ObservePropertyChanged(name, signalInitial)); }
public IEntry <TEntity> LoadRelations <TCollection, TProperty>( Expression <Func <TEntity, ICollection <TCollection> > > collectionExpr, Expression <Func <TCollection, TProperty> > propExpr = null ) where TProperty : class where TCollection : class { var path = PropertyPath <TEntity> .Create(collectionExpr); EntityEntry node = entity; foreach (var prop in path.Properties.Take(path.Properties.Count - 1)) { if (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType)) { throw new ArgumentException("Invalid path: " + collectionExpr); } var refEntity = node.Reference(prop.Name); refEntity.Load(); if (refEntity.CurrentValue == null) { return(this); } node = entity.Context.Entry(refEntity.CurrentValue); } node.Collection(path.Properties.Last().Name).Load(); if (propExpr != null) { var collection = (ICollection <TCollection>)path.Properties.Last().GetValue(node.Entity); foreach (var item in collection) { var entry = new Entry <TCollection>(entity.Context.Entry(item)); entry.LoadRelations(propExpr); } } return(this); }
public void AddNestedMerge(int n) { int count = 0; var source = new ObservableCollection <Fake>(); var path = PropertyPath.Create <Fake, int>(x => x.Next.Next.Value); var view = source.AsMappingView(x => x.ObservePropertyChanged(path, true)); var sw = Stopwatch.StartNew(); var subject = new Subject <IObservable <EventPattern <PropertyChangedEventArgs> > >(); subject.Switch().Publish().RefCount().Subscribe(_ => count++); for (int i = 0; i < n; i++) { var fake = new Fake(); source.Add(fake); subject.OnNext(view.Merge()); } sw.Stop(); Console.WriteLine("// source.ObserveItemPropertyChanged(x => x.Next.Next.Value): {0} Adds took {1} ms {2:F3} ms each. {3}", n, sw.ElapsedMilliseconds, sw.Elapsed.TotalMilliseconds / n, DateTime.Now.ToShortDateString()); }
internal static IMaybe <TValue> ValuePath <TValue>(Expression <Func <TValue> > path) { var valuePath = PropertyPath.Create(path); return(valuePath); }
public void Test_ToString() { var path = PropertyPath <Root> .Create(r => r.Branch.Value); Assert.Equal("Object Branch.Value", path.ToString()); }
public void ToStringTest() { var path = PropertyPath.Create <Fake, int>(x => x.Next.Value); Assert.AreEqual("x => x.Next.Value", path.ToString()); }