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.).
Related
I have a stored procedure that has many parameters and I've been using the following to return the results:
db.Database.SqlQuery<GetListCamera_Result>("Camera.sp_get_list_camera #room_name, #disk_status,
#signal_loss_reason, #department_id , #sortColumnName, #sortDirection, #start, #length",
new SqlParameter("room_name", room_name),
new SqlParameter("disk_status", disk_status),
new SqlParameter("department_id", department),
new SqlParameter("signal_loss_reason", reason),
new SqlParameter("sortColumnName", sortColumnName),
new SqlParameter("sortDirection", sortDirection),
new SqlParameter("start", start),
new SqlParameter("length", length)).ToList();
I saw one of my senior using these much more cleaner than mine:
db.Database.SqlQuery<GetLiquidbooks_Result>("sp_get_liquidbooks {0}, {1}, {2}, {3}, {4}",
new object[] { LiquidCode, LibID, LocPrefix, LocID, UserID }).ToList();
What are the differences and is there anything I need to be aware of if I'm switching to his
I think yours is a much safer way. However, if you'd like to make it simpler, you can refer to this article, you don't have to initialize a SqlParameter instance, but the #p1 #p2 syntax is still essential. In the first section of the article, it also mentions that this care should be taken.
AFAIK, SqlQuery doesn't prevent SQL Injection, which means if I pass the DROP command into your second sample, the table may be deleted permanently. Therefore, the one which the senior wrote might expose potential security risks, you should make sure that you use parameters in your query in the correct way to guard against such attacks.
About the second sample, consider using ObjectContext.ExecuteStoreQuery<T>(), it allows you to pass the query string with {0} {1} syntax and object array as the parameter into the method. This method actually invokes CreateStoreCommand which transforms your query and objects into a parameterized query. But SqlQuery seems not.
FYI:
ExecuteStoreQuery source code - You can take a look at this method to get deep into how it works.
SqlQuery source code - As aforementioned, I rechecked the source code, and I couldn't find any codes that help to turn it into parameterized SQL
I want to replace some string in my url like this
request.RawUrl.ToString().Replace("sometext566666", "othertest")
but it s not working why is it so?
For example, the original url is like
/sometext4554544454.aspx
and I want it like this
/sometext.aspx
I'm guessing that this is .NET. If so, you should be aware the String.Replace() returns a new string containing the result of the replacement (as do all other methods that purport to modify a string).
So you need to assign the result to a variable or field to hold the result. In some circumstances, you might assign the result back to the same place you obtained the original string from. But you're not allowed to overwrite RawUrl (and, it would be potentially confusing for you to do so).
The statement you are using is working, but you are not assigning the result of the replace function, just executing it.
request.RawUrl.ToString().Replace("sometext566666", "othertest")
If you want to keep the result, you will need to assign it to a string.
e.g.
String result = request.RawUrl.ToString().Replace("sometext566666", "othertest");
Otherwise, you can assign it to the same RawURL but I think that is a URI so you'll need to use a new URI, something like:
request.RawUrl = new URI(request.RawUrl.ToString().Replace("sometext566666", "othertest"));
Nevertheless, I'm not sure if you can actually edit that property.
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.
Right now I am using ArrayCollection. But I want to change that to Set as I want make sure do duplicate values come.
var addressList:ArrayCollection = new ArrayCollection();
One way is I can use Dictionary and store addresses as a key. And I can just use keys to iterate.
But I am looking for Java HashSet like implementation.
You want to download Polygonal Data Structures. The swc contains a HashSet. If you want Java-style template syntax for Flash, you should also check out Haxe.
The AS3 equivalent to HashMap or HashSet is the Dictionary class, and to a lesser extent, the Object class. Object keys are stored as strings, while with Dictionary the keys are objects. You can't have duplicate entries with either. Are you looking for a specific implementation other than that?
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!