What I want is a class (or a list or whatever) where I can say:
String ClientName;
String DealerID;
And it would generate the code for me like
public static string ClientName
{
get
{
object obj = HttpContext.Current.Session["clientName"];
if (obj != null)
{
return (string)obj;
}
return null;
}
set
{
HttpContext.Current.Session["clientName"] = value;
}
}
One way may be to use reflections but I don’t know how.
Another solution maybe to use typed datasets but again I don’t know how.
Another Way may be to use T4 templates but I do´t know how.
T4 sample:
It generates the following code:
If you do extract the “view” the model file would look like this:
Regarding CodeSnippets vs T4
Sometimes it is thought that CodeSnippets (and Resharper code templates) are equivalent to T4. They are not.
CodeSnippets (and others) promotes code redundancy and is basically CopyPaste programming with extra tool support.
T4 (or CodeSmith) are MetaProgramming Tools which helps you reduce code redundancy in the code you maintain (they might generate redundant code but you don’t need to maintain that code).
A thought experiment around CodeSnippets; you have used a snippet extensively but you realize there’s an issue in the code it generated.
How do you resolve it? You have to find all instances where you used the snippet and adjust the code but run into problems; how do you find all instances? How do you merge the Changes when someone modified the snippeted code?
With MetaProgramming Tools like T4 or CodeSmith you fix the template and regenerate the code.
This is why I die a litte bit inside everytime someone mentions code snippets.