DTML C# Coding guidelines

All code should follow SOLID Principles and require a code review before being merged into the main branch.

Couple high level guidelines which are absolutely must haves for DTML code:

Logging and telemetry.

We use log4net logger. Make sure all components have a single instance and initialized with correct type:

readonly ILog logger = LogManager.GetLogger(typeof(LearnPathController));
  1. Throw exceptions rather than returning status value.

  2. Log all exceptions or possible error conditions.

  3. Be conservative with logging warnings and info messages. If you need to log BI event use notificaitonManager class rather than logger.

NotificationManager is asynchronous event processing system based on Azure Service Bus.

 await notificaitonManager.Value.RecordEvent("New User Registered", 
                                              EventType.Registration, 
                                              User.Identity.GetUserId());

Use notification manager only for BI events not captured by client side Google Analytics.

Dependency injection

We use structure map for our Dependency Injection. Do not create instances of the classes directly. We use constructor based injection. Follow below pattern and Structure Map will resolve it for you.

 public ExampleController(INotificationManager notificationManager)
 {
    this.notifications = notificationManager;
 }
  1. Do not use StructureMap.Resolve() in code

  2. Do not create instances directly

Code structure

Controllers should be very lightweight and you should never have data access code in the controller. All data access should happen in Repositories. New repositories should be created under DTML.Common SharedRepositories folder.

  1. Code against interfaces.

  2. Favor composition over inheritance

Views

  1. No inline CSS styles in views. Use CSS classes.

  2. No inline JavaScript in views. Place all JavaScript into separate files and make sure to update bundles in BundleConfig to crunch both CSS and JS.

  3. Be aware of image sizes. Make sure to compress and minimize images

Last updated