How to get distinct paged list from SharpRepository FindAll method with selector? - sharp-repository

I was trying to get a field from an entitytype which has many dups using the sharprepository FindAll method with paging. I don't know how to supply the Distinct parameter or if it is even possible?
repo.FindAll(spec, c => c.Field, new PagingOptions<EntityType>(1, 20, "Field", false);

At this point it's not something you can do, though it makes sense that we should add it to the list of things to add. If you wouldn't mind adding that as an issue to GitHub I'm sure we can add it pretty easily. We would just need to figure out the best way to include it.

Related

OData WebAPI: How to hardcode OrderBy in Function?

I am using OData WebAPI (Microsoft.AspNet.OData). for some reasons, I need hardcore OrderBy in a function. for example and return like the following,
return DbContext.Companies.OrderByDescending(a => a.CompanyName);
When I executed the function, orderby hardcoded did not work, I also checked sqlprofile, it seem framework did something and remove it. If I use $orderby queryoption, it is no problem.
Is there anyone knows how to solve it? thks in advance.
One workaround to implement is by making CompanyName a part of the composite key and specifying the Column attribute. I doubt that it will work without making the property part of the key and just using the Column attribute but you can quickly try that out.
One other workaround is to insert an orderby clause in ODataQueryOptions in your controller.

Can a Cerberus schema have an arbitrary name for the base dict?

I need to validate Python dicts that will have arbitrary names. When I attempt to validate them using Cerberus, I get unknown field. Is there a way of allowing for arbitrary dict names?
I was thinking that keysrules might work, but it appears to only work on items within the base dict.
{'account_created': {'category': 'Accounts',
'conversion_event': True,
'description': 'A new account is created'}
}
I would like to be able to use an arbitrary name where account_created is in this dict.
Assuming you don't need to validate that base key, I just attempted to answer a question similar to this on the Cerberus GitHub. My suggestion was to maybe use a dynamically formed schema. You could follow the GitHub issue thread and see if anyone there comes up with a better answer.

Something akin to "Sparse Fieldsets" in .NET

I'm trying to find the vocabulary to describe what I want and if it exists.
I have a table that shows a few data points from large objects. Loading the entire objects just for the table is very slow. Is there a way to only pass to the front the few properties I want without having to define a new object?
I found something called Sparse Fieldsets in JSON API, and I'm wondering if something like this exists for .NET under another name.
Update:
Talking to another coder, I realize it probably makes more sense to implement something like this between the backend and the database and make a specific call for this table. I still need to work out if I need to create a new object to support this. I think it'd still be faster if I just kept the same object but nulled out all of the connecting objects that I don't need for the table. But maybe that's considered bad practice? Also we're using Entity Framework for what it's worth.
Update 2:
I just created a new query without all of the .Include() and works well enough for what I need:
_dataContext.ApplePie
.Include(f => f.Apples).ThenInclude(f => f.Apple)
.Include(f => f.Sugars).ThenInclude(f => f.MolecularStructure)
.Include(f => f.Recipe)
Maybe you are looking for Anonymous Types?
For example, if you had a typed object with three properties, but you only wanted to operate on two:
var threePropThing = new ThreePropertyThing { Id = 1, Message = "test", ExtraProperty = "ex" };
var myAnonThing = new { Id = threePropThing.Id, Message = threePropThing.Message };
Best practice would be to not pass this anonymous object around. But, if you really needed to, you could return it as type object.
Typically, when passing data around in c#, you want to have it typed.
C# is a strongly-typed language and I would say that it is unusual for C# to support scenarios, when object definition (properties) are not known in advance, like in JSON API "fields" parameter case. Implementing this would imply using reflection to filter the properties, which is usually slow and error-prone.
When implementing C# web-services, people usually create one DTO response model per each request.
If your table has fixed set of fields, I would personally recommend to create a DTO class containing only the fields which are required for your table, and then create a method which returns this response for your specific request. While it doesn't align with "without having to define a new object" in the question, it makes the intention clear and makes it really easier to maintain the API in future.
You might want to use libraries like AutoMapper to save time and avoid duplicated code of copying the values from data model to DTO, if you have many such methods.

How to strip/ignore unused attributes when saving a model object?

I am sending angular model objects to bookshelf to save, but it may carry extraneous attributes that aren't in the database. When I save, bookshelf will try to save all attributes and say it can't find these extra attributes.
What is the recommended way to handle this? I'm sure I can set out an array of whitelisted attributes, and strip the object manually, but is there another way?
IE, is there a built in way to ignore unused attributes? Or is there a way to query the DB to get the array of columns, then use that to strip my object?
You may use parse() in addition to an array of permitted attributes, like Ghost team did.
Mode = bookshelf.Model.extend({
permittedAttributes: [ 'field1', 'field2', 'field3' ],
parse: function (attrs) {
return _.pick(attrs, this.permittedAttributes)
}
})
If you define parse() in a base model, all models that extend it will behave the same way
Tooting my own horn here, but I encountered this problem so many times that I created a plugin for bookshelf. Didn't want to have to manually define permitted attributes every single time.
https://www.npmjs.com/package/bookshelf-strip-save

Why does Flex's ArrayCollection's Contain method look at memory reference?

When using .contains() on an ArrayCollection in Flex, it will always look at the memory reference. It does not appear to look at an .equals() method or .toString() method or anything overridable. Instead, I need to loop through the ArrayCollection every time and check each individual item until I find what I'm looking for.
Does anyone know why Flex/ActionScript was made this way? Why not provide a way from people to use the contains() method the way they want?
Couldn't you just extend ArrayCollection and override the contains() method? Alternatively you can paste the source for ArrayCollection into an "mx/collections" package in your project and modify the source; this "monkey-patching technique" will override the behavior throughout your entire project. However I would be extremely cautious about changing ArrayCollection in that manner: since it's used all over the place in the Flex APIs there is a good chance you'll start breaking other components in the framework.
The contains() method searches by reference, correct (I believe even for primitives), so if you're trying to find a string or an int in an ArrayCollection, you'll have to do the searching yourself, by some variation of looping or searching. I don't think any of us could tell you why there isn't, say, an optional parameter on that method indicating whether to search by ref or by val, though; so it goes, as they say.
But I'd definitely warn you off monkey-patching the framework code -- that's just asking for trouble. :)
Well, it seems like the ArrayCollection doesn't actually look directly at memory, but only as a last resort. It will attempt to find a Unique ID (UID) for the object. If the UID doesn't exist, it will create one for it using the UIDUtil.as.
You can get around this whole default UID stuff by having your object implement the IUID interface and providing your own UID for the object. The ArrayCollection will look at the UID you provide it.
I would suggest a simple:
in_array($haystack, $arrayCollection->toArray());

Resources