public IDictionary <string, IMapperRequest> LoadMapperRequests(string basePath, string[] filenames)
        {
            Dictionary <string, IMapperRequest> result = new Dictionary <string, IMapperRequest>();

            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 MapperRequestTemplate mrTemplate)
                        {
                            IMapperRequest mr = new MapperRequest(mrTemplate.SourceType, mrTemplate.DestinationPropModelKey, mrTemplate.ConfigPackageName);

                            result.Add((string)kvp.Key, mr);
                            _mapperRequestCache.Add((string)kvp.Key, mr);
                        }
                    }
                }
            });

            thread.SetApartmentState(ApartmentState.STA);

            thread.Start();
            thread.Join();

            thread = null;

            _mapperRequestCache = result;
            return(result);
        }
Exemple #2
0
 private IMapperRequest FetchMapperRequest(string resourceKey)
 {
     if (CanFindMapperRequestWithJustKey)
     {
         try
         {
             MapperRequestTemplate mr       = _mapperRequestBuilder.GetMapperRequest(resourceKey);
             IMapperRequest        mrCooked = new MapperRequest(mr.SourceType, mr.DestinationPropModelKey, mr.ConfigPackageName);
             return(mrCooked);
         }
         catch (System.Exception e)
         {
             throw new ApplicationException($"MapperRequest for ResourceKey = {resourceKey} was not found.", e);
         }
     }
     else if (HasMrLookupResources)
     {
         throw new InvalidOperationException($"A call providing only a ResourceKey can only be done, " +
                                             $"if this PropModelBuilder was supplied with a MapperRequestBuilder upon construction. " +
                                             $"No class implementing: {nameof(IMapperRequestBuilder)} was provided. " +
                                             $"Please supply a MapperRequest 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 {_mapperRequestBuilder.GetType()} was provided, but it does not have the necessary resources. " +
                                             $"Please supply a ResourceDictionary and ResourceKey or a MapperRequest object.");
     }
 }
Exemple #3
0
 public IMapperRequest GetMapperRequest(ResourceDictionary rd, string resourceKey)
 {
     if (HasMrLookupResources)
     {
         try
         {
             MapperRequestTemplate mr            = _mapperRequestBuilder.GetMapperRequest(rd, resourceKey);
             IMapperRequest        mapperRequest = new MapperRequest(mr.SourceType, mr.DestinationPropModelKey, mr.ConfigPackageName);
             return(mapperRequest);
         }
         catch (System.Exception e)
         {
             throw new ApplicationException("Resource was not found.", e);
         }
     }
     else
     {
         throw new InvalidOperationException($"A call providing a ResourceDictionary and a ResouceKey can only be done, " +
                                             $"if this PropModelBuilder was supplied with a resource upon construction. " +
                                             $"No class implementing: {nameof(IMapperRequestBuilder)} was provided.");
     }
 }
        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);
        }
        public void CanGetMapperForMod3ToDestination_Extra()
        {
            IPropFactory propFactory = _propFactory_V1;
            ViewModelFactoryInterface viewModelFactory = _viewModelFactory;

            PropModel propModel = GetPropModelForModel3Dest(propFactory);

            viewModelFactory.PropModelCache.Add(propModel);

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


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

            //IMapper rawAutoMapper = AutoMapperHelpers.GetAutoMapper<MyModel3, DestinationModel3>
            //    (
            //    mr,
            //    _amp,
            //    out IPropBagMapperKey<MyModel3, DestinationModel3> propBagMapperKey
            //    );

            //IPropBagMapper<MyModel3, DestinationModel3> cookedAutoMapper = new SimplePropBagMapper<MyModel3, DestinationModel3>
            //    (
            //    propBagMapperKey,
            //    rawAutoMapper,
            //    viewModelFactory,
            //    _amp
            //    );

            IPropBagMapper <MyModel3, DestinationModel3> propBagMapper = AutoMapperHelpers.GetAutoMapper <MyModel3, DestinationModel3>
                                                                         (
                mr,
                _amp,
                out IPropBagMapperRequestKey <MyModel3, DestinationModel3> propBagMapperKey
                                                                         );


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

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

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

            Assert.That(propBagMapper, 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
            };


            DestinationModel3 testDest = propBagMapper.MapToDestination(testSource);

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

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


            IPropBagMapper <MyModel3, DestinationModel3> propBagMapper2 = AutoMapperHelpers.GetAutoMapper <MyModel3, DestinationModel3>
                                                                          (
                mr,
                _amp,
                out IPropBagMapperRequestKey <MyModel3, DestinationModel3> propBagMapperKey2
                                                                          );

            //IPropBagMapper<MyModel3, DestinationModel3> cookedAutoMapper2 = new SimplePropBagMapper<MyModel3, DestinationModel3>
            //    (
            //    propBagMapperKey,
            //    rawAutoMapper,
            //    viewModelFactory,
            //    _amp
            //    );

            DestinationModel3 testDest2 = propBagMapper2.MapToDestination(testSource);
        }
        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));
        }