Skip to content

Latest commit

 

History

History
246 lines (209 loc) · 3.93 KB

2. Classes and Interfaces.md

File metadata and controls

246 lines (209 loc) · 3.93 KB

4 Classes

4.0 Naming

Classes that represent services or brokers in a Standard-Compliant architecture should represent the type of class in their naming convention, however that doesn't apply to models.

4.0.0 Models

Do
class Student {
	...
}
Don't
class StudentModel {

}

4.0.1 Services

In a singular fashion, for any class that contains business logic.

Do
class StudentService {
	....
}
Don't
class StudentsService{
	...
}
Also, Don't
class StudentBusinessLogic {
	...
}
Also, Don't
class StudentBL {
	...
}

4.0.2 Brokers

In a singular fashion, for any class that is a shim between your services and external resources.

Do
class StudentBroker {
	....
}
Don't
class StudentsBroker {
	...
}

4.0.3 Controllers

In a plural fashion, to reflect endpoints such as /api/students to expose your logic via RESTful operations.

Do
class StudentsController {
	....
}
Don't
class StudentController {
	...
}



4.1 Fields

A field is a variable of any type that is declared directly in a class or struct. Fields are members of their containing type.

4.1.0 Naming

Class fields are named in a camel cased fashion.

Do
class StudentsController {
	private readonly string studentName;
}
Don't
class StudentController {
	private readonly string StudentName;
}
Also, Don't
class StudentController {
	private readonly string _studentName;
}

Should follow the same rules for naming as mentioned in the Variables sections.


4.1.1 Referencing

When referencing a class private field, use this keyword to distinguish private class member from a scoped method or constructor level variable.

Do
class StudentsController {
	private readonly string studentName;
	
	public StudentsController(string studentName) {
		this.studentName = studentName;
	}
}
Don't
class StudentController {
	private readonly string _studentName;

	public StudentsController(string studentName) {
		_studentName = studentName;
	}
}



4.2 Instantiations

4.2.0 Input Params Aliases

If the input variables names match to input aliases, then use them, otherwise you must use the aliases, especially with values passed in.

Do
int score = 150;
string name = "Josh";

var student = new Student(name, score);
Also, Do
var student = new Student(name: "Josh", score: 150);
But, Don't
var student = new Student("Josh", 150);
Also, Don't
Student student = new (...);

4.2.1 Honoring Property Order

When instantiating a class instance - make sure that your property assignment matches the properties order in the class declarations.

Do
public class Student
{
	public Guid Id {get; set;}
	public string Name {get; set;}
}

var student = new Student
{
	Id = Guid.NewGuid(),
	Name = "Elbek"
}
Also, Do
public class Student
{
	private readonly Guid id;
	private readonly string name;

	public Student(Guid id, string name)
	{
		this.id = id;
		this.name = name;
	}
}

var student = new Student (id: Guid.NewGuid(), name: "Elbek");
Don't
public class Student
{
	public Guid Id {get; set;}
	public string Name {get; set;}
}

var student = new Student
{
	Name = "Elbek",
	Id = Guid.NewGuid()
}
Also, Don't
public class Student
{
	private readonly Guid id;
	private readonly string name;

	public Student(string name, Guid id)
	{
		this.id = id;
		this.name = name;
	}
}

var student = new Student (id: Guid.NewGuid(), name: "Elbek");
Also, Don't
public class Student
{
	private readonly Guid id;
	private readonly string name;

	public Student(Guid id, string name)
	{
		this.id = id;
		this.name = name;
	}
}

var student = new Student (name: "Elbek", id: Guid.NewGuid());