@manhnguyenv

Welcome to my blog!

Design Principles

December 20, 2018 20:48

Design Principles (edit)

Best Practices C# inurl:microsoft site:microsoft.com inurl:best-practice-architecture

Conceptual Overview

Design Principles

Design Patterns

https://channel9.msdn.com/

https://www.pluralsight.com/

Common web application architectures

https://docs.microsoft.com/en-us/dotnet/standard/modern-web-apps-azure-architecture/common-web-application-architectures

4 Best Practices for Microsoft .NET Framework and Applications

https://www.roberthalf.com/blog/salaries-and-skills/4-best-practices-for-microsoft-net-framework-and-applications

2019 Technology Salary Guide

https://www.roberthalf.com/sites/default/files/documents_not_indexed/2019_Salary_Guide_Technology_NA.pdf

Microsoft Visual C# Step by Step, 9th Edition

https://www.microsoftpressstore.com/store/microsoft-visual-c-sharp-step-by-step-9780134850788

Mock Frameworks vs Microsoft Fakes

https://saucelabs.com/blog/mock-frameworks-vs-microsoft-fakes

Free eBooks from Microsoft Press

https://mva.microsoft.com/ebooks

The Developer’s Guide to Azure

https://azure.microsoft.com/en-us/campaigns/developer-guide/?v=18.45

Azure Architecture Guide

https://docs.microsoft.com/en-us/azure/architecture/guide/

Case study code on GitHub

https://www.pluralsight.com/guides/asp-net-mvc-creating-solutions-with-separate-projects-for-entities-data-access-and-website-functionality

Case study code on GitHub

https://github.com/ajsaulsberry

The complete Visual Studio solutions described in this guide are available on GitHub:

https://github.com/ajsaulsberry/BlipDrop

https://github.com/ajsaulsberry/BlipProjects

You can fork the projects, run the code, and experiment on your own.

Note that the sample project is not intended to be a real life case study or production code; it exists to illustrate the topics covered in this guide.

ASP.NET Core Web API

https://github.com/cwoodruff/ChinookASPNETCoreAPINTier

API developed with ASP.NET Core 2.1 using Chinook DB. Developed with an Architecture for better unit testing.

InfoQ article covering this architecture located here: https://www.infoq.com/articles/advanced-architecture-aspnet-core

You can find the Chinook database here for the project https://github.com/cwoodruff/ChinookDatabase

Resources

Episodes:

Design Patterns and Groups

The following list shows some of the most common design patterns within these groups:

  • Presentation Logic
    • Model-View-Controller (MVC)
    • Model-View-Presenter (MVP)
    • Use Case Controller
  • Host or Behavioral
    • Command
    • Publish-Subscribe / Observer
    • Plug-in / Module / Intercepting Filter
  • Structural
    • Service Agent / Proxy / Broker
    • Provider / Adapter
  • Creational
    • Factory / Builder / Injection
    • Singleton
  • Persistence
    • Repository

Hire .NET Developer

· Demonstrated understanding of C#, ASP.NET, MVC and Microsoft SQL Server

· WCF Service Knowledge

· Bachelor's degree in Computer Science or Software Engineering

· 6+ years of recent experience as a developer

· Strong experience with MVC Framework, Visual Studio, and Microsoft SQL Server

· Strong experience in C# and .NET 4.5 Framework technologies and web APIs

· Strong client coding skills using Angular, JavaScript, jQuery, CSS, HTML, and Bootstrap

· Experience with .NET and Java script Unit Testing

· Design and develop WCF Data Services

· Develop web-based applications and services using various .Net framework and SQL server

· Design and develop the UI using MVC

MICROSOFT ADVANCED FRAMEWORKS

Sharpen your C# skills and learn to write beautiful code.

The Microsoft Advanced Frameworks is a free program addressed to students in the final year of their studies who have completed our C# Remote Learning program.

What will you learn?

This foundation course focuses on design principles, best practices, technology core elements. It covers topics such as:

  • Classes, objects, structures, execution control, array class, fields, methods, visibility modifiers, interfaces
  • Finalizers, Disposable objects, I/O in C#
  • Delegates,events, lambda expressions, Linq
  • Collections and generics
  • Error handling with exceptions & common errors, collections, searching, modifying and encoding text; reflection; serialization
  • Network Sockets, Multithreading
  • Entity framework
  • Asp.net WEB API basic functionality
  • Logging, refactoring
  • Clean code

C# Language Best Practices

