Last active
June 24, 2016 18:26
-
-
Save tormodfj/5778009 to your computer and use it in GitHub Desktop.
Using T4 to make redundant code more maintainable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template language="C#" #> | |
<#@ output extension=".cs" #> | |
<# | |
var exceptions = new [] | |
{ | |
DefineException("Message"), | |
DefineException("BadResponse").DerivedFrom("Message"), | |
DefineException("InvalidState") | |
}; | |
//---------------------------------------------------------------------------------- | |
#> | |
using System; | |
using System.Runtime.Serialization; | |
namespace MyNamespace | |
{ | |
<# foreach(var exception in exceptions) { #> | |
[Serializable] | |
public partial class <#= exception.ClassName #> : <#= exception.BaseClassName #> | |
{ | |
public <#= exception.ClassName #> () : base () {} | |
public <#= exception.ClassName #> (string message) : base (message) {} | |
public <#= exception.ClassName #> (string message, Exception inner) : base (message, inner) {} | |
protected <#= exception.ClassName #> (SerializationInfo info, StreamingContext context) : base (info, context) {} | |
} | |
<# } #> | |
} | |
<#+ | |
//---------------------------------------------------------------------------------- | |
ExceptionDefinition DefineException(string name) | |
{ | |
return new ExceptionDefinition { Name = name, BaseName = "" }; | |
} | |
class ExceptionDefinition | |
{ | |
public string Name; | |
public string BaseName; | |
public string ClassName { get { return Name + "Exception"; } } | |
public string BaseClassName { get { return BaseName + "Exception"; } } | |
public ExceptionDefinition DerivedFrom(string baseName) { BaseName = baseName; return this; } | |
} | |
#> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.Serialization; | |
namespace MyNamespace | |
{ | |
[Serializable] | |
public partial class MessageException : Exception | |
{ | |
public MessageException () : base () {} | |
public MessageException (string message) : base (message) {} | |
public MessageException (string message, Exception inner) : base (message, inner) {} | |
protected MessageException (SerializationInfo info, StreamingContext context) : base (info, context) {} | |
} | |
[Serializable] | |
public partial class BadResponseException : MessageException | |
{ | |
public BadResponseException () : base () {} | |
public BadResponseException (string message) : base (message) {} | |
public BadResponseException (string message, Exception inner) : base (message, inner) {} | |
protected BadResponseException (SerializationInfo info, StreamingContext context) : base (info, context) {} | |
} | |
[Serializable] | |
public partial class InvalidStateException : Exception | |
{ | |
public InvalidStateException () : base () {} | |
public InvalidStateException (string message) : base (message) {} | |
public InvalidStateException (string message, Exception inner) : base (message, inner) {} | |
protected InvalidStateException (SerializationInfo info, StreamingContext context) : base (info, context) {} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MyNamespace | |
{ | |
public partial class InvalidStateException | |
{ | |
public int StatusCode { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment