public MapperRequest(Type sourceType, PropModelType propModel, string configPackageName) { SourceType = sourceType ?? throw new ArgumentNullException(nameof(sourceType)); PropModelFullClassName = null; ConfigPackageName = configPackageName ?? throw new ArgumentNullException(nameof(configPackageName)); PropModel = propModel ?? throw new ArgumentNullException(nameof(propModel)); }
public bool TryGetPropModel(string fullClassName, long generationId, out PropModelType propModel) { lock (_syncLock) { if (_cache.TryGetValue(fullClassName, out PropBagModelFamilyCollInterface familyCollection)) { if (familyCollection.TryGetPropModel(generationId, out propModel)) { return(true); } } if (generationId == GEN_ZERO) { if (TryFetchFromSourceProviders(fullClassName, out propModel)) { Add(propModel); return(true); } } } propModel = null; return(false); }
private Type GetWrapperType_Internal(PropModelType propModel, Type typeToWrap, string fullClassName) { TypeDescription td = _typeDescCachingService.GetOrAdd(new NewTypeRequest(propModel, typeToWrap, fullClassName)); Type newWrapperType = _wrapperTypeCachingService.GetOrAdd(td); return(newWrapperType); }
public IDictionary <string, PropModelType> LoadPropModels(string basePath, string[] filenames) { Dictionary <string, PropModelType> result = new Dictionary <string, PropModelType>(); Thread thread = new Thread(() => { ResourceDictionary rd = _resourceDictionaryProvider.Load(basePath, filenames); foreach (ResourceDictionary rdChild in rd.MergedDictionaries) { foreach (DictionaryEntry kvp in rdChild) { if (kvp.Value is PropBagTemplate pbt) { PropModelType propModel = _pbtParser.ParsePropModel(pbt); result.Add((string)kvp.Key, propModel); _propModelCache.Add((string)kvp.Key, propModel); } } } }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); thread = null; _propModelCache = result; return(result); }
private void CheckOwnerShip(PropModelType propModel) { if (!TryFind(propModel, out long generationId)) { throw new InvalidOperationException("We are being asked to fix a PropModel that is not in our cache."); } }
// BaseType + PropModel (BaseType known at compile time.) public object GetNewViewModel <BT>(PropModelType propModel, ViewModelFactoryInterface viewModelFactory, object propBagAutoMapperService, IPropFactory propFactory = null, string fcnOverride = null) where BT : class, IPropBag { object result = GetNewViewModel(typeof(BT), propModel, viewModelFactory, propBagAutoMapperService, propFactory, fcnOverride); return(result); }
private PropModelType FetchPropModel(string resourceKey) { try { if (CanFindPropBagTemplateWithJustKey) { PropBagTemplate pbt = _propBagTemplateBuilder.GetPropBagTemplate(resourceKey); PropModelType pm = _pbtParser.ParsePropModel(pbt); FixUpPropFactory(pm, _propFactoryFactory); return(pm); } else if (HasPbtLookupResources) { throw new InvalidOperationException($"A call providing only a ResourceKey can only be done, " + $"if this PropModelBuilder was supplied with a PropBagTemplateBuilder upon construction. " + $"No class implementing: {nameof(IPropBagTemplateBuilder)} was provided. " + $"Please supply a PropBagTemplate object."); } else { throw new InvalidOperationException($"A call providing only a ResourceKey can only be done, " + $"if this PropModelBuilder was supplied with the necessary resources upon construction. " + $"A {_propBagTemplateBuilder.GetType()} was provided, but it does not have the necessary resources. " + $"Please supply a ResourceDictionary and ResourceKey or a ProbBagTemplate object."); } } catch (System.Exception e) { throw new ApplicationException($"PropBagTemplate for ResourceKey = {resourceKey} was not found.", e); } }
private PropModelType FixUpPropFactory(PropModelType propModel, IPropFactoryFactory propFactoryGenerator) { //// Include a reference to this PropModelBuilder //propModel.PropModelBuilder = this; // If the propModel does not supply a PropFactory, create one using the PropFactoryType. if (propModel.PropFactory == null) { if (propModel.PropFactoryType != null) { // If the propModel does not specify a PropFactory, but it does specify a PropFactoryType, // use the PropFactoryFactory given to us to create a PropFactory. IPropFactory generated = propFactoryGenerator.BuildPropFactory(propModel.PropFactoryType); propModel.PropFactory = generated; System.Diagnostics.Debug.WriteLine($"Just created a new PropFactory of type: {propModel.PropFactoryType} for the propModel with FullClassName: {propModel}."); } else { throw new InvalidOperationException($"The PropModel with FullClassName: {propModel} does not have a value for PropFactory, nor does it have a value for PropFactoryType."); } } return(propModel); }
public long Add(PropModelType propModel) { string fullClassName = propModel.FullClassName ?? throw new InvalidOperationException("The PropModel must have a non-null FullClassName."); if (propModel.PropModelCache != null) { throw new InvalidOperationException("The PropModel is already associated with a PropModel cache."); } long generationId; lock (_syncLock) { if (_cache.TryGetValue(fullClassName, out PropBagModelFamilyCollInterface familyCollection)) { generationId = familyCollection.Add(propModel); } else { familyCollection = new SimplePropModelFamilyCollection(); _cache.Add(fullClassName, familyCollection); generationId = familyCollection.Add(propModel); } AssignNewGenerationId(propModel, generationId); // Associate the propModel with this cache. propModel.PropModelCache = this; } return(generationId); }
public PersonVM(PropModelType pm, ViewModelFactoryInterface viewModelFactory, IPropBagMapperService autoMapperService, IPropFactory propFactory, string fullClassName) : base(pm, viewModelFactory, autoMapperService, propFactory, fullClassName) { //System.Diagnostics.Debug.WriteLine("PersonVM is being created with a PropModel."); //////IList<string> pNamesFromOurProvider = TypeInspectorUtility.GetPropertyNames ////// (_typeDescriptionProvider.GetTypeDescriptor(this)); ////ICustomTypeDescriptor ourCustomTypeDescriptor = _typeDescriptionProvider.GetTypeDescriptor(this); ////IList<string> pNamesFromOurProvider = TypeInspectorUtility.GetPropertyNames //// (ourCustomTypeDescriptor); //IList<string> pNamesFromOurPropModel = TypeInspectorUtility.GetPropertyNames // (pm.CustomTypeDescriptor); ////IList<string> pNamesFromAppDomain_WT = TypeInspectorUtility.GetPropertyNames //// (typeof(PersonVM), this); ////IList<string> pNamesFromAppDomain_WT = TypeInspectorUtility.GetPropertyNames //// (this.GetType(), this); //IList<string> pNamesFromAppDomain = TypeInspectorUtility.GetPropertyNames // (this); }
public TypeDescription GetTypeDescription(PropModelType propModel, Type typeToWrap, string fullClassName) { NewTypeRequest request = new NewTypeRequest(propModel, typeToWrap, fullClassName); TypeDescription result = GetTypeDescription(request); return(result); }
public MainWindowViewModel(PropModelType pm, ViewModelFactoryInterface viewModelFactory, IPropBagMapperService autoMapperService) : base(pm, viewModelFactory, autoMapperService) { //System.Diagnostics.Debug.WriteLine("Beginning to construct MainWindowViewModel -- From PropModel."); //System.Diagnostics.Debug.WriteLine("Completed Constructing MainWindowViewModel -- From PropModel."); }
public DestinationModel1(PropModelType pm, ViewModelFactoryInterface viewModelFactory, IPropBagMapperService autoMapperService, IPropFactory propFactory, string fullClassName) : base(pm, viewModelFactory, autoMapperService, propFactory, fullClassName) { TryOpenPropSet(); AddProp <int>("NewAfterFixedProp", null, null, 0); TryFixPropSet(); }
//private MainViewModel() { } // Shows that if no default constructor is available, the one that takes a single byte is use. // If it not desirable to provide a public, default, parameterless constructor, // a consructor that takes a single byte can be used instead. // NOTE: Neither of these constructors is required if an instance of this class already exists from the proerty // marked with the PropBagInstanceAttribute. // An instance of this class must be available so that we create an instance of a Action<T,T> delegate. //public MainViewModel(byte dummy) : base(dummy) {} /// <summary> /// Constructor used by View to create with properties /// </summary> /// <param name="pm"></param> public MainViewModel(PropModelType pm, ViewModelFactoryInterface viewModelFactory, IPropBagMapperService propBagMapperService, IPropFactory propFactory, string fullClassName) : base(pm, viewModelFactory, propBagMapperService, propFactory, fullClassName) { this.PropFirstDidChange = false; this.PropMyStringDidChange = false; this.PropMyPointDidChange = false; }
//public DestinationModel3(PropBagTypeSafetyMode typeSafetyMode, PSAccessServiceCreatorInterface storeAccessCreator, // string fullClassName, IPropFactory propFactory) // : base(typeSafetyMode, storeAccessCreator, propFactory, fullClassName) //{ // AddProp<Guid>("ProductId", null, null, Guid.NewGuid()); // AddProp<int>("Amount", null, null, initialValue: 0); // AddProp<double>("Size", null, null, 10.1); // AddProp<MyModel4>("Deep", null, null, null); //} public DestinationModel3(PropModelType pm, ViewModelFactoryInterface viewModelFactory, IPropBagMapperService autoMapperService, IPropFactory propFactory, string fullClassName) : base(pm, viewModelFactory, autoMapperService, propFactory, fullClassName) { //AddProp<Guid>("ProductId", null, null, Guid.NewGuid()); //AddProp<int>("Amount", null, null, initialValue: 0); //AddProp<double>("Size", null, null, 10.1); //AddProp<MyModel4>("Deep", null, null, null); }
private IEnumerable <PropertyDescription> GetPropertyDescriptions(PropModelType pm) { List <PropertyDescription> result = new List <PropertyDescription>(); foreach (IPropItemModel pi in pm.GetPropItems()) { result.Add(new PropertyDescription(pi.PropertyName, pi.PropertyType, canWrite: true)); } return(result); }
public Type GetWrapperType <BT>(PropModelType propModel) where BT : class, IPropBag { //TypeDescription td = _typeDescCachingService.GetOrAdd(new NewTypeRequest(propModel, typeof(BT), null)); //Type newWrapperType = _wrapperTypeCachingService.GetOrAdd(td); //return newWrapperType; Type result = GetWrapperType_Internal(propModel, typeof(BT), null); return(result); }
public void Z_BindParent() { AutoMapperHelpers ourHelper = new AutoMapperHelpers(); IPropBagMapperService autoMapperService = ourHelper.GetAutoMapperSetup_V1(); IPropFactory propFactory_V1 = ourHelper.GetNewPropFactory_V1(); ICreateWrapperTypes wrapperTypeCreator = ourHelper.GetWrapperTypeCreator_V1(); ViewModelFactoryInterface viewModelFactory = ourHelper.ViewModelFactory; //viewModelFactory.PropModelCache.Add(propModel5); _propModelCache = viewModelFactory.PropModelCache; Assert.That(ourHelper.StoreAccessCreator.AccessCounter == 0, "The Provider of PropStoreAccessServices has not had its Access Counter reset."); List <DestinationModel1> destinationList = new List <DestinationModel1>(); PropModelHelpers pmHelpers = new PropModelHelpers(); // Set up Child VM (Using Model 5) PropModelType propModel5 = pmHelpers.GetPropModelForModel5Dest(propFactory_V1, _propModelCache); DestinationModel5 testChildVM = new DestinationModel5(pm: propModel5, viewModelFactory: viewModelFactory, autoMapperService: autoMapperService, propFactory: propFactory_V1, fullClassName: "PropBagLib.Tests.PerformanceDb.DestinationModel5"); Business b = new Business(); testChildVM.SetIt(b, "Business"); testChildVM.RegisterBinding <Business>("Business", "../Business"); //List<Person> personList = b.Get().ToList(); //ObservableCollection<Person> personList2 = new ObservableCollection<Person>(personList); //testChildVM.SetIt(personList2, "PersonCollection"); // Set up MainVM (Using Model 6) PropModelType propModel6 = pmHelpers.GetPropModelForModel6Dest(propFactory_V1, _propModelCache); DestinationModel6 testMainVM = new DestinationModel6(propModel6, viewModelFactory, autoMapperService, propFactory_V1, null); Business b2 = new Business(); testMainVM.SetIt(b2, "Business"); testMainVM.SetIt <DestinationModel5>(testChildVM, "ChildVM"); testMainVM.RegisterBinding <Business>("Business", "./ChildVM/Business"); b2 = new Business(); testMainVM.SetIt(b2, "Business"); }
public bool TryClone(PropModelType propModel, out PropModelType clonedCopy) { if (!ReferenceEquals(propModel.PropModelCache, this)) { throw new InvalidOperationException("We are being asked to clone a PropModel that is assocated with some other PropModel cache."); } propModel.PropModelCache = null; clonedCopy = (PropModelType)propModel.CloneIt(); Add(clonedCopy); return(true); }
public object GetNewViewModel(Type typeToCreate, PropModelType propModel, ViewModelFactoryInterface viewModelFactory, object propBagAutoMapperService, IPropFactory propFactory, string fcnOverride) { object[] parameters = new object[] { propModel, viewModelFactory, propBagAutoMapperService, propFactory, fcnOverride }; //BindingFlags bFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance; //object result = Activator.CreateInstance(typeToCreate, bFlags, binder: null, args: parameters, culture: null); object result = Activator.CreateInstance(typeToCreate, args: parameters); return(result); }
// TODO: Add locks to this code. public PropModelType GetPropModel(string resourceKey) { if (_propModelCache.TryGetValue(resourceKey, out PropModelType value)) { return(value); } else { PropModelType result = FetchPropModel(resourceKey); _propModelCache.Add(resourceKey, result); return(result); } }
// Gen Submit public IPropBagMapperRequestKeyGen SubmitPropBagMapperRequest ( PropModelType propModel, Type sourceType, string configPackageName ) { Type typeToCreate = propModel.RunTimeType; PropBagMapperReqSubDelegate mapperRequestSubmitter = GetPropBagMapperReqSubDelegate(sourceType, typeToCreate); IPropBagMapperRequestKeyGen result = mapperRequestSubmitter(propModel, configPackageName, this); return(result); }
private void AssignNewGenerationId(PropModelType propModel, long generationId) { if (propModel.IsFixed) { propModel.Open(); propModel.GenerationId = generationId; TryFix(propModel); } else { propModel.GenerationId = generationId; } }
/// <summary> /// Creates a new ViewModel using the specified PropModel. The PropFactory and FullClassName values recorded in /// the PropModel may be overridden by the pfOverrid and fcnOverride arguments. If these values should not be /// overridden set the arguments to null. /// </summary> /// <param name="propModel"></param> /// <param name="pfOverride"></param> /// <param name="fcnOverride"></param> /// <returns></returns> public object GetNewViewModel(PropModelType propModel, IPropFactory pfOverride, string fcnOverride) { object result = _viewModelActivator.GetNewViewModel ( typeToCreate: propModel.TypeToWrap, propModel: propModel, viewModelFactory: this, ams: AutoMapperService, pfOverride: pfOverride, fcnOverride: fcnOverride ); return(result); }
// Typed Submit with Mapping Configuration. public IPropBagMapperRequestKey <TSource, TDestination> SubmitPropBagMapperRequest <TSource, TDestination> ( PropModelType propModel, IConfigureAMapper <TSource, TDestination> mappingConfiguration, IHaveAMapperConfigurationStep configStarterForThisRequest, IPropFactory propFactory = null ) where TDestination : class, IPropBag { // Use the AutoMapperService to register a 'raw' request. IAutoMapperRequestKey <TSource, TDestination> autoMapperRequestKey = this.SubmitRawAutoMapperRequest <TSource, TDestination> ( propModel, mappingConfiguration, configStarterForThisRequest ); // Note: at this point we can fetch the Mapper Config Details. //IAutoMapperConfigDetails autoMapperConfigDetails = autoMapperRequestKey.AutoMapperConfigDetails; //IPropBagMapperConfigDetails propBagMapperConfigDetails = (IPropBagMapperConfigDetails)autoMapperConfigDetails; // Create a MapperBuilder for this request. IPropBagMapperBuilder <TSource, TDestination> propBagMapperBuilder = BuildTheAutoMapperBuilder <TSource, TDestination>(/*configStarterForThisRequest*/); // Create the PropBag Mapper Request. IPropBagMapperRequestKey <TSource, TDestination> propBagMapperRequestKey = new PropBagMapperRequestKey <TSource, TDestination> ( propBagMapperBuilder, autoMapperRequestKey // This contains a reference to the propModel. //, //propModel ); // Note: at this point we can fetch the Mapper Config Details from the PropBagMapperRequestKey. //IPropBagMapperConfigDetails propBagMapperConfigDetails = propBagMapperRequestKey.PropBagMapperConfigDetails; //// Store the request //IPropBagMapperKeyGen newMapRequest = RegisterPropBagMapperRequest(typedMapperRequest); //return (IPropBagMapperKey<TSource, TDestination>)newMapRequest; // We could store the request in the PropBagMapper Cache, but it would not be used. return(propBagMapperRequestKey); }
public Type GetWrapperType(PropModelType propModel, Type typeToWrap) { if (!typeToWrap.IsPropBagBased()) { throw new InvalidOperationException($"Type: {typeToWrap.Name} must derive from IPropBag."); } //TypeDescription td = _typeDescCachingService.GetOrAdd(new NewTypeRequest(propModel, typeToCreate, null)); //Type newWrapperType = _wrapperTypeCachingService.GetOrAdd(td); //return newWrapperType; Type result = GetWrapperType_Internal(propModel, typeToWrap, null); return(result); }
public bool TryFix(PropModelType propModel) { CheckOwnerShip(propModel); if (propModel.IsFixed) { System.Diagnostics.Debug.WriteLine("Already Fixed."); return(false); } else { propModel.Fix(); return(true); } }
public bool TryFind(PropModelType propModel, out long generationId) { string fullClassName = propModel.FullClassName ?? throw new ArgumentException("propModel.FullClassName cannot be null."); if (_cache.TryGetValue(propModel.FullClassName, out PropBagModelFamilyCollInterface list)) { generationId = list.Find(propModel); return(true); } else { generationId = -1; return(false); } }
public PropBagTypeDescriptor(PropModelType propModel, string propertyName = null) : base(GetPropertyName(propertyName, propModel), new Attribute[] { }) { string propertyNameToUse = base.Name; _tdConfig = new TypeDescriptorConfig(new Attribute[] { }, typeof(object), true, propertyNameToUse, typeof(BagT), true); _children = GetBasePropertyCollection(); PropertyDescriptor[] propDescriptors = BuildPropDescriptors(propModel); foreach (PropertyDescriptor pDesc in propDescriptors) { _children.Add(pDesc); } }
public PersonVM(PropModelType pm, ViewModelFactoryInterface viewModelFactory, IPropBagMapperService autoMapperService, IPropFactory propFactory, string fullClassName) : base(pm, viewModelFactory, autoMapperService, propFactory, fullClassName) { //PropBagTypeDescriptionProvider<PersonVM> tdp = RegisterTypeDescriptorProvider<PersonVM>(pm); //pm.TypeDescriptionProvider = tdp; //TypeDescriptor.AddProvider(tdp, this); //List<string> pNamesFromOurProvider = tdp.GetPropertyDescriptors(this).Select(x => x.Name).ToList(); //List<string> pNamesFromOurPropModel = pm.PropertyDescriptors.Select(x => x.Name).ToList(); //List<string> pNames = TypeInspectorUtility.GetPropertyNames(this); //System.Diagnostics.Debug.WriteLine("PersonVM is being created with a PropModel."); }