コード例 #1
0
        public override DistributeDataObject Action(ProcessDataObject data, CancellationToken cancellationToken)
        {
            //Pretend to save the data to an archive
            _repository.SaveData(data);

            //Do some operation on the data to create a result
            var result = new DistributeDataObject
            {
                DataValue1 = data.DataValue,
                DataValue2 = data.DataValue / 2
            };

            //Simulate some processing time
            Random rng = new Random();

            Thread.Sleep(rng.Next(1, 100));

            //Send result to Distributor
            return(result);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: BWeesy/Smoulder
        static void Main(string[] args)
        {
            var fakeRepository  = new ExampleRepository();
            var smoulderFactory = new SmoulderFactory();

            //Object oriented methodology
            var firstSmoulder = smoulderFactory.Build(new ExampleLoader(), new ExampleProcessor(fakeRepository), new ExampleDistributor(), 50000);

            //Run Demo with first Smoulder
            RunDemo(firstSmoulder);


            //Functional methodology
            //Create second smoulder by passing methods to factory, thus removing the requirement to instantiate any worker units
            var secondSmoulder = smoulderFactory.Build <ProcessDataObject, DistributeDataObject>(null, null, null, 50000)
                                 .SetLoaderAction(token =>
            {
                var rng  = new Random();
                var data = new ProcessDataObject {
                    DataValue = rng.Next()
                };
                Thread.Sleep(rng.Next(1, 50));
                return(data);
            })
                                 .SetProcessorAction((data, token) =>
            {
                fakeRepository.SaveData(data);
                var result = new DistributeDataObject
                {
                    DataValue1 = data.DataValue,
                    DataValue2 = data.DataValue / 2
                };

                Random rng = new Random();
                Thread.Sleep(rng.Next(1, 100));

                return(result);
            })
                                 .SetDistributorAction((data, token) =>
            {
                Random rng = new Random();
                Thread.Sleep(rng.Next(1, 25));
            })
                                 .SetLoaderOnError(e => throw new Exception("Throw loader exception with inner exception attached", e))
                                 .SetProcessorOnError(e => throw new Exception("Throw processor exception with inner exception attached", e))
                                 .SetDistributorOnError(e => throw new Exception("Throw distributor exception with inner exception attached", e));

            //Run Demo with second Smoulder
            RunDemo(secondSmoulder);


            //Object oriented methodology with some functional stuff to save having to create oversimple worker units
            //Create third smoulder by mixing the two previous methods
            var thirdSmoulder = smoulderFactory.Build(new ExampleLoader(), new ExampleProcessor(fakeRepository), null, 50000)
                                .SetDistributorAction((data, token) =>
            {
                Random rng = new Random();
                Thread.Sleep(rng.Next(1, 25));
            });

            //Run Demo with third Smoulder
            RunDemo(thirdSmoulder);

            Console.WriteLine("Press enter to finish the Smoulder demonstration process");
            Console.ReadLine();
        }