Invoke lambda function using aurora DB - c#

The project I'm working on requires creating an DB aurora (PostgreSQL) that will send request to a lambda function (After every insert to X table) and the lambda function invoke a step function.
Aurora > Lambda > Step Function > Lambda
So the question : How can I invoke (and sent data) to my lambda function and after that invoke the step function in every insert to my X table? (I am open to any solutions)
I'm using asp.net and C# and i want to use EF with the step function to do "Select Data".

So the question : How can I invoke (and sent data) to my lambda function
Follow this documentation to trigger a Lambda function from Aurora. It shows examples of sending payload data from Aurora to the Lambda function.
and after that invoke the step function in every insert to my X table?
Just use the AWS SDK in the Lambda function to call StartExecution on your Step Function. In C#, that would be this method.

Related

Is it possible to send a function to another function with the same return type but different parameters?

I have an AttachmentService.cs class that is currently used to upload images to a database and/or a cloud storage container. When uploading to the cloud I have some retry logic in place that I would like to reuse when being called by two separate methods. I would like to pass in each function as a parameter and then call them from within the method they have been passed into. Here are the two signatures I have currently:
C#
//Example: I want to be able to pass the first function and also the second
//I'm sure it can done with generics but can't seem to get it currently
private AttachmentUploadResult UploadAttachmentToGLS(Func<string, Guid, byte?[], string, AttachmentUploadResult> uploadFunction)
private AttachmentUploadResult UploadAttachmentToGLS(Func<AttachmentEntity, AttachmentUploadResult> uploadFunction)
I would like the above code to only have one signature that could take either
Func<string, Guid, byte?[], string, AttachmentUploadResult> uploadFunction or Func<AttachmentEntity, AttachmentUploadResult> uploadFunction so that my method could look like something like this:
private AttachmentUploadResult UploadAttachmentToGLS(Func<parameters, AttachmentUploadResult>uploadFunction)
{
//Some retry logic
uploadFunction(parameters);
//Some more retry logic
}
Is there a way in which I can acheive the above? I have read into Func and do not believe this is the correct delegate to use.
The signature of your function can be just Func<AttachmentUploadResult> - you don't really care about the parameters in the UploadAttachment method, so you should close over them. The call might then look something like this:
Upload(() => SomeUploadMethod(theActual, parameters, toTheMethod));
It also allows you to hide the details of the implementation from the UploadAttachment method - all you care about is "this is the function to call when I want to do the upload".

SingleOrDefaultAsync: what does it actually mean?

As someone who deals with 'old' C# and now getting up to speed as I move to ASP.NET, I'm wondering if anybody could help me with this simple example. The built in wizard generates code like this:
var product = await _context.Products.SingleOrDefaultAsync(m => m.ProductId == id);
now, what do I get by the lamba expression m => m.ProductId == id? why can't it just be:
var product = await _context.Products.SingleOrDefaultAsync(m.ProductId);
I'm trying to read the documentation but it only adds to my confusion. it documents the query is:
public static Task<TSource> SingleOrDefaultAsync<TSource>(
this IQueryable<TSource> source
)
so... what does it mean that it returns a Task? is that a generic? what is the <TSource> doing there 3 times?
any reference to general reading that would clarify this will be highly appreciated... are these what's called expression trees? is this Linc...? where would i start? etc...
Thanks!
So the documentation for the correct overload of SingleOrDefaultAsync has the signature as:
public static Task<TSource> SingleOrDefaultAsync<TSource>(
this IQueryable<TSource> source,
Expression<Func<TSource, bool>> predicate
)
This is an extension method as indicated by the this preceding the first parameter. An extension method is a static method that is used (syntactically) as if it is a member method.
So, the first parameter source will be _context.Products.
The second parameter is an Expression<Func<>> which means the compiler will convert the lambda expression passed in into an Expression data structure representing the code. A lambda expression is used to allow you to specify any desired conditions for the query being executed e.g. you could have m => m.ProductId == id && m.Active to exclude inactivated products.
So, your second parameter predicate will be m => m.ProductId == id as an Expression tree, which the method can convert to SQL and send to the database.
Finally, the return type of Task<> indicates the method will be executed asynchronously and you will immediately get a Task object which you can use to get the actual result once it is available.
Calling await on the result causes this method to be paused in execution, control to return to the caller (which also gets a Task object), and this method to be resumed once the Task completes.

