Wednesday, June 17, 2009 by Nate Bross
Someone asked on Stack Overflow:
In the years that I’ve been at my place of employment, I’ve noticed a distinct trend towards something that I consider an anti-pattern: Maintaining internal data as big strings of XML. I’ve seen this done a number of different ways, though the two worst offenders were quite similar.
The Webservice
The first application, a web service, provides access to a potentially high volume of data within a SQL database. At startup, it pulls more-or-less all of that data out of the database and stores it in memory as XML. (Three times.) The owners of this application call it a cache. I call it slow, because every perf problem that’s been run into while working against this has been directly traceable to this thing. (It being a corporate environment, there should be no surprise that the client gets blamed for the perf failure, not the service.) This application does use the XML DOM.
The Importer
The second application reads an XML file that was generated as the result of an export from a third-party database. The goal is to import this data into a proprietary system (owned by us). The application that does it reads the entire XML file in and maintains at least two, sometimes as many as four, copies of the XML file throughout the entire importing sequence. Note that the data can be manipulated, transformed, and configuration can occur before the import takes place, so the importer owns this data in an XML format for it’s entire lifetime. Unsurprisingly, this importer then explodes when a moderately sized XML file is provided. This application only uses the XML DOM for one of it’s copies, the rest are all raw XML strings.
My understanding of common sense suggests that XML is not a good format for holding data in-memory, but rather data should be translated into XML when it’s being output/transferred and translated into internal data structures when being read in and imported. The thing is, I’m constantly running into production code that completely ignores the scalability issues, and goes through a ton of extra effort to do so. (The sheer volume of string parsing in these applications is frightening.)
Is this a common failure to apply the right tool for the job that others people run into alos? Or is it just bad luck on my part? Or am I missing some blindingly obvious and good situations where it’s Right and OK to store high volumes of data in-memory as XML?
I posted the following answer, which was chosen as the accepted answer and received 4 upvotes:
Any data stored in memory should be in classes. The higher volume of data we are talking about, the more important this becomes. Xml is a hugely bloated format that reduces performance. Xml should be used only for transfering data between applications. IMHO.
Originally posted on Stack Overflow — 4 upvotes (accepted answer). Licensed under CC BY-SA.
Thursday, June 11, 2009 by Nate Bross
I asked this on Stack Overflow:
I’ve read (http://blogs.msdn.com/joshwil/archive/2005/08/10/450202.aspx) that the maximum size of an object in .NET is 2 GB.
Am I correct in assuming that if I have an Object that takes up 256 MB Memory, since it is a reference type, I can have an array of these 256 MB Objects where all the objects together may takeup >2GB Memory as long as the size of the reference array stays below 2 GB?
LukeH answered (11 upvotes):
Yes, your assumption is correct.
The 2GB limit applies to each object individually. The total memory used for all objects can exceed 2GB.
(Whether the runtime is able to allocate enough memory for your requirements is another matter. I doubt if it could find a full 2GB of spare memory on a 32bit machine, but it shouldn’t be a problem on 64bit.)
Notable comments
Nate (0 upvotes): I agree, there are not many scenarios where you would need more than 2 GB in a single object. A game server or database server is really the only thing that comes to mind.
Originally posted on Stack Overflow — 12 upvotes. Licensed under CC BY-SA.
Thursday, June 11, 2009 by Nate Bross
Someone asked on Stack Overflow:
I have an EXE (app.exe) that calls a COM exposed method in a C# DLL (lib.dll). Lib.dll shows a dialog box.
How do I make the dialog box (not a windows message box) recognize the app.exe as the owner, so that when app.exe is minimized, so is the dialog from lib.dll?
The dialog cannot be modal.
Is this a problem that can be solved with Winforms? I prefer a Winforms solution, but will be satisfied with a XAML solution if necessary.
I posted the following answer, which was chosen as the accepted answer and received 2 upvotes:
I assume your C# Dll is just creating an instance of a form and calling the .Show() method. If that is the case, in your COM exposed method should take an hWnd parameter and set the forms owner to that hWnd.
Originally posted on Stack Overflow — 2 upvotes (accepted answer). Licensed under CC BY-SA.
Wednesday, June 10, 2009 by Nate Bross
Someone asked on Stack Overflow:
I’d like to implement a voting system on my site, without having to force them to create an account. They would ultimately be voting up/down a piece of content which has a unique ID.
- I know that I could store entries in a table with IP/ID, but what if there are more than one user coming from the same IP?
- Is there a way to uniquely identify visitors without it being tied to their external ip?
- If created a GUID, store it in a cookie on that machine, could it be retrieved later on that same computer with the same IP? A different IP?
Any thoughts on these questions, or any insight to a better approach would be greatly appreciated.
I posted the following answer, which was chosen as the accepted answer and received 23 upvotes:
Yes, you could use a cookie and set the expiration very far into the future; however, there is nothing stopping anyone from clearing their cache and voting again.
Your best bet, is to use the cookie and don’t allow votes from the same IP within 15 minutes of each other… without registration thats the best you can do.
Notable comments
Nate (5 upvotes): The only time that many users will have the same IP address is when there is NAT involved. This is likely at a place of business, a coffee shop, or even if someone has many computers in their house. And in these situations its unlikely that two people will be on this website trying to up/down vote the same piece of content.
Nate (0 upvotes): That is simply not true; each AOL customer will have their own IP address, until they disconenct and are assigned a new IP address. It is unlikely that a user will get an AOL IP address, vote on this website, disconnect and have another user connect to AOL get that same IP address and try to vote on this site. – as I said in my post without registration, that is the best you can do. Its not fool-proof but it will prevent extreme abuse.
meade (3 upvotes): there are many users coming from the same IP (AOL for example) and most people still use IE so storing browser related info will not help. There’s also a number of people that don’t allow cookies to be saved (not a large amount). It sounds like the voting isn’t so rigid, so I would suggest creating a random value that you store in the cookie and DB to uniquely (to the extent you can) identify someone
Originally posted on Stack Overflow — 23 upvotes (accepted answer). Licensed under CC BY-SA.
Wednesday, June 10, 2009 by Nate Bross
Someone asked on Stack Overflow:
I am ActionScript 3/Flex programmer, it is the first language I learned.
I want to learn either Java or C++.
Would one of these be easier to learn based on my current knowledge?
I posted the following answer, which was chosen as the accepted answer and received 3 upvotes:
It really depends what you want to do. C++ is more powerful and fast. But Java has a smaller learning curve.
I’d say learn C++, only because it will require you to gain a better understanding of how computers work under the hood. It will also help position you to learn Java, C#, or any other language down the road.
Notable comments
Nate (1 upvotes): Personally, I think that Visual Studio is far better than Eclipse; however, I’ve only used Eclipse for Java development. I’d try out Visual C++ Express microsoft.com/express
Originally posted on Stack Overflow — 3 upvotes (accepted answer). Licensed under CC BY-SA.
Thursday, June 4, 2009 by Nate Bross
Someone asked on Stack Overflow:
I’m writing an installer that will tune the configuration of the product for the particular hardware on which it will be run. In particular, I want to determine how much physical RAM is installed in the system so I can estimate how much memory to allocate to the product when it runs.
Ideally, I’d like to do this in a platform-independent, pure Java way, as the installer will need to run on several different platforms, but in case this isn’t possible, solutions for Windows are preferred as that’s the most common deployment platform.
In this case, it’s safe to assume that the product will be the only/main application running on the box, so I don’t have to worry about squeezing anyone else out. I don’t want to over-allocate as this, in our experience, can hurt performance.
I posted the following answer, which was chosen as the accepted answer and received 2 upvotes:
For windows, you’ll need to access WMI - this will get you going: Accessing Windows Management Instrumentation (WMI) from Java.
You’ll want to use this section of WMI: Win32_LogicalMemoryConfiguration.
There may be a pure java way of doing this, but I am unaware of it.
Notable comments
Nate (0 upvotes): Yes different versions of windows support different WMI Classes. You’ll need to adjust based on your target OS.
Originally posted on Stack Overflow — 2 upvotes (accepted answer). Licensed under CC BY-SA.
Wednesday, June 3, 2009 by Nate Bross
Someone asked on Stack Overflow:
We’re looking at using HTTPS in our ASP.NET webforms application for a shopping cart section. I have read somewhere that we will need to write code to check if the users’ browser/device has encryption to avoid it falling over?
Is this the case, if so can you point me in the direction of what sort of namespaces/blogs etc I should be looking at?
Any help appreciated.
I posted the following answer, which was chosen as the accepted answer and received 3 upvotes:
This codeproject article - http://www.codeproject.com/KB/aspnet/WebPageSecurity.aspx - is likely worth a read.
Originally posted on Stack Overflow — 3 upvotes (accepted answer). Licensed under CC BY-SA.
Wednesday, June 3, 2009 by Nate Bross
Someone asked on Stack Overflow:
In C# i am making a simple text editor with line numbers. I want to count the ammount of valid line breaks in a string.
i want to count
\r \n \r\n
How can i do this?
Or better yet, can someone point me to an article on how to line number an rtf box
I posted the following answer, which was chosen as the accepted answer and received 1 upvote:
Counting Lines - http://ryanfarley.com/blog/archive/2004/04/07/511.aspx
RTB With Line Numbers - http://www.xtremedotnettalk.com/showthread.php?s=&threadid=49661&highlight=RichTextBox
Originally posted on Stack Overflow — 1 upvotes (accepted answer). Licensed under CC BY-SA.
Wednesday, June 3, 2009 by Nate Bross
Someone asked on Stack Overflow:
The goal: Allow user to turn on and off different layers of data; and to dynamically pull the data for the current extent from a database on map move event.
This works fine and good if you hard code your VEShapeLayers as done here.
My list of layers is coming from a database, I have everything working the way I want except that when I add shapes to my VEShapeLayer none are rendered on my map. Calling VEShapeLayer.GetShapeCount() returns the expected number… so the layer has the data..
Here are the important bits of the code:
var assets = [];
if (WebServiceResult.length > 0) {
for (var i = 0; i < WebServiceResult.length; i++) {
var ix = FindLayerIndex(WebServiceResult[0].AssetMapLayer);
var velatlong = new VELatLong();
velatlong.Latitude = WebServiceResult[i].Latitude;
velatlong.Longitude = WebServiceResult[i].Longitude;
newShape = new VEShape(VEShapeType.Pushpin, velatlong);
assets.push(newShape);
}
// ix is defined above and is vaild and correct
map.GetShapeLayerByIndex(ix).AddShape(assets);
}
// a call here to map.GetShapeLayerByIndex(ix).GetShapeCount()
// returns the expected number of shapes
I posted the following answer, which was chosen as the accepted answer:
I feel dumb; I had not added the stylesheet to my page’s header, and my custom icon was rendering transparent as a result of the stylesheet not being available.
Originally posted on Stack Overflow — 0 upvotes (accepted answer). Licensed under CC BY-SA.
Tuesday, June 2, 2009 by Nate Bross
Someone asked on Stack Overflow:
I can’t capture/trigger OnMouseEnter or OnMouseLeave events through C# code for list items. To be clear, I don’t need an OnSelectedItem event.
What I want to do is to be able to handle the OnMouseEnter and OnMouseLeave events for ListBoxItem which will start the DoubleAnimation for that ListBoxItem - I want to enlarge its font on MouseEnter and restore to original size on MouseLeave.
Any ideas? Thanks.
I posted the following answer, which was chosen as the accepted answer and received 4 upvotes:
Something like this (as part of the ListBox’s DataTemplate):
<DataTemplate.Triggers>
<EventTrigger
SourceName="BorderControl"
RoutedEvent="TextBlock.MouseEnter">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="BorderControl"
Storyboard.TargetProperty="Background.Color"
To="DarkRed" Duration="00:00:00.2" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger
SourceName="BorderControl"
RoutedEvent="TextBlock.MouseLeave">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="BorderControl"
Storyboard.TargetProperty="Background.Color"
To="WhiteSmoke" Duration="00:00:00.2" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</DataTemplate.Triggers>
via http://www.dotnet-blog.com/index.php/2009/01/29/how-to-style-and-animate-a-wpf-listbox/
Originally posted on Stack Overflow — 4 upvotes (accepted answer). Licensed under CC BY-SA.