Back to directory
Generic method: it is an abstract concept that abstracts batch operations that have common characteristics. generic types are used to represent this method. Methods of these types share the same logic, the only difference is that their type, that is, the type is a variable in the generic method, which is expressed in the lung!
During Development today, I encountered this problem and finally reconstructed my code. After the reconstruction, I used the generic method and felt that the code was much more beautiful.
Before using the generic Method
/// <Summary> /// update the relationship between the instructor and the student /// </Summary> /// <Param name = "list"> list of links to be inserted </ param> // <Param name = "isall"> whether it is all, if all, you do not need to insert a </param> // <Param name = "teacherid"> current instructor id </param> // <Param name = "type"> type to the relational table: 0 video, 1 job, 3 Documents </param> Public void adduser_source_r (list <user_source_r> list, bool isall, int teacherid, int objid, int objtype) {Switch (objtype) {Case 0: var respository1 = loadrepository <classroom_info> (); var entity1 = loadrepository <classroom_info> (). find (objid); If (isall) {entity1.accessstatus = 0; respository1.update (entity1);} else {entity1.accessstatus = 1; entity (entity1); loadrepository <user_source_r> (). insert (list);} break; Case 1: var respository2 = loadrepository <courseware_info> (); var entity2 = loadrepository <courseware_info> (). find (objid); If (isall) {entity2.accessstatus = 0; respository2.update (entity2);} else {entity2.accessstatus = 1; entity (entity2); loadrepository <user_source_r> (). insert (list);} break; Case 2: var respository3 = loadrepository <task_info> (); var entity3 = loadrepository <task_info> (). find (objid); If (isall) {entity3.accessstatus = 0; respository3.update (entity3);} else {entity3.accessstatus = 1; entity (entity3); loadrepository <user_source_r> (). insert (list);} break; Case 3: var respository4 = loadrepository <substance_info> (); var entity4 = loadrepository <substance_info> (). find (objid); If (isall) {entity4.accessstatus = 0; respository4.update (entity4);} else {entity4.accessstatus = 1; entity (entity4); loadrepository <user_source_r> (). insert (list) ;}break; default: Throw new argumentexception ();}}After using the generic method
/// <Summary> /// update the relationship between the instructor and the student /// </Summary> /// <Param name = "list"> list of links to be inserted </ param> // <Param name = "isall"> whether it is all, if all, you do not need to insert </param> /// <Param name = "teacherid"> current instructor id </param> /// <Param name = "type"> resource Type 0 course, 1 video, 2 job, 3 Documentation </param> Public void adduser_source_r (list <user_source_r> list, bool isall, int objid, int objtype) {Switch (objtype) {Case 0: updatesource_r <classroom_info> (list, isall, objid); break; Case 1: updatesource_r <courseware_info> (list, isall, objid); break; Case 2: updatesource_r <task_info> (list, isall, objid); break; Case 3: updatesource_r <substance_info> (list, isall, objid); break; default: throw new argumentexception () ;}/// <summary> /// generic method, only different types of things: // </Summary> /// <typeparam name = "tentity"> </typeparam> /// <Param name = "list"> </ param> /// <Param name = "isall"> </param> // <Param name = "teacherid"> </param> // <Param name =" objid "> </param> // <Param name =" objtype "> </param> void updatesource_r <tentity> (list <user_source_r> list, bool isall, int objid) Where tentity: Class, iaccess {var entity = loadrepository <tentity> (). find (objid); If (isall) {entity. accessstatus = 0; loadrepository <tentity> (). update (entity);} else {entity. accessstatus = 1; loadrepository <tentity> (). update (entity); loadrepository <user_source_r> (). insert (list );}}
We can see that generic methods abstract unchanged logic together, which facilitates code extension and maintenance. This is the object-oriented code!
Back to directory
The foundation is the top priority ~ Now we should use the generic method.