AWS Amazon.S3.Model.PutObjectRequest is merely a 3rd-party AWS Data Transfer Object (DTO) / Plain Old C# Object (POCO) type that can be used to build a request that can be used to send requests to an S3 Bucket.
(reference
The keyContentDictionary Dictionary will contain:
- a key that will correspond to an s3 Bucket entry's
objectKey - a value that will correspond to the s3 Bucket entry's content data.
IDictionary<string, string> keyContentDictionary = ……blah blah blah……
Since PutObjectRequest is merely a DTO/POCO, it would be easy to use the PutObjectRequest with C# Action Delegates. It's better to use Action Delegates because we can use Anonymous C# Lambda functions to pass arguments. Using Anonymous C# Lambda functions to pass arguments gives us a great amount of flexibility for the future. This in turn can reduce the number of methods in the interfaces. Essentially, it reduces the chances of having too many different Interface methods , and overloaded Interface methods with so many different method signatures.
Therefore, here is a collection of PutObjectRequest parameterized Action Delegates:
IEnumerable<Action<PutObjectRequest>> actionDelegatesCollection = new
> List<Action<PutObjectRequest>> ();
Within the for-loop code below, there are 2 important Unconventional( i.e., hacky) "circuitous" programming techniques that needs to be noted:
there is some "circuitous" programming code that involves the retrieval of the Key and Value pairing. To elaborate the key and value within the dictionary are copied over to
aKeyandaValuevariables, respectively, which are separate dedicated variables. If the C# Lambda Anonymous function was implemented in such a way that involved retrieving directly from the dictionary's entries via the loop iteration index (i.e, 'i' in case of the code below ) like so then it will throw an error at runtime that complains about index being out of bounds:/* more code */(r) => { r.Key = keyContentDictionary.Keys.ElementAt(i); r.ContentBody = keyContentDictionary.Values.ElementAt(i); }; // more code....One had to resort to an Unconventional "circuitous" programming that involves copying over to the
aKeyandaValuevariables, and then using said variables in the C# Lambda Anonymous function.The
aKeyandaValuevariables need to be Redeclared in every loop iteration. If I did not redeclare the aforementioned variables in every loop iteration then all theactionDelegatesCollectionwould all only contain the very last key and content values of the dictionary (In other words, the very last key and content values would just repeatedly show up in each of the elements in theactionDelegatesCollection)for (int i = 0; i < keyContentDictionary.Count; i++) { string aKey = keyContentDictionary.Keys.ElementAt(i); string aValue = keyContentDictionary.Values.ElementAt(i); actionDelegatesCollection.Add((r) => { r.Key = aKey; r.ContentBody = aValue; }); }
Could someone please provide an elaborate theoretical explanation for why:
Why is
IndexOutOfRangeExceptionbeing thrown when using a loop's iteration index to retrieve elements in theDictionaryfrom within the C# Lambda Anonymous function?Why do the
aKeyandaValuevariables need to be re-declared in every loop iteration?
forloop) is adding of value to be able to understand the second part (theforloop and concrete questions).