views:

53

answers:

4

We have a static html/webform site, the site lacks search functionality, I was able to get yahoo BOSS (Build your Own Search Service) after a few hours yesterday, i got it working (still working on adding missing features like pagination) , I was wondering about the configuration options of the class, as I have a BossSearch.cs in App_Code, with some fields that are set at the top:

public class BossSearch
{
    String sResultsPage = "~/searchResults.aspx";
    String sSearchString="";

    String sApiKey = ConfigurationSettings.AppSettings["BossApiKey"];
    String sSite = "www.oursite.com"; //without http://
    String sQuery = "http://boss.yahooapis.com/ysearch/web/v1/{0}%20+site:{1}?appid={2}&format=xml&start={3}&count={4}";
    String sStart = "0";
    Uri address;

    WebProxy webproxy = new WebProxy("http://192.168.4.8:8080");
    bool bUseProxy = true;

    int nResultsPerPage = 10;

    int nTotalResults = 0;
    ...

As you can see, i get the BossApiKey from the web.config file, but all others I have them in the declared in the class, should I put all of them in the web.config file? if I'm thinking of reusing the class (should i say class library?) in other websites as well? can I turn it into a dll and what would the advantages be? i read somewhere that a dll has its own config file, is this the way to store those settings?

Apologies for my ignorance, since I'm not that familiar with developing applications (still studying)

+2  A: 

If you declare all of them in a database or web.config, you don't need to recompile each time you reconfigure the search engine

Nico
Great, thanks for the answer, one of the issues is that we don't have a database available (I know right? ridiculous that, at this day and age, management can't provide me with a DB server).
Prozaker
+2  A: 

Hi,

I you're striving for reuse and ease of use, then I would recommend writing a custom configuration section for your control. This can be part of the dll you distribute to other application and will allow you to have the ultimate in flexibility and explicit portability to other .net apps.

Enjoy!

Doug
I will take a look at it, thanks for the post. :)
Prozaker
@Prozaker - no problem - its a bit to learn but once you do you can have native config sections that work just like the built in ones - they will work across app.config's and web.config's
Doug
+2  A: 

What you read about .NET assemblies having their own config files is not absolutely correct; a web site has web.config files, one in the root and zero/one in each subdirectory. If a .NET assembly that is in the application calls into the standard config API, it will get its data from that web.config.

The same goes for WinForms apps and the [appname].exe.config file; any assemblies included that use the standard config API will be getting their data from that.

All of that is not to say that any assembly could not define its own configuration mechanism which pulls its data from wherever it wants.

And yes; if you intend to reuse this code a good bit, you are thinking along the right lines; put the code in its own assembly, and have it get its data from Config files so you do not need to recompile it for each application.

Andrew Barber
If the configuration grows into say, a dozen of keys I need to define, is there a best practice on how to name them? or should I consider putting the configuration in a separate file (I think I saw that the web config could 'include' other files.. I don't know if this is a good practice.
Prozaker
I would definitely suggest considering custom configuration at that point. Configuration 'including' would not be a solution for you to consider in creating your library and how it accesses configuration information; that would be a choice made by the web site administrator who needs to set it. No matter what you do with your library using the app.config, whether the appSettings or a custom section, web admins will be able to use/not use config includes to set those values.
Andrew Barber
Thanks for taking the time to respond. I will definitely look into custom configuration that you and Doug suggested. wishing I could provide you both with a 'Right answer' check, hehehe
Prozaker
+2  A: 

You should only store the value in a single place in your application. For an ASP.NET application, the web.config file is an appropriate place for these kind of things. You won't need to recompile your application if this value changes.

If you decide to put your code into a separate class library and still want to use a config file to store your api key, you should note that your appSetting key needs to be entered in the application or web site's config file - you can't define a config file for a class library.

One other approach that you might find useful would be to make a wrapper class to store your settings. You could have class with static methods to look up your appSettings key so that you get a nice, compile time, way to get the api key, rather than typing out the name of your appSettings key everywhere.

wsanville
Thanks, I think I will try to use a wrapper class, sounds like a good idea.
Prozaker