Use external file or resource?
The end result is that your file(s) get embedded in your DLL so that you only have 1 DLL to distribute instead of a DLL and a folder of files that have to move around together. That said, you haven’t said how large these files are.
Use external file or resource?
I am writing a C# application that uses a long «hard-coded» string.
For maintainability reasons I have decided to put this string in an external text file and load it. Is this a good idea? The extra I/O does not seem big in this case.
I realize that I also have an option to embed this file as a .resx resource. Is this a better idea? The file will never need to be localized.
If you intend to allow users/administrators to change the string, I agree with the other answers, and I’d suggest putting it in settings.
If you don’t want it to be editable after deployment and it will only be modified by you and your developers, then I would put it in an embedded resource (note, this is not the same as a .resx file). You would read it at runtime like this:
Assembly assembly = Assembly.GetExecutingAssembly(); Stream stream = assembly.GetManifestResourceStream(“MyAssemblyNamespace.MyTextFile.txt”); StreamReader reader = new StreamReader(stream); string theText = streamReader.ReadToEnd();
Update: This is solution that is easy to maintain. The .txt file will just be another file in your solution explorer in Visual Studio and you can edit it just like any other file, keep it under source control like any other file, etc. To turn it into an embedded resource by changing the Build Action in the properties window to «Embedded Resource».
The end result is that your file(s) get embedded in your DLL so that you only have 1 DLL to distribute instead of a DLL and a folder of files that have to move around together.
Update 2: Regarding «production debugging», this is a very static solution, and so you won’t be able to change the contents of the text file at runtime because the file is baked into the DLL at compile time. For reading the contents of the file, you can use tools like reflector to view the embedded resources of a DLL. You could also write a simple command line tool that dumps all the embedded .txt files from a DLL into individual files for you to look at.
For memory usage, there isn’t a solution more efficient than «I load it from a file into memory only exactly when it is needed». You have to decide whether the improved maintainability and deployment is worth the cost of a little extra memory when your DLL is loaded into memory for your specific situation. That said, you haven’t said how large these files are. If they are really huge (megabytes+), I would probably not use this solution and would go with loose files on the hard drive. If they are generally pretty small (hundreds of kilobytes), I wouldn’t worry about the extra memory unless you are in some kind of embedded device situation where RAM is really tight.
Why not make it an appSetting in your web/app.config file?
using System.Configuration; //To ease your typing pains var myReallyLongString = ConfigurationManager.AppSettings["MyLongString"];
You can follow this MSDN link how to use Application and User Settings.
I would put this in an application configuration file.
Scope on extension resource types, Apply at deployment scope. To apply an extension resource type at the target deployment scope, you add the resource to your template, as would with any resource type. The available scopes are resource group, subscription, management group, and tenant. The deployment scope must support the …
Why should I put external resources in the html head?
I really like to know why people say «put external resorces on the head of the page», such as :
Is it not better to add these resources to the page only if it needs those resources? For example, when programming on PHP, if I have a page that needs some CSS instead of other that don’t need it, will it be more intelligent to use link type or script src into that page, without loading everything in the top, isn’t it?
I see many suggestions on forum/community that say to put ALL in the head. what can you say?
The reason to use external resources, even if they include directives that aren’t used on the current page, is to take advantage of HTTP and browser caching. The resource is only sent to the browser once, on first request. This is especially important for mobile devices as there is a great deal of overhead in sending additional resources over HTTP when using limited bandwidth.
You should put external JavaScript last in the page body, not the HTML head.
There are two things here, and I’m not sure which you are asking about.
Saying they should be in the can mean as opposed to elsewhere in the document. Some people do put CSS links in the body, even though they aren’t allowed there, and also while is allowed in the body, it’s almost always a better idea to put it in the head (and those cases where it isn’t should be obvious, and even though can often be re-written to work better from the head, with the exception of cut-and-paste scripts relating to other services, where e.g. amazon give you a script to put in your page that they want to make simple for you, rather than requiring serious re-writing of your code).
As for whether it should only be in those pages that need them, there is a balance here.
Firstly, let’s say that 90% of a CSS stylesheet’s declarations are used on all pages in a site (this is a sign of a well thought-out style in itself). We could put the other 10% in separate files for some pages only, and indeed more likely 2% in one, 3% in another and 5% in yet another or something like that. However, since each URI access has its own overhead on top of the bandwidth of the actual stream downloaded, we probably lose more than we gain here.
Add in the effect of caching, and keeping it in one file is a clear winner.
On the other hand, say there are a set of resources in a site that have very different styling needs to the others. In this case the separate CSS might be as large or larger than the first one, so it makes more sense to have that as a separate file only used where necessary (also, it can make over-riding in the cascade work much better, e.g. if the «main» stylesheet gives a blue background then the second can give a red background and we indicate which we need simply by whether that stylesheet is there or not, without having to set different classes throughout the relevant HTML pages).
The same applies to javascript, a couple of functions used only rarely should probably still be in the «common» javascript files, but a large set of functions for use only in one page should almost certainly only be loaded in that one page.
That said, we may choose to include the second js in a relatively light page that is likely to be hit before a relatively heavy page that will use it, just to take advantage of the caching. This latter I would consider more an optimisation of the site as a whole, than a concern of the design of that one page (considered as an individual page, the second js is clearly wasteful).
Finally, it’s just simpler to have a single file handling all of these items, and the waste in a few extra downloads may be justified by the reduced maintenance.
If your page only contains PHP, there is no need for external resources. (= no head, no doctype clarification, no tags)
If it does contain minor or major HTML (or any other markup for that matter) it is highly adviced to use external resources.
For your javascript it’s a best practice to put all your tag in the bottom of the page just before just