Skip to content

Latest commit

 

History

History
258 lines (224 loc) · 6.12 KB

2. Classes and Interfaces.md

File metadata and controls

258 lines (224 loc) · 6.12 KB

4 Classlar

4.0 Nomlash

Xizmatlar(service) va vositachilarni(broker) ifodalovchi sinflar(class) Standartga amal qilingan arxitekturada tiplarni nomlashdagi qoidalariga binoan ifodalashi kerak, ammo bu qoidalar modellar uchun amal qilinmaydi.

4.0.0 Modellar

Tavsiya etiladi
class Student {
	...
}
Tavsiya etilmaydi
class StudentModel {

}

4.0.1 Xizmatlar(Services)

Birlikda, biznes logikani o'z ichiga olgan istalgan sinf(class) uchun

Tavsiya etiladi
class StudentService {
	....
}
Tavsiya etilmaydi
class StudentsService{
	...
}
Bu holatda ham tavsiya etilmaydi
class StudentBusinessLogic {
	...
}
Bu holatda ham tavsiya etilmaydi
class StudentBL {
	...
}

4.0.2 Brokerlar

Birlikda, sizning xizmatlaringiz(service) va tashqi resurslar orasini ajratuvchi istalgan sinf uchun

Tavsiya etiladi
class StudentBroker {
	....
}
Tavsiya etilmaydi
class StudentsBroker {
	...
}

4.0.3 Controllerlar

Ko'plikda, oxirgi nuqtalar(endpoint)larni aks ettirishda, masalan RESTful amallar orqali dastur mantig'ini(logic) ifodalovchi api/students kabi

Tavsiya etiladi
class StudentsController {
	....
}
Tavsiya etilmaydi
class StudentController {
	...
}



4.1 Maydonlar

Maydon sinf(class) va tuzilma(struct) ichida e'lon qilingan istalgan tipdagi o'zgaruvchi

4.1.0 Nomlash

Sinf(class) maydonlari(field) camel case usulida nomlanadi.

Tavsiya etiladi
class StudentsController {
	private readonly string studentName;
}
Tavsiya etilmaydi
class StudentController {
	private readonly string StudentName;
}
Bu holatda ham tavsiya etilmaydi
class StudentController {
	private readonly string _studentName;
}

Nomlashda O'zgaruvchilar(Variable) bo'limidagi aytilgan qoidalarga amal qilish kerak


4.1.1 Bog'lanish

Classning private maydoniga yo'llanma berayotganda classning private a'zosini scoped metodlar va konstruktor miqyosidagi o'zgaruvchilardan farqlash uchun this kalit so'zidan foydalaning.

Tavsiya etiladi
class StudentsController {
	private readonly string studentName;
	
	public StudentsController(string studentName) {
		this.studentName = studentName;
	}
}
Tavsiya etilmaydi
class StudentController {
	private readonly string _studentName;

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



4.2 Instantiations

4.2.0 Input Params Aliases

Agar kiruvchi o'zgaruvchilar nomi parameter nomi bilan to'g'ri kelsa to'g'ridan-to'g'ri o'zgaruvchilarni ishlating. Agar aksincha bo'lsa parameter nomi bilan ishlatishingiz kerak, xusuan qiymatlar bilan.

Tavsiya etiladi
int score = 150;
string name = "Josh";

var student = new Student(name, score);
Bu holatda ham tavsiya etiladi
var student = new Student(name: "Josh", score: 150);
Tavsiya etilmaydi
var student = new Student("Josh", 150);
Bu holatda ham tavsiya etilmaydi
Student student = new (...);

4.2.1 Xossalar(Property) ketma-ketligiga amal qilish

Sinfdan(class) namuma(instance) yaratayotganda xossalar berishdagi ketma-ketlik sinf(class)ni e'lon qilgandagi xossalar ketma-ketligi bilan bir xil bo'lishiga amin bo'ling

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());