7. Repeating myself
[Serializable]
public class AnException : Exception
{
public AnException ();
public AnException (string message);
public AnException (string message, Exception exc);
protected AnException (
SerializationInfo info, StreamingContext context);
[SecurityPermission(
SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData (
SerializationInfo info, StreamingContext context)
}
8. Repeating myself
public class MyViewModel : INotifyPropertyChanged
{
public int MyProperty
{
get { return m_myProperty; }
set
{
m_myProperty = value
OnRaisePropertyChanged ("MyProperty")
}
}
void OnRaisePropertyChanged (string propertyName);
}
10. Repeating myself
public class MyViewModel : INotifyPropertyChanged
{
public int MyProperty
{
get { return m_myProperty; }
set
{
m_myProperty = value
OnRaisePropertyChanged ("MyProperty")
}
}
void OnRaisePropertyChanged (string propertyName);
}
11. Repeating myself
public static readonly DependencyProperty PreviewWidthProperty =
DependencyProperty.Register (
"PreviewWidth",
typeof (double),
typeof (AccordionPanel),
new FrameworkPropertyMetadata (32.0));
public double PreviewWidth
{
get { return (double)GetValue (PreviewWidthProperty); }
set { if (PreviewWidth != value)
SetValue (PreviewWidthProperty, value); }
}
28. Autobiographical examples
MC68000 – Self modifying code
C/C++
- Higher order macros
C++ - Partial template specialization
.NET
– System.Reflection.Emit
.NET
– System.Linq.Expressions
T-SQL
– SELECT * FROM SYS.tables
Visual Studio – T4
29. Benefits of T4
Simple concept
ASP/PHP for code
Easy to debug
The generated code can be debugged just as easy as hand
written code
Flexible
T4 can generate any text artifact
C#
VB
C++
Doesn’t affect deployment
30. Additional resources
Visual Studio Pro
2008, 2010 or 2012
T4 Addin
http://t4-editor.tangible-engineering.com/
Good T4 Blog
http://www.olegsych.com/
33. Separate generated & crafted code
partial class X
{
partial void Partial_Y (int q, ref int a);
public int Y (int q)
{
var a = 0;
Partial_Y(q, ref a);
return a;
}
}
35. DependencyProperties (WPF)
Controls requires DependencyProperties
DependencyProperty pattern Redundant code
Becomes inconsistent
Error prone
Naming conventions
Takes a long time to write
Is mind-numbingly boring to write
T4 can help
36. T4 – ASP/PHP for code
class X
{
<# for (var iter = 0; iter < 10; ++iter) { #>
public int X<#=iter#> = <#=iter#>;
<# } #>
}