https://social.technet.microsoft.com/wiki/contents/articles/38011.c-language-best-practices.aspx

Best Practices for Strings in C#

https://docs.microsoft.com/en-us/dotnet/standard/base-types/best-practices-strings

Best Practices for Developing World Application

https://docs.microsoft.com/en-us/dotnet/standard/globalization-localization/best-practices-for-developing-world-ready-apps

Best Practices for API Design

https://docs.microsoft.com/en-us/azure/architecture/best-practices/api-design

The API Checklist: https://mathieu.fenniak.net/the-api-checklist/

Best Practices for General Naming Conventions

https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/general-naming-conventions

Async/Await - Best Practices in Asynchronous Programming

https://msdn.microsoft.com/en-us/magazine/jj991977.aspx

Windows Communication Foundation Architecture

WCF Architecture

The WCF Architecture

To debug a service

  1. Build the solution (both client and service) using the Build menu or CTRL+SHIFT+B.

  2. If the service is hosted in IIS:

    1. Activate the service using a browser by entering the address http://localhost/servicemodelsamples/service.svc.

    2. In the solution, choose the Debug menu and the Attach to Process menu item.

    3. Select the Show processes from all users check box.

    4. Select the host worker process w3wp.exe to debug (select aspnet_wp.exe on Windows XP).

  3. You can now set breakpoints in the service code and enable breakpoints on exceptions.

  4. Right-click the client project item and choose Debug, Start new instance.

To clean up after the sample

  • If the service is hosted in IIS for security purposes, remove the virtual directory definition and permissions granted in the setup steps when you are finished with the samples.

One-time setup procedure for WCF sample

Download link here

1. How to set up ASP.NET, see Internet Information Service Hosting Instructions.

2. Ensure that .NET Framework version 4 is installed. Search the following directory for v4.0 (or later):  %WINDIR%\Microsoft.NET\Framework

"%WINDIR%\Microsoft.Net\Framework\v4.0.30319\aspnet_regiis" -i -enable
"%WINDIR%\Microsoft.Net\Framework\v4.0.30319\ServiceModelReg.exe" -r

Running the command aspnet_regiis –i –enable will make the Default App Pool run using .NET Framework 4, which may produce incompatibility issues for other applications on the same computer.

3. Follow the Firewall Instructions for enabling the ports used by the samples.

4. Open Visual Studio 2017, create the WCF Service project

5. Create a virtual directory in IIS named ServiceModelSamples

6. Make sure we have two folders %SystemDrive%\Inetpub\wwwroot\ServiceModelSamples and %SystemDrive%\Inetpub\wwwroot\ServiceModelSamples\bin

If you want to manually create Virtual Directory, see the Virtual Directory Setup Instructions.

You can learn the batch file with Setupvroot.bat batch file and Cleanupvroot.bat batch file.

You must grant permission to modify for %SystemDrive%\inetpub\wwwroot to the account under which you are building the samples (cmd > hostname) and the Network Service user.

You must run the command prompt as Administrator.

7. Make sure the NT Authority\Network Service account has write access granted to the c:\logs folder.

8. Run the Setupcerttool.bat batch file. 

This script will perform the following tasks:

  • Build the FindPrivateKey tool.

  • Create a directory called %ProgramFiles%\ServiceModelSampleTools.

  • Copy the new FindPrivateKey tool to this directory.

This tool is required by samples that use certificates and are hosted in IIS.

9. Note

For security purposes, remember to remove the virtual directory definition and permissions granted in the setup steps above by running the batch file named Cleanupvroot.bat after you are finished with the samples.

10. IIS Server Certificate

Some samples require certificates. See Internet Information Services (IIS) Server Certificate Installation Instructions.

Setup.exe and Cleanup.exe batch files and scripts should be run from Developer Command Prompt for Visual Studio. Several set up and clean up files perform tasks that require administrative privileges and should be launched with administrator privileges.

Firewall

To enable samples programs in advance

  1. Build the sample.

  2. Click Start, click Run, and type firewall.cpl. This opens the Windows Firewall Control Panel applet.

  3. On Windows Vista and Windows Server 2008, click Allow a program through Windows Firewall.

  4. On the Exceptions tab, click Add Program.

  5. Click the Browse button and select the executable file of the sample you plan to run.

  6. Repeat steps 3 and 4 until you have added the executable files of all the samples you plan to run.

  7. Click OK to close the firewall applet.

