Summary of web api knowledge and webapi knowledge in the project

Source: Internet
Author: User

Summary of web api knowledge and webapi knowledge in the project

Recently, I am working on a project in the company. I am responsible for setting up and developing the webapi framework. I am very busy recently and have no time to summarize the knowledge points I encountered at work, it is an improvement for yourself. If my personal tips and summary can help Boyou, it is also good.

(1) First, let's talk about c # dynamic. dynamic is a new feature of FrameWork4.0. I have used a lot of dynamic in the api. First of all, I personally think that dynamic can make the code more elegant, and can save the need to define Redundant entities. dynamic is the runtime to determine the attributes of the type, attributes can be randomly specified, including values and values,

For example:

dynamic sourceInfo = SqlDataHelper.GetWebDMDynamicData(sourceIdSql, new {id=3).FirstOrDefault();                    if (null != sourceInfo)                    {                        item.SourceId = sourceInfo.SourceId;                        item.DocType = sourceInfo.DocType;                        item.InvestmentType = sourceInfo.InvestmentType;                    }

In the above Code, sourceInfo specifies dynamic, and the developer knows that if the value is successful, the SourceId attribute will certainly exist, so it is not surprising that item. SourceId = sourceInfo. SourceId; Value assignment.

The following code can also specify attributes for dynamic as needed:

1 dynamic data= null;2 data= SqlDataHelper.GetWebDMDynamicData(sql, new { Id = id }).ToList().FirstOrDefault();3 data.a= 124;4 data.b= "test2";5 data.c="test3";

You can specify attributes for data of the dynamic type unless the data value in step (2) is null. Otherwise, the data value in step (3) is null, at this time, a null exception will occur.

(2) Let's Talk About the lightweight ORM framework. This framework has been filled with a lot of learning materials on the Internet. I just want to summarize my usage experiences in the project. If you are not interested, please correct me.

We know that there must be parameterization in calling the database storage process. dapper provides a good parameterized object DynamicParameters. The code above:

DynamicParameters dynamicParameters = new DynamicParameters();            dynamicParameters.Add("@p_Id1", id1, DbType.String);            dynamicParameters.Add("@p_Id2", id2, DbType.Int32);            dynamicParameters.Add("@p_dateTime", dateTime, DbType.DateTime);            List<dynamic> info =  conn.Query<T>(storeProcedureName,dynamicParameters, null, true, null, CommandType.StoredProcedure) as List<T>;

If you want to specify the execution of the stored procedure, you need to specify the CommandType. StoredProcedure keyword.

(3) The following is the List <T> able conversion method, which saves a lot of code for type conversion, but there is also a problem that has not been solved by yourself, that is, T must specify the object type, but cannot use dynamic. Otherwise, type. getProperties

If the correct attribute is not obtained, the system must define one more entity class to pass in.

 public static DataTable ToDataTable<T>(IList<T> list)        {            //create propertyInfo            List<PropertyInfo> pList = new List<PropertyInfo>();            //get reflector interface            Type type = typeof(T);            DataTable dt = new DataTable();            //add datatable column            Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, Nullable.GetUnderlyingType(            p.PropertyType) ?? p.PropertyType); });            foreach (var item in list)            {                //create new row                DataRow row = dt.NewRow();                pList.ForEach(p => row[p.Name] = p.GetValue(item, null)??System.DBNull.Value);                dt.Rows.Add(row);            }            return dt;        }

Well, the above is a small summary of the project, and there are still some fragmented knowledge points that are not described too much. There are still a lot of capabilities to learn in the project process, especially the communication and technical guidance of business capabilities.

Related Article

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.