In contrast to imperative programming, a statement-type program can describe the behavior of a software in a simpler, clearer way. Declaration programming means defining the behavior of a program with a declaration, rather than writing some instructions. In C #, as with most other languages, most of your programs are imperative: Write a method in your program to define the behavior. In C #, the feature you use when you are programming is the declaration of programming. You add an attribute to a class, a property, a data member, or a method, and then. NET runtime will add some behavior for you. The purpose of this affirmation is simplicity and ease of reading and maintenance.
Let's start with an example that you've already used. When you write your first asp.net Web service, the wizard generates such code:
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
The Vs.net Web Service Wizard adds the [WebMethod] attribute to the HelloWorld () method, which defines HelloWorld as a Web method. The ASP.net runtime generates code for you to respond to this feature. The Web Service Description Language (WSDL) document generated by the runtime, which contains the document that describes the soap, invokes the HelloWorld method. ASP.net also supports HelloWorld methods for sending SOAP requests at run time. In addition, the ASP.net runtime dynamically generates HTML pages that allow you to test your new Web services in IE. And these are all responses to the previous WebMethod characteristics. This feature affirms your intent, and the runtime ensures that it is supported. Using features saves you a lot of time and there are fewer mistakes.
This is not a myth, asp.net runtime uses reflection to determine which methods in a class are Web services, and when they discover these methods, the ASP.net runtime adds some necessary framework code to these methods so that any method that adds the code becomes a Web method.
The [WebMethod] feature is just. NET class library features that may help you create the correct program faster. There are a number of features that help you create serialization types (see Principle 25). As you can see in principle 4, attributes control conditional compilation. In some other cases, you can use a declaration program to write code that you want faster, less wrong.
You should use. NET Framework with some features to affirm your intentions, which is better than your own writing. Because it takes less time, simpler, and the compiler does not have an error.
If the preset features do not fit your needs, you can also use a statement programming structure by defining your own features and using reflection. As an example, you can create an attribute that, however, is associated with the code so that users can use this feature to create types that can be sorted by default. An example shows how to add this feature, which defines how you want to sort in a customer set:
[DefaultSort( "Name" )]
public class Customer
{
public string Name
{
get { return _name; }
set { _name = value; }
}
public decimal CurrentBalance
{
get { return _balance; }
}
public decimal AccountValue
{
get
{
return calculateValueOfAccount();
}
}
}
The DEFAULTSORT attribute, the Nane attribute, implies that any collection of customers should be sorted by customer name. The DEFAULTSORT feature is not part of the. NET Framework, and in order to implement it, you create a Defaultsortattribute class:
[AttributeUsage( AttributeTargets.Class |
AttributeTargets.Struct )]
public class DefaultSortAttribute : System.Attribute
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public DefaultSortAttribute( string name )
{
_name = name;
}
}