Is it possible to use an id which isn't an integer using taste?
My model may have preferences for remote products. To differentiate these we use a string identifier which is "locationId:itemId".
I can't see support for this however.
Is there any other way to get around this?
Yes, though it's going to slow things down. Look at IDMigrator and implementations and usages in the framework. This is how you can get some help in translating to/from integer IDs automatically.
There is no support for this in the distributed version.
Yes. Look at the answers to this question. Basically you can extend FileDataModel, overriding readItemIDFromString(String stringID), and perhaps use an IDMigrator to do the conversion.
userId and itemId can be string, so this is the CustomFileDataModel which will convert your string into integer and will keep the map (String,Id) in memory; after recommendations you can get string from id.
Related
I'm testing Orleans out.
I want to store grain-ids for later use. Is there a way to make the ids typesafe? I want to make it impossible to mix ids of differet types of grains.
Or maybe I should store grainreferences instead? Can grainreferences be typesafe?
One of the overrides of GrainFactory.GetGrain have this signature:
IGrain GetGrain(Type grainInterfaceType, long grainPrimaryKey, string keyExtension);
So you can save the type next to the key, and be able to recreate it later in runtime.
I need to figure out what is the standard/best practice for displaying localized UI text in an iOS from text that is received from a remote source, such as API driven, server-provided text. iOS Localization of static text is not a problem and we already have a system for managing that.
It seems to me the right and best thing would be to have the translated text sent by the server and displayed as received on the app, but there are some concerns about doing it this way, such as the app's locale/current language being possibly different than the current settings on the server. This presents the possibility that there may be a mismatch in dialect or language between what the phone is currently set to and what the server is set to. Also, we are considering how having the server do this breaks any of the S.O.L.I.D. design principles.
So, it comes to two possibilities. Either the server provides the translated text or the app does.
We could possibly provide a parameter to the server that would indicate which locale the device is set to (ie "en-us"). I imagine that this would be sent as an HTTP request header parameter.
It has also been suggested that the mobile app provide similar functionality for itself. This would involve maintaining a data store of some kind (tbd) so that display strings would be given to a facade and translated strings would be returned. ie: func translate(uiText: String) -> String. Internally, it could determine the user's locale and use that as part of the query needed to select the correct translated text. Again, the implementation of this would have to be decided, but that's not the problem I'm hoping to find a solution for.
To sum it up, what I really need is to know what is standard practice for translating server provided text that is to be displayed to the user and are there any frameworks out there designed to assist with this requirement (assuming the solution should exist in the mobile app)?
It seems to me that this is functionality best provided by the server. Also note that we have an Android app that would require similar enhancement.
I think I found a solution to this question but I do not want to mark it as the accepted answer unless I can get some feedback reinforcing what I have learned.
Most of the research I've done has lead me to what works for Web development, which is similar because it's still development but a much different creature than mobile. Mobile is compiled, which makes the advantages and disadvantages of where to do the presentation logic balance a little differently.
Some sources said it belongs on the server where it came from. Some sources said the client should do it. (I'll post references at the end.)
I opened up a discussion amongst my colleagues on the topic and this is where I found what I think will be our best solution. (Comments and criticisms are certainly welcome.)
I'll refer to data provided by the server through the API here forth as just "server".
For every string that the server provides that is meant to be displayed in the UI, I need to have some way to define these strings statically in the iOS app. Ideally I think an enum per display string would work. I can wrap every string with NSLocalizedString in the definition of the enum. Make the enum a String type, and initialize an instance of the enum from the string received from the server. The enum will return the NSLocalizedString. The iOS Localization system should export all of those localized strings, I will receive translations, and they will then exist in the various respective .strings files.
EG:
enum Emotion: String {
case happy = "Happy"
case sad = "Sad"
case angry = "Angry"
case joy = "Joy"
case amused = "Amused"
case bored = "Bored"
case undefined = ""
func translation() -> String {
switch self {
case .happy:
return NSLocalizedString("Happy", comment: "Happy")
case .sad:
return NSLocalizedString("Sad", comment: "Sad")
case .angry:
return NSLocalizedString("Angry", comment: "Angry")
case .joy:
return NSLocalizedString("Joy", comment: "Joy")
case .amused:
return NSLocalizedString("Amused", comment: "Amused")
case .bored:
return NSLocalizedString("Bored", comment: "Bored")
default:
return "--"
}
}
}
Implementation:
let thisCameFromTheServer: String? = "Happy"
let emo = Emotion(rawValue: thisCameFromTheServer ?? "") ?? .undefined
References
https://softwareengineering.stackexchange.com/questions/313726/where-should-i-do-localization-server-side-or-client-side
https://softwareengineering.stackexchange.com/questions/373395/api-internationalization
https://www.appliedis.com/localization-of-xcode-ios-apps-part-1/
I want to store a list of phone numbers in my app. There are currently 53 of them and I wish to store the following information.
Country
TollNumber
TollFreeNumber?
TollNumber2?
ISOCode
It won't be updated very often and will likely be done manually for now, maybe by an API in the future. Persistent storage is not necessary. I'd normally just jam this in to a [[String]] Array or perhaps a [[NSDictionary]], but I'm curious to know what the pros and cons are of the various non-persistent solutions out there.
For example would there be advantages/downsides to storing this in XML or JSON formats, are persistent solutions worth considering despite not needed persistence and are there Swift structures better suited to handling static data sets?
If you are not interested in persisting the edits (CRUD) for the given information, creating a model to be a representation (template) of your data would be a good choise, for instance:
// of course determining what's the data type of each property is up to you,
// or even giving them an initial value...
struct MyModel {
var country: String?
var tollNumber: String?
var TollFreeNumber: String?
var TollNumber2: String?
var ISOCode: String?
}
let containerArray = [MyModel(country: "country", tollNumber: "tollNumber", TollFreeNumber: "TollFreeNumber", TollNumber2: "TollNumber2", ISOCode: "ISOCode"),
MyModel(country: "country", tollNumber: "tollNumber", TollFreeNumber: "TollFreeNumber", TollNumber2: "TollNumber2", ISOCode: "ISOCode"),
MyModel(country: "country", tollNumber: "tollNumber", TollFreeNumber: "TollFreeNumber", TollNumber2: "TollNumber2", ISOCode: "ISOCode"), ...]
If you are required to read the given data from a file, I think that .plist file would be a good choice, it is easy to work with from the end-users perspective, also check this Q&A.
Although reading data directly from a struct instance -as mentioned in the first approach- should be better (speed wise), the benefit of working with a .plist file might be the ease of editing, all you have to do is replacing the updated file and that's it!
Remark: if we are talking about a small amount of data, speed issue won't be notable -almost- at all.
Also: if you are looking for a mechanism to persist data into your application, you might want to check this Q&A.
Hope this helped.
One possible and easy to implement solution would be to convert your data into JSON and save that to the file system. It would allow you later to fetch the phoneNumbers from an API and just store them locally.
You could also look in storing them in a .plist file and reading from that. CoreData or Realm are in my opinion an overkill for your situation.
I am having doubt in whether to use string or string builder to append html elements like "div" and others in my page in mvc. Is there any other approach for this thing.
Thanks.
I read that Microsoft recommends using StringBuilder when you predict to have more then 6 concatenations.
StringBuilder is the way to go. A String holds a reference to an immutable (fixed) string, and appending to a string is horribly inefficient. If your intention is to repeated perform appends then this is exactly what the StringBuilder was designed for.
You should use StringBuilder if you change string a lot (add, remove, change, replace characters) because it's more efficient. If you do simply operation you should use string.
The problem with string is that it's immutable, so operatrion
string text = myStringVariable + "new string"
causes that the new instance of the text variable will be created. If you do many operation on string class then you will have many instances of string objects.
Whenever you have perform appending texts, you should always use stringbuilder.
Using string would repeatedly create new instances of a string and hence inefficient.
check this post for the depth knowledge about : Why to use StringBuilder over string to get better performance
To be honest and saying something unusual at the end it really does not matter. The differences are so small that you shouldn't care about this and you should invest the time in other things that make difference.
Check this article of Jeff where all this is explained (also in a web environment, when he was creating StackOverflow).
Coding horror article about why does not matter how do you create strings
String bulider Can Be Used When More than One String to be
concatenated.
StringBuilder which is more efficient because it does
contain a mutable string buffer. .NET Strings are immutable
which is the reason why a new string object is created
every time we alter it (insert, append, remove, etc.).
I like how cleanly an object is stored in ravenDB, but have a practical question for which I'm not sure of the best answer.
Lets say i have a quote request:
QuoteRequest.cs
int Id;
dateTime DateCreated;
List<Quotes> Quotes;
Quote.cs
int ProviderId;
int Price;
int ServiceDays;
int ServiceTypeId;
when someone hits a page, i spit out a list of quotes from which they can choose. These quotes are only related to an instance of the quote request.
My question is, since a child object, such as a quote in the list, doesnt have an Id generated by the database, how do I generate a querystring to let the next page know which quote the user wants to buy?
There could be multiple quotes by one providerId.
My thoughts were either add a QuoteId and increment it based on this.Quotes.Count, but that seems a little hacky, or generate a random number, also a little hacky.
How do people generally handle something like this?
Do you really need to associate the purchase (what the user chose to buy) with the original quote? I'm guessing that you take the quote and then convert it to a purchase.
If that is so, then don't worry about an id at all. Just pass along the constituent values to the next step. In other words, treat quote like a Value in the DDD sense.
However, if you do need to store an association to the purchase... well, then it depends on what you really need to track. For example, you could just update the QuoteRequest, marking the selected quote. (Add an IsSelected or something similar to the quote class Quote.) Then the purchase could be linked back to the quote request, and you could identify the quote by way of the flags.
Again, all this depends on the context (and I'm just making guesses about that).
Since no one has answered this yet I'll just say how I would do it;
Why add a Id at all? just use the index of the List? It the request is "?quote=0" they get the quote at position 0?
Not really sure If I'm not getting something here though...
One option is to have the parent object store the last used id. When adding a new child object you increment the id-counter and add that to the child. When the object is saved the id-counter is automatically incremented.
Lets say you have blog post with comments:
public class Post
{
public int NextCommentId;
public List<Comment> Comments;
...
}
...
var comment = new Comment { Id = post.NextCommentId++ };
post.Comments.Add(comment);
session.SaveChanges();
The code above might not be 100% correct, but should give you an idea of how to do it at least!