Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm learning C# by programming a real monstrosity of an application for personal use. Part of my application uses several SPARQL queries like so:

const string ArtistByRdfsLabel = @"
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?artist
WHERE
{{
    {{
        ?artist rdf:type <http://dbpedia.org/ontology/MusicalArtist> .
        ?artist rdfs:label ?rdfsLabel .
    }}
    UNION
    {{
        ?artist rdf:type <http://dbpedia.org/ontology/Band> .
        ?artist rdfs:label ?rdfsLabel .
    }}
    FILTER 
    (
        str(?rdfsLabel) = '{0}'
    ) 
}}";

string Query = String.Format(ArtistByRdfsLabel, Artist);

I don't like the idea of keeping all these queries in the same class that I'm using them in so I thought I would just move them into their own dedicated class to remove clutter in my RestClient class.

I'm used to working with SQL Server and just wrapping every query in a stored procedure but since this is not SQL Server I'm scratching my head on what would be the best for these SPARQL queries. Are there any better approaches to storing these queries using any special C# language features (or general, non C# specific, approaches) that I may not already know about?

EDIT: Really, these SPARQL queries aren't anything special. Just blobs of text that I later want to grab, insert some parameters into via String.Format and send in a REST call. I suppose you could think of them the same as any SQL query that is kept in the application layer, I just never practiced keeping SQL queries in the application layer so I'm wondering if there are any "standard" practices with this type of thing.

share|improve this question
    
what are the advantages of using SPARQL? in which type of app are you using it? –  Yusubov Jun 23 '12 at 21:14
    
@ElYusubov, I'm writing a desktop app that will query Wikipedia via DBpedia using SPARQL. The advantages of using SPARQL is that it is easier to get more specific information from Wikipedia than using the Mediawiki API. –  Jason Holland Jun 23 '12 at 21:19
add comment

3 Answers

up vote 2 down vote accepted

You may also consider the Resource File (the one with .resx extension in VS) for this task. It may serve like a dictionary , a pair of < key, value > to store/access your "blobs of text" much easily. Here you are couple links: MSDN

share|improve this answer
add comment

You can always store them in a Database table and retrieve them on application start up or on first use. With proper caching the performance would be as good. The advantage is that they can be changed without recompiling the application. A dedicated static class or even storing them as resources is a good idea as well.

share|improve this answer
add comment

You shouldn't be creating dynamic queries. Your best bet in that environment would be to create stored procedures. Make sure your stored procedures are also not dynamically generated. Each field needs to be parameterized to avoid the risk of a sql injection attack. It's also a separation of concerns violation. Your application code should consist of application logic. Your database should contain your database queries.

Not to mention, these dynamically generated queries will become a nightmare to maintain.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.