Exemplo n.º 1
0
        public IPropBagMapperGen GetPropBagMapper(IPropBagMapperRequestKeyGen mapperRequest)
        {
            if (mapperRequest.AutoMapper == null)
            {
                throw new InvalidOperationException($"The {nameof(SimplePropBagMapperCache)} was asked to GetPropBagMapper, however the mapperRequest has a null AutoMapper.");
            }

            IPropBagMapperRequestKeyGen save = mapperRequest;

            if (_propBagMappers.TryGetValue(mapperRequest, out IPropBagMapperGen result))
            {
                return(result);
            }
            else
            {
                //IPropBagMapperKeyGen newOrExistingRequest = _Requests.GetOrAdd(mapperRequest);
                //result = _propBagMappers.GetOrAdd(newOrExistingRequest);

                // Remove the request from the request que if present.
                _requests.TryRemoveValue(mapperRequest, out IPropBagMapperRequestKeyGen dummyExistingRequest);

                result = _propBagMappers.GetOrAdd(mapperRequest);
                CheckForChanges(save, mapperRequest, "GetOrAdd PropBagMapper.");
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Queues a request to create and IMapper and a new PropBagMapper to be created from the IMapper.
        /// </summary>
        /// <param name="mapperRequest"></param>
        /// <returns>The 'true' key present in the dictionary. This will be the key given if no previous matching registration has occured.</returns>
        public IPropBagMapperRequestKeyGen RegisterPropBagMapperRequest(IPropBagMapperRequestKeyGen mapperRequest)
        {
            IPropBagMapperRequestKeyGen result;

            if (_propBagMappers.ContainsKey(mapperRequest))
            {
                //ICollection<IPropBagMapperKeyGen> keys = _propBagMappers.Keys;

                //// TODO: This is relatively resource intensive, perhaps there's a better way.
                //IPropBagMapperKeyGen existingRequest = keys.FirstOrDefault
                //    (
                //    x => x.DestinationTypeGenDef.Equals(mapperRequest.DestinationTypeGenDef)
                //    );

                //result = existingRequest;
                result = mapperRequest;
            }
            else
            {
                IPropBagMapperRequestKeyGen newOrExistingRequest = _requests.GetOrAdd(mapperRequest);
                result = newOrExistingRequest;
            }

            return(result);
        }
Exemplo n.º 3
0
        // 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);
        }
Exemplo n.º 4
0
        //public static IMapper GetAutoMapper<TSource, TDestination>
        //    (
        //    IMapperRequest mapperRequest,
        //    //ViewModelFactoryInterface viewModelFactory,
        //    IPropBagMapperService propBagMapperService,
        //    out IAutoMapperRequestKey<TSource, TDestination> rawAutoMapperRequest
        //    )
        //    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 = mapperRequest.PropModel.TypeToWrap;

        //    // Submit the Mapper Request.
        //    rawAutoMapperRequest = propBagMapperService.SubmitRawAutoMapperRequest<TSource, TDestination>
        //        (mapperRequest.PropModel/*, viewModelFactory*/, typeToWrap, mapperRequest.ConfigPackageName);

        //    // Get the AutoMapper mapping function associated with the mapper request just submitted.
        //    //IPropBagMapperGen genMapper = _propBagMapperService.GetMapper(mapperKey);

        //    IMapper rawAutoMapper = propBagMapperService.GetRawAutoMapper(rawAutoMapperRequest);
        //    return rawAutoMapper;
        //}

        public static IPropBagMapperGen GetAutoMapper
        (
            IMapperRequest mapperRequest,
            IPropBagMapperService propBagMapperService,
            out IPropBagMapperRequestKeyGen propBagMapperRequestKeyGen
        )
        {
            // Submit the Mapper Request. TODO: See if we can submit the request earlier; perhaps when the mapper request is created.
            propBagMapperRequestKeyGen = propBagMapperService.SubmitPropBagMapperRequest(mapperRequest.PropModel, mapperRequest.SourceType, mapperRequest.ConfigPackageName);

            // Get the AutoMapper mapping function associated with the mapper request just submitted.
            IPropBagMapperGen mapperGen = propBagMapperService.GetPropBagMapper(propBagMapperRequestKeyGen);

            return(mapperGen);
        }
Exemplo n.º 5
0
        private void CheckForChanges(IPropBagMapperRequestKeyGen original, IPropBagMapperRequestKeyGen current, string operationName)
        {
            if (!ReferenceEquals(original, current))
            {
                System.Diagnostics.Debug.WriteLine($"The mapRequest object was replaced by method call: {operationName}.");
            }

            if (original != current)
            {
                System.Diagnostics.Debug.WriteLine($"The mapRequest was updated by method call: {operationName}.");
            }
            else
            {
                System.Diagnostics.Debug.Assert(1 == 1, "Remove this.");
            }
        }
Exemplo n.º 6
0
        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));
        }
