Implementing trim for all string properties - linq-to-sql

We are using linq-to-sql entities,and want to implement trimming for all string properties.One way is on submitchanges,using reflection trim all string properties.Is there any other way?
thanks

Trim on the get/set of your properties.

If you don't want to use reflection, you can implement the code generator to use generate the proper code with Trim() on all string procedures.
Another way is to get the context object and do the trimming on the submit part of the process.
I'm too sleepy now, as it's 1:45am here, to remember the proper code to do it. But it's possible, because I did something similar using Linq-to-Entities.
Edited to add
Take a look at my site. This code generator I made is for Linq-to-Entities but should be similar for Linq-to-SQL.

Related

Customizing json output for enumerations using spay-json-shapeless

Spray-json-shapeless works fine for me for general cases. What I'm looking for is a way to extend the given functionality the output from enums in a special/customized way. Maybe there is an easy way how do do this. I'm new to all of this stuff and it is really hard for me to figure out.
At the moment I use a method similar to the spray ones, called enumFormat(enum). It contains the code for reading/writing JSON. It has to be specified for every enum, so I want to get rid of this.
It would be nice if it could work the same way as for case classes.

Finding it difficult to process JSON in delphi

I'm currently working on an application that will get data of your character from the WoW armory.
Example character: My WoW Character(link)
I will get all the info I want by calling the API provided by Blizzard and I will get the response in JSON.
Example JSON: JSON response for the character above(link)
At first I tried get the data from the JSON by string manipulation.
This mean, splitting my strings, searching for keywords in the string to find the position and formatting that into individual pieces of data such as talents and stats.
This worked great at the beginning but as I wanted more data this became harder because of the many functions I ran on all the strings it just became one big blur and unclear to see what I was doing at that moment.
Is there a good way to process my JSON?
I was thinking about getting the JSON and creating an empty class.
While working through the JSON it would generate properties and store the values in there.
But I have no idea if and how its possible to generate properties dynamically.
In the future I would like to get even more data but first I want to get this up and running before even thinking about that.
Does anyone have any ideas/advice on this?
Thanks in advance.
Your JSON seems rather short and basic. It does not seem you need special speed or exotic features. http://jsonviewer.stack.hu/#http://eu.battle.net/api/wow/character/moonglade/Xaveak?fields=stats,talents
And while since Delphi XE2 you really have stock JSON parser as part of DB-Express suite, still there are concerns:
1. It was told to cause problems with both speed and reliability.
2. It would make you program dependent on DB-Express package (why, if you not actually using it for DB access?)
3. It would bind your future to Enterprise edition of Delphi.
So you'd better try some 3rd-party library.
One of the fastest would probably be Synopse JSON parser, side-project of their mORMot library. It is generally good code, with large attention to speed and developers actively helping on their forum.
One more known and used library would be Henri Gourvest's SuperObject.
It made claims to be the fastest parser for Delphi, and while due to above that is probably no more true, the speed is quite adequate for most tasks. Henri himself is not actively supporting his ex-projects, always doing something new, so the scarce documentation (also duplicated in install package) would be all you have officially, plus there is a forum where other users might help you. OTOH the main idea behind SuperObject design was uniformity, and while some tasks could really be documented better - that is mostly due to uncertainty "if this task would really work in uniform matter without any special treatment". But usually it does.
PS. Since that is wiki you may try to enhance it for future users ;-)
So coming back to documentation, you would need
1) to load the whole JSON to the library. That you can do via creating TStream by your http library or providing string buffer wth the data: that is Parsing a JSON data structure section of the manual
2) reading values like "name" and "level" - described in How to read a property value of an object ? section there.
3) enlist arrays like "talents" - described in Browsing data structure section.
XE3 has "built in" JSON support (see docwiki), but I have heard (haven't used it myself) that it isn't very well optimised.
So perhaps look for some thirdparty option like SuperObject.
Your task is easily achievable using TSvSerializer which is included in my delphi-oop library. You only need to declare your model type and deserialize it from your json string. Your model (very simplified incomplete and untested version) should look something like this:
type
TStats = class
public
property health: Integer read fhealth write Fhealth;
...
end;
TTalent = class
public
property tier: Integer read Ftier write Ftier;
...
end;
TMainTalent = class
public
property selected: Boolean read Fselected write Fselected;
property talents: TObjectList<TTalent> read Ftalents write Ftalents;
end;
TWowCharacter = class
public
property lastModified: Int64 read FlastModified write FlastModified;
property name: string read Fname write Fname;
...
property stats: TStats read Fstats write Fstats;
property talents: TObjectList<TMainTalent> read Ftalents write Ftalents;
...
end;
Then you just need to do:
uses
SvSerializer;
var
LWowCharacter: TWowCharacter;
begin
LWowCharacter := TWowCharacter.FromJson(YourJsonString);
...
You can find my contact email in delphi-oop project, ask me if something's unclear, I'll try to help you in my spare time.

I am trying to understand Functions

Ok I am coming into a stumbling block no matter what language I am using. I am trying to understand when I need to pass arguments in a Function and when I don't need to pass arguments in a function. Can someone give me some direction on where to find guidance on this?
I would rather say if your function needs data, you MUST pass parameters, cuz the other alternative is to put the data in a global store and let the function access it from there. DO NOT DO IT as it will make your code nearly impossible to maintain as it grows more complex.
Does the function need external data to perform its job? If so, then you need to pass arguments.
If the function doesn't need external data to perform its job, you don't need to worry about passing arguments.
That handles creating your own functions. If you're simply trying to call somebody else's function, you need to pass arguments for each required function parameter.
Well...if a function takes parameters, then you have to pass arguments to it. If it takes no parameters, then you don't. (If you happen to be working in a language in which functions have optional parameters, you only have to pass an argument if you want something other than the default value.)
Well that pretty much depends on what are you trying to accomplish. If your functions needs some values to modify or use you will probably need to pass arguments. Why don't you try it with some examples in some books. Most of them are pretty relevant.
You should not think on what you "need" to pass to a function, you should try to think what are you writing that function for and then you will see if you need arguments or not.
Are you talking about existing function or writing your own?
If it is an existing one - you have no choice - in order for it to work you need to pass it whatever it wants. To figure out what it wants - read the manual, the function code, or harass the author of the function
If you are talking about designing your own - it is a much bigger discussion which goes way beyond a single function. You need to understand what the function (and any other components) have to do to accomplish the ultimate goal, how they interact with each other, etc.

storing code snippets in a database

I want to make a code snippet database web application. Would the best way to store it in the database be to html encode everything to prevent XSS when displaying the snippets on the web page?
Thanks for the help!
The database has nothing to do with this; you simply need to escape the snippets when they are rendered as HTML.
At minimum, you need to encode all & as & and all < characters as <.
However, your server-side language already has a built-in HTML encoding function; you should use it instead of re-inventing the wheel. For more details, please tell us what language your server-side code is in.
Based on your previous questions, I assume you're using PHP.
If so, you're looking for the htmlspecialchars or htmlentities functions.
You would either have to escape it when you store it, or escape it when you display it. It'd probably be better to do it on display so that if you need to edit it later on, you don't have to decode it then re-encode it.
Also, you'll want to make sure you escape it properly when you store it in the database, otherwise you'd be leaving yourself open to SQL injection. Parameterized statements would be the best method, you shouldn't have to change the raw data at all.
The best thing to do is to not store it in the database. I have seen people store stored procedures in databases as a row. Just because you can doesn't mean you should.
It doesn't matter how you store it, what matters is how you render it in the HTML representation. I'd guess you'll need to do some sort of sanitation before rendering the bytes. Another option might be to convert every character to an HTML entity; this might suffice to prevent any code or tags from actually being interpreted.
As an example, view the source of a Stack Overflow page with some example code, and see how they're representing the code in the HTML.

c# XML manipulation VB code conversion query... and more!

I am following a VB tutorial to do some HTML manipulation using LINQ
It has the following construct
Imports <xmlns="http://www.w3.org/1999/xhtml">
How do I do the same in C#?
There appears to be something called an XMLNamespaceManager that may hold the solution, but I am too foolish to understand how to work it, and I am not sure it is the correct tree to bark up.
Got any advice?
VB.Net has a feature called XML Literals that is not present in C#. This import statement adds a namespace for use with those literals.
In researching this information, I found this link helpful:
http://blogs.msdn.com/bethmassi/archive/2007/10/30/quickly-import-and-export-excel-data-with-linq-to-xml.aspx
It's also very informative for working with Excel xml docs, but that's a side issue ;)
I think the example you're looking at may be using XML literals, which are not supported in C#.