To enable a port range in advance

  1. Click Start, click Run, and type firewall.cpl. This opens the Windows Firewall Control Panel applet.

  2. On Windows Vista and Windows Server 2008, click Allow a program through Windows Firewall.

  3. On the Exceptions tab, click Add Port.

  4. Enter a name, enter port 8000, and select the TCP option.

  5. Click the Change Scope button, select the My Network (subnet) only option, and click OK.

  6. Repeat steps 2-4 for ports 8001, 8002, 8003, 9000, 80 (HTTP), and 443 (HTTPS/SSL).

  7. Click OK to close the firewall applet.

Tools

Resgen.exe (Resource File Generator)
Describes how to use Resgen.exe to convert .txt files and XML-based resource format (.resx) files to common language runtime binary .resources files.

Winres.exe (Windows Forms Resource Editor)
Describes how to use Winres.exe to localize Windows Forms forms.

See also

There are countless ways for developing applications using the Microsoft .NET framework. Here are four best practices cherry-picked by a developer with many years of experience creating applications built on .NET.

With a flexible development platform like the Microsoft .NET framework, there are a few key areas where you’ll want to apply best practices. I tapped Rion Williams, a longtime .NET developer, to help explain these areas. They include:

  • Coding practices and conventions
  • Design and architecture
  • Performance
  • Security

4 Best Practices for Microsoft .NET Framework and Applications

1. Coding Practices and Conventions

Following best practices when writing code in the Microsoft .NET framework allows you to create consistency and facilitate refactoring and maintenance. You also can avoid errors and exceptions.

Consider naming conventions. They may not affect performance, but they can often directly affect the readability of your code. Using variables with meaningful names can help you infer what is going on.

For example:

var carsPassed = 0;

is a clearer than:

var c = 0;

Every developer working with the Microsoft .NET framework has his or her own conventions. But there are some commonly accepted standards around naming, code layout, appearance and the like. The following resources can clue you in to these standards:

2. Design and Architecture

The biggest reason to incorporate best practices in the design and architecture of apps you create in the Microsoft .NET framework is to avoid errors and costly rewrites.

There are hundreds of patterns out there to help govern the design of your application. You’ve probably heard of design patterns with names like “MVC,” “Gang of Four,” “MVVM,” “Repository,” “Unit-of-Work” and “Singleton,” for example. All of these are widely used and each serves a different purpose. They can be used on their own or in conjunction with other patterns if your application calls for it.

Another common concept within the Microsoft .NET framework is N-Tier, or N-Layer, design. The most common of these for smaller applications is three-tier architecture. If you’re using a three-tier architecture for the first time or want to brush up, check out these resources:

The following additional resources can help you delve even deeper into different Microsoft .NET framework design and architecture patterns:

3. Performance

Looking for some instant gratification? Pay close attention to Rion’s suggestions for improving performance.

One of the best ways to identify possible areas of improvement is to use an analyzer to take a look at your code (or the Web Requests that are made from your application). Check out these analyzers for the Microsoft .NET framework:

  • ReSharper, says Rion, “is the single greatest tool available in the .NET development world.” It provides code suggestions for improvements as you develop your applications. The tool also offers code quality analysis, error elimination and code testing. ReSharper is available for purchase, but you can download a 30-day free trial before you buy.
  • EQATEC Profiler is a free tool that identifies areas for improvement and areas of weakness within your code.

Another tool Rion raves about is the Web Developer Checklist, which he describes as an excellent resource for improving web application performance.

4. Security

Some of the major concerns relating to security within applications built on the Microsoft .NET framework are SQL Injection and XSS (Cross Site Scripting) Injection. These attacks could cripple your data or web application if you aren’t properly protected, Rion says. By default, .NET implements its own measures to help prevent this from happening. But there are additional precautions you should take.

One example, Rion says, is the use of SQL Parameters as opposed to simply concatenating strings when building SQL queries.

If you are calling your Stored Procedure or query through ASP.NET and are simply concatenating the values of something like a TextBox with your SQL call that calls your query, that could make your code susceptible to SQL Injection.

You should always populate any values that are pulled from your application as SQL Parameters to ensure proper encoding. You never want to give users an opportunity to write their own SQL code (or inject it) into one of your queries, Rion cautions.

If Stored Procedures are an option within your Microsoft .NET framework application, consider using them. Stored Procedures are safer than using direct SQL code because they have this same functionality already integrated into them.

Here are some additional resources to learn more about Stored Procedures:

Armed with the Microsoft .NET framework best practices and resources mentioned above, you can develop more consistent, better performing and more secure applications.

Categories

Recent posts