Is Linq Select asynchronous inside a separate function?

Here's my situation:
I have an C# API Get function, inside which contains a global variable (to be returned in the end) and several functions, as below:
public Dictionary<...,...> Get()
{
getDataFromLinQ(); //inside this, there is a LinQ select
processRawLinQData(); //this processes raw data, and store it into dictResult
return dictResult;
}
If i am writing in this way, it will return no result, as it seems that it doesn't wait the LinQ to finish select first.
However, if I am writing this way:
public Dictionary<...,...> Get()
{
//execute linq directly, but not inside a separate function
IQueryable<table1> linQResult = from t1 in db.table1
where t1...
select t1;
foreach(table1 x in linQResult)
{
//do processing and store in some variables.
}
processRawLinQData();
return dictResult;
}
This will work. But why?
Is LinQ select an Asynchronous method or it behaves differently if i put it inside another function?
P.S.:
1. I prefer method 1 (using function) as the codes are more readable.
2. I notice this scenario on the development/live server. In my local computer, both are working.
Linq is lazy ... if you don't use it, or use a ToList() to force a result, it might not be called if not needed.
So, you have a method call ... it goes in, have some linq, says ..I'll call it if I need, and goes back.
Then other method assumes your query finished and returned something ... but you're in a whole different place now.
I'm not sure I'm conveying my point ... but maybe it'll help you out a bit.

Using Return values in Railway Programming in C#

I have just started using Railway Orientated Programming and it seems to reduce the code when you have a lot of functions to call, based on the previous call failing.
However, I don't know what to do if I need to use a return value from one of the items in an OnSuccess call and used it in the next call?
Example:
Function 2 returns a value ('param1') that I want to include in the call to Function 3:
Function1()
.OnSuccess(() => Function2())
.OnSuccess(() => Function3(param1))
.OnBoth(result => AddtoLog(result));
Is there a way to do this in Railway-Orientated Programming?

RhinoMocks - Fetching parameters of called functions

Using RhinoMocks - can I fetch the parameters of a called function? I mean; can I get some of the unknown parameters from the function call out?
I have a mock, and I expect some function to be called on this. I know one of the parameters, but the other one is unknown as this comes from the class that uses the mock and calls a function on it. More specificly - in this case - the unknown argument is a lambda function. This is a callback function that is supposed to be called when the function is finished executing. As the mock prevents the callback from being called I want to fetch it and call it myself.
So; I want to check that the function was called. I want to make sure some of the arguments were the expected ones. And I want to get out the unknown arguments to do some operations on them afterwards.
Assuming both arguments are ints (for simplicity) I'd like to do something like this:
int unknownInt;
_fakeSomething.AssertWasCalled(factory => factory.Foo(1, out unknownInt));
// then play around with unknownInt..
Can this be done? I see there is an Arg.Out, but couldn't quite make it work..
Note: Updated the question as it seemed to be misleading.
Arg<string>.Matches(arg => you got the argument here...);
UPDATE:
To fetch the second argument made on the first call of the Foo method on _fakeSomething:
string someArg = null;
var args = _fakeSomething.GetArgumentsForCallsMadeOn(
x => x.Foo(0, 0),
x => x.IgnoreArguments()
);
var int = (int)args[0][1];
Not sure if it can be done, but testing like that can result in unreliable tests, as you don't know the actual paramter that was passed in.
If possible, test on explicit data. If you f.eks pass in null instead of a real value your test will likely pass for the wrong reason.
Testing with Arg.Is.Anything should be done carefully and when you truly don't care about the parameter, such as in AssertWasNotCalled.

Categories

Resources