C # Learning Generics

Source: Internet
Author: User

    1. What generic type?

      Generics are a feature of programming languages. Allows programmers to define variable parts when writing code in a strongly typed programming language, which must be specified before they are used. The various programming languages and their compilers and operating environments have different support for generics. Parameterization of the type to achieve code reuse a data type that improves the productivity of software development. A generic class is a reference type, a heap object, and the concept of a type parameter is introduced primarily.

    2. Why use generics?

      Usually we encounter this situation in the development: A method to implement the function is the same (for example, to save an object), but we pass the parameter type is not the same, if in general will copy a method to change the type of parameter passed to the desired type.

      The code is as follows:


Student entity class public class student    {         public string stuno { get; set; }         public string Name { get; set; }         public string Sex { get; set; }         public string Phone { get; set; }         public string Addr { get; set; }    }     //Teacher entity class public class teacher    {         public string name { get; set; }//Teacher Name          PUBLIC STRING DEPARTMENT { GET; SET; }//Affiliated Departments          public string title { get; set; }//title          //... Omit     }    //student operator Interface public interface studentdao     {        public void save (Student s);     }//Teacher Operator Interface public interface teacherdao    {         public void save (teacher t);    }     //Student Interface Implementation  class StudentDaoImpl:StudentDao    {         public void save (student s)          {            isession session  = factory. Opensession ();             itransaction tx;            try {             tx = session. BeginTransaction ();             session.save (s);             tx. Commit ();            } catch  (Exception  e)  {            if  (tx !=  null)  tx. Rollback ();            throw;             } finally              {             Session. Close ();             }        }    }//Teacher Interface Implementation  class TeacherDaoImpl:TeacherDao    {         public void save (teacher t)         {             isession session = factory. Opensession ();            itransaction tx;             try {             tx = session. BeginTransaction ();             session.save (t);             tx. Commit ();            } catch  (ExceptioN e)  {            if  (tx !=  null)  tx. Rollback ();            throw;             } finally              {             Session. Close ();            }         }    }

The above save method is mostly the same, in addition to the entity parameter types passed in, the basic operation of the entity (increase and deletion) can be implemented through the generic interface, without the implementation of each entity operation interface to write again repeated code, which greatly improve the efficiency of programming, and code reuse rate. What should be done about the above example? Take a look at my code below:

Basic Universal Interface public interface daosupport<t>     {     Void save (t entity);     }//generic interface implementation Public class basedaoimpl<b>: Basedao<b>{    public void save (b b)     {         isession session = factory. Opensession ();            itransaction tx;             try {             tx = session. BeginTransaction ();             session.save (t);             tx. Commit ();            } catch  (Exception  e) &NBSP;{&Nbsp;           if  (Tx != null)  tx. Rollback ();            throw;             } finally              {             Session. Close ();             }    }     }

In this way, Teacherdao,studentdao can perform interface inheritance Daosupport and Teacherdaoimpl,studentdaoimp inheritance interface implementation Class Daosupportimpl class can be common to those functional methods. is not very simple, very useful?!

Teacher Interface class public Interface teacherdao<teacher>:D aosupport<teacher>{////here can be a custom implementation of teacher-specific operations}// Teacher interface Implementation public class teacherdaoimpl<teacher>:D aosupportimpl<teacher>,teacherdao<teacher>{// This can be done by the teacher-specific operation of the custom implementation of}//student interface public interface studentdao<student>:D aosupport<student>{//Here can be customized for student-specific operations }//Student Interface Implementation public class studentdaoimpl<student>:D aosupportimpl<student>,studentdao<student>{// This allows you to customize the class-specific operations for students}


Summarize:

1. Generics are similar to a type placeholder (which is understood by itself), and no detailed declaration is made in the interface, just a position for the type

2. When you change the parameter T to the type of the object you want to manipulate, then the operation class is the instance of the class that is being manipulated. Greatly improves programming efficiency and code reuse.

3.DaoSupport The method is placed in any one type of entity objects have operations, such as basic additions and deletions, is those can be common method code extracted into the common base interface class.

4. If the business needs are not met in the methods implemented in Daosupport, you can customize your own unique approach in the specific entity action class. For example: Teachers in the category of multi-conditional query and so on.

This article is from the "Love Coffee" blog, please be sure to keep this source http://4837471.blog.51cto.com/4827471/1565233

C # Learning Generics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.