How to execute (Materialize ) a LINQ query call to a ServiceOperation returning just a string value - entity-framework

Up front, the guy on the keyboard here is way overdue on the sleep department. But also kinda desperate.
I have a Data-service [WebGet] method, setup like this.
[WebGet]
public string Finalize(string PayloadObject, string CltUUID, string Comment){..}
It return a simple string, depending on the outcome of the execution inside. This is working fine.
I call it like this
var res = base.ServiceRef.CreateQuery<DBcontext>("Finalize")
.AddQueryOption("PayloadObject", string.Format("'{0}'", builder.ToString()))
...
How do I get this query to materialize?
Usually one uses res.ToList(), ToArray() or .First(). If I do, I get an exception. Using .ToString() does not execute the query, just returns the url.
The query works, if I break in a line after this code and click 'View...' in the debugger, the query is executed, the method runs on the server.
It seems I just don't get it to run, what am I missing?? besides sleep
Thanks for any pointers
Regards, Andreas

I think you cannot use CreateQuery to call operation returning single value. You must use Execute instead. MSDN documentation also mentions that operations returning primitive types cannot use QueryOption which make sense because query option is used to define query for IQueryable. If you need to pass parameters try to use common WCF REST approach to specify template in WebGet and on client side use the URI with parameters correctly included.
You can also try to use SingleResultAttribute on your service operation.

Related

Is there another way way to replace paginate() with take()->get() where query param is present? (Laravel 9)

Usually I use paginate when I want the user to view a list (or a narrowed down list based on filters). Simple example below:
Thing::query()
->orderByDesc('created_at')
->paginate(40);
If I wanted the user to view a short list, like get the five newest models, I would create a separate api with a query like below:
Thing::query()
->orderByDesc('created_at')
->take(5)
->get();
I want to combine the two eloquent queries in such a way that it gets the paginated list by default, but will take 5 if the query param 'take=5' is present. I can do this the following way:
Thing::query()
->orderByDesc('created_at')
->when(
$request->query('take'),
fn ($query, $count) => $query->take((int)$count)->get(),
fn ($query) => $query->paginate(50)
);
The above works but has been described by a colleague as a little confusing, since the 3rd argument to when() is if the first argument is false (documentation) but that isn't immediately apparent when viewing the code. The "confusing" part might be subjective here but I would like to make sure my code is quickly understood by other devs as best as possible.
Does anyone know of a simpler/clearer or just another way to achieve this? In an ideal world the take()->get() would only exist in the when() method and paginate() would exist outside of it, but be overridden by the when() condition if true.
Note: I anticipate some people might say that they should remain as separate api's, however in my opinion the extra logic here is so simple that the gain in reduced code outweighs the gain in "do one thing well".

Can the Post for a ODataContoller in WebAPI 2 take an IEnumerable of a complex object

I want to know if passing a collection to the post of an ODataController is even possible before I waste my time trying to figure out where my problem is.
I've seen variations of this question over the internet, but nothing that has led me to an answer.
I created the Post method on an ODataContoller where I want to pass in a IEnumerable of a complex object. However, when I debug the controller, the parameter is null.
When I take each individual element of the collection and pass it into a Controller's Post (that takes a single object), the object is accepted. So I know the individual objects are being formatted correctly.
Technically this is possible. If you get null as complexObjects in the following call
[HttpPost]
public IHttpActionResult CreateMany([FromBody] IEnumerable<ComplexObject> complexObjects)
{
// ...
}
it's probably due to a format error in the bodies json object. If you have a working single object you can post, you just need to wrap it in brackets ... an array is an array even if it cotains only one element. This assumes, you check your web api actions via postman, fiddler etc., where you can 'compose' the entire request. Alternatively you can use the output of a 'GET all' action (if you have one) as input.
Regarding another aspect, the REST-fulness of list creation, you may find RESTful way to create multiple items in one request interesting
Hope this helps.

Access to Bind Parameters in MyBatis Interceptor

How do I read the bind parameters inside a MyBatis Interceptor? I'm trying to extract those information so I can write them to a log table.
The guide (http://www.mybatis.org/mybatis-3/configuration.html) didn't mention how to get them, and the JavaDoc (http://www.mybatis.org/mybatis-3/es/apidocs/org/apache/ibatis/mapping/BoundSql.html) does not have a single line of comment. I saw an example on SO about constructing a new BoundSql but that isn't what I needed.
I tried to test what contents are stored in BoundSql.getParameterMappings() and BoundSql.getParameterObject(), but it seems to be pretty complex. There's JavaType and JdbcType, and if there's only one parameter the ParameterObject isn't a Map object.
What is the proper way to get the bind parameters from BoundSql?
After going through MyBatis source code (where comment is an endangered species), I found out how MyBatis processes the bind parameters. However, that requires access to the JDBC Statement object, which is simply not available inside an Interceptor.
Then I did some testing and settled on this:
If there is only a single parameter, BindSql.getParameterObject() will give you the parameter itself. By using BindSql.getParameterMappings() and ParameterMapping.getJavaType() I can tell which Java class the parameter is.
If there are more than one parameter, BindSql.getParameterObject() will return an instance of org.apache.ibatis.binding.MapperMethod.ParamMap, which extends HashMap, or it will be an instance of the DTO you used. Using .getProperty() from ParameterMapping as key or as getter name, you can process the bind parameters one by one.
If anyone has a better way to do this, I'm all ears.

How do I use multiple levels in my REST call?

I'm trying to create a REST service with the following signature for a GET call:
//somesite/api/customer/1/invoices
Of course using the correct path, I can get this to work, but all the documentation that I look at for REST tells me how to query .../api/customer or .../api/customer/id, but nothing tells me how to define and get to the level after id.
I suspect it will have something to do with the router code, but could use some instruction on how to get to that next level.
Thanks

Calling PL/pgSQL function ignoring results

I like to call a stored PL/pgSQL function the same way as with PERFORM ignoring the results, but from plain SQL. How can i achive this? I'm currently using SELECT to execute the function, but this prints data on the console what i don't need.
I thought about disabling client output for specific SELECT statements, but i can't find any client settings for this. Maybe there's a better way to do this kind of calls.
There is no such functionality in plain sql. What you can do though, is make the function not return anything.
Here is a dirty hack, i just came up with;
background is, i need to call a function, but specify an UPDATE, not a SELECT ...maybe your background is the same...
So I specified my UPDATE like this:
UPDATE sometable_doesnt_matter
SET some_comlumn=some_comlumn
WHERE (select my_function = 1);
And my function always returns the integer 1.
Ofc i'm going to change the code so that it will also work with a SELECT, but right now, as a hotfix, this works for me.