Exemplo n.º 7
0
        public void CanRegisterMod3ToDestinationMapper_Proxy()
        {
            IPropFactory propFactory = _propFactory_V1;
            ViewModelFactoryInterface viewModelFactory = _viewModelFactory;

            PropModel propModel         = GetPropModelForModel3Dest(propFactory);
            Type      typeToWrap        = typeof(PropBag);
            string    configPackageName = "Emit_Proxy";

            //IMapperRequest mr = new MapperRequest(typeof(MyModel3), propModel, configPackageName);

            IPropBagMapperRequestKeyGen mapperRequest =
                _amp.SubmitPropBagMapperRequest(propModel, typeof(MyModel3), configPackageName);


            Assert.That(mapperRequest, Is.Not.Null, "mapperRequest should be non-null.");
        }
Exemplo n.º 8
0
        // --- LOOK AT ME ----
        public IPropBagMapperGen GetPropBagMapper(IPropBagMapperRequestKeyGen mapperRequest)
        {
            // Fetch the raw AutoMapper, if not already retrieved.
            if (mapperRequest.AutoMapper == null)
            {
                if (mapperRequest is PropBagMapperRequestKeyGen 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 = GetRawAutoMapperGen(ourImp.AutoMapperRequestKeyGen);
                }
                else
                {
                    throw new InvalidOperationException("The mapperRequest is not a PropBagMapperRequestKeyGen.");
                }
            }

            return(_propBagMappersCache.GetPropBagMapper(mapperRequest));
        }
Exemplo n.º 9
0
        public void CanGetMapperForMod3ToDestination_Proxy()
        {
            IPropFactory propFactory = _propFactory_V1;
            ViewModelFactoryInterface viewModelFactory = _viewModelFactory;

            PropModel propModel = GetPropModelForModel3Dest(propFactory);

            viewModelFactory.PropModelCache.Add(propModel);

            Type   typeToWrap        = typeof(PropBag);
            string configPackageName = "Emit_Proxy";

            IMapperRequest mapperRequest = new MapperRequest(typeof(MyModel3), propModel, configPackageName);

            Type et = _wrapperTypeCreator_V1.GetWrapperType(propModel, typeToWrap);

            propModel.NewEmittedType = et;

            IPropBagMapperRequestKeyGen mapperKey = _amp.SubmitPropBagMapperRequest(mapperRequest.PropModel,
                                                                                    mapperRequest.SourceType, mapperRequest.ConfigPackageName);

            // Get the AutoMapper mapping function associated with the mapper request already submitted.
            IPropBagMapperGen genMapper = _amp.GetPropBagMapper(mapperKey);

            //IMapper genMapper = _amp.GetRawAutoMapper(mapperKey);

            Assert.That(mapperKey, Is.Not.Null, "mapperRequest should be non-null.");

            //IPropBagMapper<MyModel3, DestinationModel3> mapper = _amp.GetMapper<MyModel3, DestinationModel3>(mapperRequest);

            Assert.That(genMapper, Is.Not.Null, "mapper should be non-null");

            MyModel4 dp = new MyModel4
            {
                MyString = "This is a good thing."
            };

            MyModel3 testSource = new MyModel3
            {
                Amount    = 11,
                Size      = 22.22,
                ProductId = Guid.Empty,
                Deep      = dp
            };

            var testDest = genMapper.MapToDestination(testSource);

            //IPropBagMapperKey<MyModel3, DestinationModel3> mapperRequest2 =
            //    _amp.SubmitMapperRequest<MyModel3, DestinationModel3>
            //    (
            //        propModel: propModel,
            //        typeToWrap: typeToWrap,
            //        configPackageName: configPackageName
            //    );

            //IPropBagMapper<MyModel3, DestinationModel3> mapper2 = _amp.GetMapper<MyModel3, DestinationModel3>(mapperRequest2);

            IPropBagMapperRequestKeyGen mapperKey2 = _amp.SubmitPropBagMapperRequest(mapperRequest.PropModel,
                                                                                     mapperRequest.SourceType, mapperRequest.ConfigPackageName);

            // Get the AutoMapper mapping function associated with the mapper request already submitted.
            IPropBagMapperGen genMapper2 = _amp.GetPropBagMapper(mapperKey2);


            var testDest2 = genMapper2.MapToDestination(testSource);
        }
Exemplo n.º 10
0
        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));
        }
Exemplo n.º 11
0
        private IPropBagMapperGen MapperFactory(IPropBagMapperRequestKeyGen key)
        {
            IPropBagMapperGen result = key.CreatePropBagMapper(_viewModelFactory);

            return(result);
        }
Exemplo n.º 12
0
 private IPropBagMapperRequestKeyGen RequestFactory(IPropBagMapperRequestKeyGen key)
 {
     return(key);
 }
Exemplo n.º 13
0
 public IPropBagMapperRequestKeyGen RegisterPropBagMapperRequest(IPropBagMapperRequestKeyGen propBagMapperRequestKey)
 {
     return(_propBagMappersCache.RegisterPropBagMapperRequest(propBagMapperRequestKey));
 }