C# Best Practices - Define Proper Classes

时间:2021-12-19 03:50:34

Application Architecture

C# Best Practices - Define Proper Classes

Define the components appropriately for the application and create project for each one.

C# Best Practices - Define Proper Classes

What is Class?

3 Things: Visual Things, Business Things (Domain Entities), Application Things (like Logging & Email Generation)

3 Types: User interface classes, Domain entity classes, Library class

Class is a template or mold,specifies the traits or data,specifies the behavior or operations.

类是一种模板或模具,指定了具体数据或者是行为操作等。

Unit Testing

3 Advantages:

Higher Code Quality, Faster and Easier Debugging, Repeatable

Features:

Tests the behavior of a unit of code (often a method), Automated, Defined with code,Identifies errors

Tools:

MSTest, NUnit

Steps:

1.Define the test scenarios, 2.Generate the tests, 3.Execute the tests

Arrange -> Act -> Assert

FAQ

1.Why is a layer architecture important?

Logical components are easier to create,change,extend and maintain

Code are easier to reuse

2.What is a class?

A template for the objects created at runtime

Specifies the data and operations for each entity

3.What are the benefits of unit testing?

Higher quality code,faster and easier debugging,and they are repeatable over the life of the application

类定义中的访问修饰符

无或internal:只能在当前项目中访问类

public:可以在任何地方访问类

abstract或internal abstract:不能实例化,只能供继承之用(只能在当前项目中访问)

public abstract:同上,但可以在任何地方访问

sealed或internel sealed:不能供派生之用,只能实例化(只能在当前项目中访问)

public sealed:同上,但可以在任何地方访问

接口和抽象类

类似:

都不能实例化,都包含可以由派生类继承的成员。

区别:

1、派生类只能继承一个基类,但类可以继承任意多个接口。

2、抽象类的成员可以拥有代码体(即代码实现),而接口成员没有代码体,都必须在使用接口的类上实现。

3、接口成员是公共的,而抽象类的成员可以是private, protected, internal or protected internal。

4、接口不能包含字段、构造函数、析构函数、静态成员或常量。