Writing C# Rules
C# is a programming language that creates robust and secure applications for Windows, web, mobile, and other platforms. C# applications are typically developed using Microsoft's .NET framework, which provides a large library of pre-built functionality and components and a development environment for creating custom applications.
C# applications can be developed in various ways, from a basic console application to fully featured enterprise applications. They can be used for various purposes, from simple web games to complex software solutions.
Restrictions
All code must be contained within a class.
All code must be contained within a namespace.
All identifiers (variables, types, methods, classes, etc.) must be named using Pascal casing.
All code must be surrounded by a pair of curly braces.
All code must be encapsulated within one or more classes.
All code must be written using proper indentation and whitespace.
All variables must be declared before they are used.
All variables must be declared with an appropriate data type.
All classes must be declared using the sealed keyword unless they are meant to be extended.
All classes must be declared with the public access modifier unless otherwise specified.
All methods must be declared using the correct access modifier.
All methods must have a return type, even if it is void.
All method parameters must be declared with the correct data type.
All variables must be assigned a value.
All string literals must be enclosed in double-quotes.
All boolean literals must be written using the keywords true and false.
Best Practices
Use meaningful names: When naming variables, methods, parameters, and classes in your code, ensure they reflect what they are used for. This will make it much easier to understand the code and help to prevent confusion and errors.
Use comments: Adding comments to your code can be very helpful in understanding the code and ensuring it is maintainable. Comments should be used to explain the purpose of a code section, provide information about complex sections of code, or provide a reminder of something.
Keep it simple: Ensure your code is as basic and straightforward as possible. It should be easy to read, understand, and maintain. Complex and redundant code can be difficult to debug and maintain, so it is important to keep things simple.
Don’t repeat yourself: When writing code, try to avoid repeating yourself. Don’t write the same code multiple times throughout a program. Instead, use functions and methods to reduce redundancy and keep your code clean and organized.
Test your code: Before you deploy your code, make sure you test it thoroughly. This will help to ensure that the code is working as intended and that all bugs are fixed before it is released.
Refactor when necessary: Refactoring is the process of restructuring code to make it easier to maintain. It is important to refactor code when needed to keep it organized and readable.
Use resources: Make sure you take advantage of the available resources. Look up documentation, tutorials, and best practices to write the best code possible.
Sample Code
Below is an example of a C# business rule that can be used to create a function:
C# Rule Example
var token = request.AsString("token");
var domain = request.AsString("domain");
var cmsUrl = request.AsString("cmsUrl");;
//Validate required parameters here
ASF.Framework.Util.ValidationHelper.ValidateNotNullString(token, "token");
ASF.Framework.Util.ValidationHelper.ValidateNotNullString(domain, "domain");
ASF.Framework.Util.ValidationHelper.ValidateNotNullString(cmsUrl, "cmsUrl");
var resource = new ASF.CoreLib.Messages.GetCmsResourceResponse();
try
{
//get resource
resource = ASF.CoreLib.APIHelper.GetCmsResource(new ASF.CoreLib.Messages.GetCmsResourceRequest()
{
Domain = domain,
Token = token,
Url = cmsUrl
});
if(!string.IsNullOrEmpty(resource.ErrorMessage))
{
ASF.Framework.Util.LogHelper.Error(ASF.CoreLib.APIHelper.Logger, resource.ErrorMessage);
throw new InvalidOperationException("Unhandled Error");
}
else
{
resource.Resource.Stream.Seek(0, System.IO.SeekOrigin.Begin);
using (System.IO.StreamReader sr = new System.IO.StreamReader(resource.Resource.Stream))
{
response["outputText"] = sr.ReadToEnd();
}
}
}
catch (Exception e)
{
ASF.Framework.Util.LogHelper.Error(ASF.CoreLib.APIHelper.Logger, "Unhandled Error" + e);
response.Result.AddParameter(new ASF.Framework.Service.Parameters.Parameter { Name = "Error", Value = e.ToString() });
}
finally
{
resource.Resource.Stream.Close();
}