Suppose I have a class:
public class Foo { public Bar RequiredProperty { get; set;} public void Baz() { if (this.RequiredProperty == null) {
My solution has a class that is designed to be reused without having to pass many arguments to the Bar method over and over again. So what should I do if Bar not initialized to a non-zero value?
Additional Information I essentially collapse my own code analyzer and formatter. Call it a lesson. One of the classes is the HtmlCodeFormatter , which has the following properties (in honor of dependency injection):
public IFormatter Formatter { get; set; } public IParser Parsre { get; set; }
This allows me to write any number of language-specific parsers and formatters. For example, I have CSharpParser and JavascriptParser . I also have an HtmlCodeFormatter , and have plans for another (dubious utility).
The idea is that you can instantiate an HtmlFormatter using an object initializer, for example:
var formatter = new HtmlCodeFormatter() { Parser = new CSharpParser(); Formatter = new HtmlCodeFormatter(); }; formatter.Format("Console.WriteLine(\"Hello, world!\"));
When HtmlCodeFormatter.Format is HtmlCodeFormatter.Format , it should be able to verify that both the parser and the formatter have been provided. This is not a problem, in fact, but I'm a little fixated on which exception to throw. I tend to have an InvalidOperationException , but I'm not quite sure what the best choice is.
Mike hofer
source share