|
I'm replacing a ruby on rails site with C#/mvc. For the most part it's a complete rewrite because of problems with the site being scrapped. There're occasional parts where some logic is worth a directish port though. One is scoring standardized tests.
I've got a test responses table full of columns like:
Section1Question1Answer CHAR(1),
Section1Question2Answer CHAR(1),
...
Section1Question5Answer CHAR(1),
Section2Question1Answer CHAR(1),
Section2Question2Answer CHAR(1),
...
Section13Question5Answer CHAR(1),
The ruby code is then able to access the data like:
for section in 1..SECTION_COUNT
for question in 1..QUESTION_COUNT
test_to_score["Section{section}Question{question}Answer"] #do stuff
end
end
In C# I'm stuck with accessing them 1 at a time
TestToScore.Section1Question1Answer
TestToScore.Section1Question2Answer
I'm not doing enough processing for load/unloading temp arrays to be worthwhile, and every time I've thought to myself that reflection would be a good way to solve a problem I've ended up regretting it. Redoing the DB to store a single answer/row instead of a test attempt/row would just change where the pain points are; and since the test will always be M sections of N questions each keeping the entire response together is a lot simpler.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, weighing all things in the balance of reason?
Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful?
--Zachris Topelius
Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies.
-- Sarah Hoyt
|
|
|
|
|
Reflection.
Or put a reference to the fields, or whatever, into an array... and iterate.
#SupportHeForShe
Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson
You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
|
|
|
|
|
Did you read what I wrote to the end? Every time I've done reflection it ends up becoming a footgun at some point in the future; and for what amounts to a single pass over the scores loading/unloading into arrays is just as verbose as accessing everything once directly.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, weighing all things in the balance of reason?
Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful?
--Zachris Topelius
Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies.
-- Sarah Hoyt
|
|
|
|
|
My first was "reflection" as well, but then I read what you wrote at the end
I don't think reflection could hurt here though.
It's just a small piece with a clear function.
It wouldn't be much different than how Ruby solves it, which is calling the member using a literal string.
It could go wrong if your naming convention changes or if someone added QuestionXSectionYAnswer (switching Question and Section), but that goes for the Ruby code as well.
|
|
|
|
|
Sander Rossel wrote: It wouldn't be much different than how Ruby solves it, which is calling the member using a literal string.
I know the feature in ruby works because its hashes all the way down (until you get to the pipe were the designers smoke it anyway); but the hackiness there is a core language feature not a gun that shoots bullets in multiple directions with every trigger pull only more dangerous.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, weighing all things in the balance of reason?
Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful?
--Zachris Topelius
Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies.
-- Sarah Hoyt
|
|
|
|
|
I see your point, but I'd still recommend reflection.
Although typing out this stuff once isn't such a big deal either.
writing a reflection solution probably costs you as much time as writing it out.
The only reason to use reflection is because it's more fun to write
|
|
|
|
|
I generated the 1 off code. Used a mix of typing and excel copydown to generate the numbers and then regexed the code I wanted out.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, weighing all things in the balance of reason?
Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful?
--Zachris Topelius
Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies.
-- Sarah Hoyt
|
|
|
|
|
|
Or start with an array?
Section1Answers VARCHAR(N)
Then just parse/count and iter. Less of a mess in the DB?
|
|
|
|
|
Maybe. Not sure what he's trying to accomplish. Maybe a table with question and answer columns?
#SupportHeForShe
Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson
You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
|
|
|
|
|
Quote: Section1Question1Answer CHAR(1),
Why can't you do a table with:
SectionNumber int
QuestionNumber int
Answer char(1) ?
|
|
|
|
|
Way too much refactoring for the potential benefit; even before data migration and fighting with a CI system that chokes on anything it thinks might be a lossy DB change (a separate issue entirely).
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, weighing all things in the balance of reason?
Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful?
--Zachris Topelius
Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies.
-- Sarah Hoyt
|
|
|
|
|
Or you could go old school - DataTable!
table.Columns[$"Section{section}Question{question}Answer"]
|
|
|
|
|
If this thread does not end up in a lecture about the billionth other possibilities how you could have done it better in C#, this is not a coding forum anymore.
I love this kind of threads:
Q: "- Hey guys, I am using X to do Y, and I would need to know how I could do Z"
A1:"- Why are you using X to do Y ? W is much better".
A2:"- Wow, I would never use X that way. Plus Y is not recommended by [book you have never heard of, but obviously should have]. Maybe you should consider switching technologies"
...
A187273:"- No wonder that we have flat-earthers bitching about global warming criticizing vegans that do not believe in the moon landing if you do Y by using X".
|
|
|
|
|
Do not discount Reflection solely based on the fact that you weren't able to make good use of it previously. It has amazing powers but should not be your first choice, usually.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
Before I submit this Tip thing, I wanted to know among people that have used the codeDom, if they'd prefer something like this?
var result = CD.Method(typeof(bool), "MoveNextInput");
var input = CD.FieldRef(CD.This, "_input");
var state = CD.FieldRef(CD.This, "_state");
var line = CD.FieldRef(CD.This, "_line");
var column = CD.FieldRef(CD.This, "_column");
var position = CD.FieldRef(CD.This, "_position");
var current = CD.PropRef(input,"Current");
result.Statements.AddRange(new CodeStatement[] {
CD.If(CD.Invoke(input,"MoveNext"),
CD.IfElse(CD.NotEq(state,CD.Literal(_BeforeBegin)), new CodeStatement[] {
CD.Let(position,CD.Add(position,CD.One)),
CD.IfElse(CD.Eq(CD.Literal('\n'),current),new CodeStatement[] {
CD.Let(column,CD.One),
CD.Let(line,CD.Add(line,CD.One))
},
CD.IfElse(CD.Eq(CD.Literal('\t'),current),new CodeStatement[]
{
CD.Let(column,CD.Add(column,CD.Literal(_TabWidth)))
},
CD.Let(column,CD.Add(column,CD.One))))
},
CD.IfElse(CD.Eq(CD.Literal('\n'),current),new CodeStatement[] {
CD.Let(column,CD.One),
CD.Let(line,CD.Add(line,CD.One))
},
CD.IfElse(CD.Eq(CD.Literal('\t'),current),new CodeStatement[] {
CD.Let(column,CD.Add(column,CD.Literal(_TabWidth-1)))
}))),
CD.Return(CD.True)),
CD.Let(state,CD.Literal(_InnerFinished)),
CD.Return(CD.False)
});
I know it looks like hell, but it's so much less verbose than using the raw object model
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
meh
#SupportHeForShe
Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson
You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
|
|
|
|
|
Excuse me while I'm going to spoon my eyes out with a blunt object
Can you maybe break it up into multiple lines?
Perhaps use multiple descriptive functions to build up the model?
Maybe even split it up in classes?
Something like:
CD.AddStatement();
var ifStatement = CD.CreateIf(CD.NotEq(state, CD.Literal(_BeforeBegin)));
var elseStatement = CD.CreateElse(CD.Whatever);
CD.AddIfElse(ifStatement, elseStatement);
AddSomeStatement(CD);
CD = CD.AddStatement();
CD = CD.AddIfElse(ifStatement, elseStatement);
CD = AddSomeStatement(CD);
No idea if that would work, just spewing idea's here.
I did something like that once.
Maybe this will give you your "Eureka!" moment, maybe it won't.
|
|
|
|
|
I used to do something like that, and then I ran into issues with it. It either got stale or got confusing.
The thing about CD is it's actually CodeDomUtility , it's just I use a using to abbreviate it.
That's all static methods that simply create code dom objects. I will eventually make a visitor for examining and modifying existing trees (i've had lots of good luck with that in the past)
My point is it's not a builder class. If I were to make one of those I'd probably call it CodeDomBuilder , but there's a lot of problems with state management with a codedom like that.
In the end, I've found it most expedient to declare everything inline, like I did.
As messy as it is, you actually get used to reading it, and it's pretty understandable once you do, as long as you're indenting.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
honey the codewitch wrote: CodeDom users, what's your opinion? I like it without... it feels more natural
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
var @this = new CodeThisReferenceExpression();
Feels natural?
I prefer CD.This
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
OK.. it looks like the pun was no that obvious
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
sorry. i guess i'm slow. =)
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
CodeDom --> |Coud-Dom| ~= |Con-Dom|
Q: CodeDom users, what's your opinion? --> Condom users, what's your opinion?
A: I like it without... it feels more natural.
I suppose my bad english pronunciation has played against my "obvious" joke
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
using the codedom always has me worrying that it will break. =)
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|