using AutoMapper.IMappingOperationOptions in AutoMapper - automapper

How to use the function AutoMapper.Mapper.Map(TSource source, Action opts) in AutoMapper ?
I think the 2nd param (Action opts) is there for declaring mapping options if needed but i can't find any API document for this function.
I would like to map 2 objects without having to creatmap first, if the 2 objects have exactly the same fields then the following command works: AutoMapper.Mapper.Map(TSource source)
Please help.

Mapper.DynamicMap<Model, ModelDto>() should do what you want.
I just tested it out and copied over the values from the source to the destination without requiring any CreateMap calls.

For anyone else coming across this older post, Automapper's docs have improved.
You can see examples of using the 2nd parameter for the Map function here:
https://docs.automapper.org/en/latest/Before-and-after-map-actions.html

Related

Azure Functions .Net 5: Is it possible to implement POCO binding somehow?

When moving my functions to .net5 I faced the fact that POCO binding that worked fine with 3.1 is not applicable with .net 5 anymore for some reason. They say it will be implemented at some point maybe, but for the certain reasons I need it now. Tried to find some hacky way to implement this, but failed. The best thought I had was to implement explicit operator in my DTO object which will cast HttpRequestData to it's type, but the problem is that HttpRequestData is an abstract type, and it's concrete implementation type is internal. After that I tried to cast the input parameter to HttpRequestData in middleware with reflection, but parameters are stored in IReadOnlyDictionary which is immutable. So I ran out of ideas now. Maybe someone found workaround to this and can kindly share, would be much appreciated.
I suppose you're using the "dotnet-isolated" mode (only way to run on .NET 5).
I'm trying to find a more elegant solution to this as well.
Meanwhile, what I did was to deserialize the data myself, inside the function.
var body = await new StreamReader(request.Body).ReadToEndAsync();
var myobject = JsonSerializer.Deserialize<MyPocoClass>(json);
I would really prefer if the runtime did it by itself, but I couln't find a way yet. I read somewhere that it is possible to create our own binding code, but I haven't tried it.
I noticed that I could bind to individual properties of the json payload, but not to an object...
I hope this arrives in Azure Functions v4 + .NET6, since it is right around the corner.

How to Include Other Models in Loopback?

I've read the document here: https://docs.strongloop.com/display/public/LB/Include+filter
But they are not clear! For example, they give the example:
User.find({include: 'posts'}, function() { ... });
Where should this code fit into? Is it in the common/models/user.js? Then what inside the function? what is the returned?
Of course I'm getting frustrated with their documents.
Please suggest. Thanks.
According to documentation you can use an include filter with find(), findOne() and findById(). I have attached links to respective sections of API reference.
To use these methods you need your PersistedModel object (e.g. User). Like you mentioned, you can get it in common/models/model-name.js (if you store your models there). You can also easily retrieve it from your LoopBackApplication object, which you can get in several ways. Bottom line is ... you can basically use these methods, wherever you need.
function() { ... } argument signalizes a callback. It lets you define a function which is going to be used on a returned instance(s). What is inside that function depends on your needs.

How to iterate over solutions using the Alloy API?

I'm using Alloy using its API as explained in Alloy - Generate .xml instance from .als.
I want to iterate over all solutions.
How to do this?
In order to iterate over all the satisfiable solutions, you can simply loop over calls of the next() method on your A4Solution object, until the solution obtained is unsatisfiable (check with the satisfiable() method).
You will have something like :
A4Solution mySolution = TranslateAlloyToKodkod.execute_command(null, model.getAllReachableSigs(), cmd, new A4Options());
while(mySolution.satisfiable()){
mySolution=mySolution.next();
//...
}

JsConfig<MyClass>.ExcludePropertyNames example, not working for me

Trying to exclude properties from a model from being included during serialization.
I am using the following syntax:
JsConfig<MyTestClass>.ExcludePropertyNames = new[] { "ShortDescription" };
Just after that I have the following:
return (from o in __someProvider.GetAll() select (new
{
o.Name,
o.ShortDescription
o.InsertDate
}).TranslateTo<MyTestClass>()).ToList()
However once result is returned from the method, it still contains "ShortDescription" field in the Json. Am I doing something wrong?
JsConfig<T>.ExcludePropertyNames appears to be checked only once for each type, in a static constructor for TypeConfig<T>. Thus, if you are configuring ExcludePropertyNames in your service class, just before returning your response, it might be too late -- the TypeConfig properties may already be set up and cached for MyTestClass. I was able to reproduce this.
A more reliable alternative is to move all of your JsConfig<T> configuration to your AppHost setup code.
If you really do need to do this in your service class, e.g. if you are only conditionally excluding property names, then an alternative approach would be to ensure that JsConfig.IncludeNullValues is false (I believe it is by default) and in your service code set ShortDescription to null when appropriate.

Remove Single Metaclass Method

I've been starting to learn Groovy and am currently looking at the metaclass functionality. I have seen the examples of adding a new method, and removing all methods, but nothing about removing a single method. For example:
String.metaClass.foo = {delegate.toUpperCase()}
String.metaClass.bar = {delegate.toLowerCase()}
with the obvious side-effects. Now I have seen that you can say
String.metaClass = null
To remove all of the methods. I would expect one could say something along the lines of
String.metaClass.foo = null
to remove String.foo(), but have String.bar() remain, however this statement does not seem to have any effect. Is there a way to say method foo() should no longer be defined, without effecting bar() or any other added methods?
If you search this webpage for "remove method" it says that you should be able to remove a method using the exact syntax you've proposed above. But I tested it, and you're right, it doesn't seem to work.
A workaround is to assign a closure that throws MissingMethodException, which is what happens by default when you call a method that doesn't exist, e.g.
// Add method
String.metaClass.foo = {delegate.toUpperCase()}
// Remove method
def removeMethod = {throw new MissingMethodException()}
String.metaClass.foo = removeMethod
Admittedly, this is not the most pleasing solution.
As a followup, I posted a bug report here:
https://issues.apache.org/jira/browse/GROOVY-4189
And the documentation has been changed now
See the bug report for the reason this was never implemented
Don's answer is the best way around this

Resources