// Typed Get Mapper public IPropBagMapper <TSource, TDestination> GetPropBagMapper <TSource, TDestination> ( IPropBagMapperRequestKey <TSource, TDestination> mapperRequest ) where TDestination : class, IPropBag { IPropBagMapper <TSource, TDestination> result; if (mapperRequest.AutoMapper == null) { throw new InvalidOperationException($"The {nameof(SimplePropBagMapperCache)} was asked to GetPropBagMapper, however the mapperRequest has a null AutoMapper."); } if (_propBagMappers.TryGetValue(mapperRequest, out IPropBagMapperGen genMapper)) { result = (IPropBagMapper <TSource, TDestination>)genMapper; } else { result = mapperRequest.GeneratePropBagMapper(mapperRequest, _viewModelFactory); // Remove the request from the request que if present. _requests.TryRemoveValue(mapperRequest, out IPropBagMapperRequestKeyGen dummyExistingRequest); } return(result); }
public IPropBagMapper <TSource, TDestination> GeneratePropBagMapper ( IPropBagMapperRequestKey <TSource, TDestination> mapperRequestKey, ViewModelFactoryInterface viewModelFactory ) { var result = _propBagMapperBuilder.BuildPropBagMapper(mapperRequestKey, viewModelFactory); return(result); }
public bool Equals(IPropBagMapperRequestKey <TSource, TDestination> other) { if (other == null) { return(false); } if ((this.DestinationTypeDef.Equals(other.DestinationTypeDef)) && this.SourceTypeDef.Equals(other.SourceTypeDef)) { return(true); } else { return(false); } }
private IPropBagMapperGen BuildPropBagMapperGen ( IPropBagMapperRequestKeyGen mapRequestGen, ViewModelFactoryInterface viewModelFactory ) { IPropBagMapperRequestKey <TSource, TDestination> mapRequestTyped = mapRequestGen as IPropBagMapperRequestKey <TSource, TDestination>; if (mapRequestTyped == null) { throw new InvalidOperationException($"{nameof(mapRequestGen)} does not implement the correct typed {nameof(IPropBagMapperRequestKey<TSource, TDestination>)} interface."); } return(BuildPropBagMapper(mapRequestTyped, viewModelFactory)); }
// The Typed Method for PropBagMappers static IPropBagMapperRequestKey <TSource, TDestination> SubmitPropBagMapperRequest <TSource, TDestination> ( PropModelType propModel, string configPackageName, IPropBagMapperService propBagMapperService ) where TDestination : class, IPropBag { IPropBagMapperRequestKey <TSource, TDestination> result = propBagMapperService.SubmitPropBagMapperRequest <TSource, TDestination> ( propModel: propModel, configPackageName: configPackageName, configStarterForThisRequest: null, propFactory: null ); return(result); }
// TODO: The source type should be able to derive from IPropBag if its an emitted type (and therefor 'real'.) // TODO: The destination type should not have to be a IPropBag. /// <summary> /// Make sure that ... /// the source type is not a IPropBag and /// the destination type is a IPropBag. /// </summary> /// <returns></returns> private bool CheckTypes() { IPropBagMapperRequestKey <TSource, TDestination> mapperRequestKey = this; if (mapperRequestKey.MappingConfiguration.RequiresWrappperTypeEmitServices) { if (mapperRequestKey.SourceTypeDef.IsPropBag) { throw new ApplicationException("The first type, TSource, is expected to be a regular, i.e., non-propbag-based type."); } if (!mapperRequestKey.DestinationTypeDef.IsPropBag) { throw new ApplicationException("The second type, TDestination, is expected to be a propbag-based type."); } } return(true); }
public static IPropBagMapper <TSource, TDestination> GetAutoMapper <TSource, TDestination> ( IMapperRequest mapperRequest, IPropBagMapperService propBagMapperService, out IPropBagMapperRequestKey <TSource, TDestination> propBagMapperRequestKey ) where TDestination : class, IPropBag { // TODO: See if we can submit the request earlier; perhaps when the mapper request is created. //Type typeToWrap = mapperRequest.PropModel.TypeToWrap; // Submit the Mapper Request. propBagMapperRequestKey = propBagMapperService.SubmitPropBagMapperRequest <TSource, TDestination> (mapperRequest.PropModel, mapperRequest.ConfigPackageName); // Get the AutoMapper mapping function associated with the mapper request just submitted. IPropBagMapper <TSource, TDestination> propBagMapper = propBagMapperService.GetPropBagMapper <TSource, TDestination>(propBagMapperRequestKey); return(propBagMapper); }
public static IPropBagMapper <TSource, TDestination> GetAutoMapper <TSource, TDestination> ( IMapperRequest mapperRequest, IPropBagMapperService propBagMapperService, out IPropBagMapperRequestKey <TSource, TDestination> propBagMapperRequestKey ) where TDestination : class, IPropBag { // This is where the PropModel is used to define the Mapper // TODO: See if we can submit the request earlier; perhaps when the mapper request is created. //Type typeToWrap = null; // This should only be used if we are overridding the TypeToWrap value stored in the PropModel. // Submit the Mapper Request. propBagMapperRequestKey = propBagMapperService.SubmitPropBagMapperRequest <TSource, TDestination> (mapperRequest.PropModel /*, typeToWrap*/, mapperRequest.ConfigPackageName); // Get the AutoMapper mapping function associated with the mapper request just submitted. IPropBagMapper <TSource, TDestination> result = propBagMapperService.GetPropBagMapper <TSource, TDestination>(propBagMapperRequestKey); return(result); }
// Create a new PropBagMapper public IPropBagMapper <TSource, TDestination> BuildPropBagMapper ( IPropBagMapperRequestKey <TSource, TDestination> mapperRequestKey, ViewModelFactoryInterface viewModelFactory ) { CheckTypeToCreate("source", typeof(TSource), mapperRequestKey.SourceType); CheckTypeToCreate("destination", typeof(TDestination), mapperRequestKey.DestinationType); PropModelType propModel = mapperRequestKey.PropModel; IMapper theMapper = mapperRequestKey.AutoMapper; IPropBagMapper <TSource, TDestination> result = new SimplePropBagMapper <TSource, TDestination> ( propModel, theMapper, viewModelFactory, _propBagMapperService, mapperRequestKey.MappingConfiguration ); return(result); }
// Typed Get PropBag Mapper public IPropBagMapper <TSource, TDestination> GetPropBagMapper <TSource, TDestination> ( IPropBagMapperRequestKey <TSource, TDestination> mapperRequest ) where TDestination : class, IPropBag { // TODO: Consider simply using the 'Gen' version on the PropBagMappers Cache // it not that much less type safe and not that much faster -- however it does add some complexity. //IPropBagMapper<TSource, TDestination> result = GetPropBagMapper(propBagMapperRequestKey); // --- LOOK AT ME ---- // Fetch the raw AutoMapper, if not already retrieved. if (mapperRequest.AutoMapper == null) { if (mapperRequest is PropBagMapperRequestKey <TSource, TDestination> ourImp) { // We are using the concrete type here because we do not want to make // the AutoMapperRequestKey part of the public interface. // TODO: Keep a dictionary of our request keys to IMapper // -- this will allow us to remove the AutoMapper property from the Key altogether. // The BuildPropBagMapper would get a new parameter for the IMapper ourImp.AutoMapper = GetRawAutoMapper <TSource, TDestination>(ourImp.AutoMapperRequestKey); } else { throw new InvalidOperationException("The mapperRequest is not a PropBagMapperRequestKey."); } } IPropBagMapper <TSource, TDestination> result = _propBagMappersCache.GetPropBagMapper <TSource, TDestination>(mapperRequest); return(result); }
public void CanMapObservableCollection( string configPackageName, AutoMapperHelpers ourHelper, IPropFactory propFactory_V1, PropModelCacheInterface propModelCache, IPropBagMapperService amp, PropModelHelpers pmHelpers, int numberOfItemsToLoad ) { ourHelper.StoreAccessCreator.ResetAccessCounter(); Assert.That(ourHelper.StoreAccessCreator.AccessCounter == 0, "The Provider of PropStoreAccessServices did not have its Access Counter reset."); // Setup Mapping between Model1 and Person PropModelType propModel1 = pmHelpers.GetPropModelForModel1Dest(propFactory_V1, propModelCache); ViewModelActivatorInterface viewModelActivator = new SimpleViewModelActivator(); IPropBagMapperService autoMapperService = ourHelper.GetAutoMapperSetup_V1(); ICreateWrapperTypes wrapperTypeCreator = ourHelper.GetWrapperTypeCreator_V1(); ViewModelFactoryInterface viewModelFactory = new SimpleViewModelFactory(propModelCache, viewModelActivator, ourHelper.StoreAccessCreator, amp, wrapperTypeCreator); // TODO: Move this to a separate test. #region Clone Tests // Make sure we can activate (or clone as appropriate) the destination type. DestinationModel1 test = new DestinationModel1(PropBagTypeSafetyMode.AllPropsMustBeRegistered, ourHelper.StoreAccessCreator, ourHelper.PropFactory_V1, "Test"); if (configPackageName == "Emit_Proxy") { DestinationModel1 testCopy = new DestinationModel1(test); } else { DestinationModel1 testCopy = (DestinationModel1)test.Clone(); } DestinationModel1 test2 = new DestinationModel1(propModel1, viewModelFactory, amp, ourHelper.PropFactory_V1, null); if (configPackageName == "Emit_Proxy") { DestinationModel1 test2Copy = new DestinationModel1(test2); } else { DestinationModel1 test2Copy = (DestinationModel1)test2.Clone(); } #endregion Type typeToWrap = typeof(PropBag); IMapperRequest localMr = new MapperRequest(typeof(Person), propModel1, configPackageName); IPropBagMapper <Person, DestinationModel1> propBagMapper = null; IPropBagMapperRequestKey <Person, DestinationModel1> propBagMapperRequestKey = null; IPropBagMapperGen propBagMapperGen = null; IPropBagMapperRequestKeyGen propBagMapperRequestKeyGen = null; if (configPackageName == "Emit_Proxy") { wrapperTypeCreator = ourHelper.GetWrapperTypeCreator_V1(); Type et = wrapperTypeCreator.GetWrapperType(propModel1, typeToWrap); propModel1.NewEmittedType = et; propBagMapperGen = AutoMapperHelpers.GetAutoMapper ( localMr, amp, out propBagMapperRequestKeyGen ); Assert.That(propBagMapperRequestKeyGen, Is.Not.Null, "mapperRequest should be non-null."); Assert.That(propBagMapperGen, Is.Not.Null, "mapper should be non-null"); } else { propBagMapper = AutoMapperHelpers.GetAutoMapper <Person, DestinationModel1> ( localMr, autoMapperService, out propBagMapperRequestKey ); Assert.That(propBagMapperRequestKey, Is.Not.Null, "mapperRequest should be non-null."); Assert.That(propBagMapper, Is.Not.Null, "mapper should be non-null"); } PropModelType propModel5 = pmHelpers.GetPropModelForModel5Dest(propFactory_V1, propModelCache); string fullClassName = null; // Don't override the value from the PropModel. _testMainVM = new DestinationModel5(propModel5, viewModelFactory, amp, ourHelper.PropFactory_V1, fullClassName); Business b = new Business(); _testMainVM.SetIt(b, "Business"); // THIS IS A SET ACESSS OPERATION. b = _testMainVM.GetIt <Business>("Business"); // TODO: try using IEnumerable<Person> instead. List <Person> unMappedPeople = b.Get(1000); //IEnumerable<DestinationModel1> mappedPeople; IEnumerable <object> mappedPeople; if (configPackageName == "Emit_Proxy") { mappedPeople = propBagMapperGen.MapToDestination(unMappedPeople); } else { mappedPeople = propBagMapper.MapToDestination(unMappedPeople); } _readyForTheView = new ObservableCollection <object>(mappedPeople); // Each time a item is mapped, it is first created. (5 sets during consruction, and another 5 for the actual mapping.) int totalNumberOfGets = ourHelper.StoreAccessCreator.AccessCounter; if (configPackageName == "Extra_Members") { Assert.That(totalNumberOfGets == 1, $"Total # of SetIt access operations is wrong: it should be {1}, but instead it is {totalNumberOfGets}."); } else { Assert.That(totalNumberOfGets == 1 + (numberOfItemsToLoad * 5), $"Total # of SetIt access operations is wrong: it should be {1 + numberOfItemsToLoad * 5}, but instead it is {totalNumberOfGets}."); } int currentNumRootPropBags = ourHelper.StoreAccessCreator.NumberOfRootPropBagsInPlay; int totalRootPropBagsCreated = ourHelper.StoreAccessCreator.TotalNumberOfAccessServicesCreated; PropBag sampleItem = (PropBag)_readyForTheView[0]; int howManyDoSetDelegatesGotCreated = sampleItem.NumOfDoSetDelegatesInCache; int howManyCreateFromString = sampleItem.CreatePropFromStringCacheCount; //int howManyCreateWithNoVal = sampleItem.CreatePropWithNoValCacheCount; //Thread.Sleep(new TimeSpan(0, 0, 1)); }