Entity Framework: The initial experience of WCF

Source: Internet
Author: User

before writing, this is something that was occasionally written in February, recently, I have been busy with MVC applications. I don't have time to sort them out, so I can sort them out during the holidays for better communication.

when I was not familiar with WCF, on the EF design team blog and the adodotnetentityframework community , I often see some topics about the application of EF in the WCF scenario and Article . Because EF has been very concerned, I am very curious to know how it is applied in WCF. I want to find some Code examples on the Internet, but there is nothing to gain. Then, when learning WCF, go to New WCF features in. net 3.5 SP1 I learned that WCF fully supports poco (support for ADO. NET Entity Framework entities in WCF contracts although I know that Poco is defined as plain old C # object, I have not figured out what poco cannot be understood and what is POCO ), this makes me more impulsive to try, but after I finish writing the test method, I use the wcftestclient tool as an example to call it. I can find that it cannot be called, and my contract is declared as follows:

Figure 1

[Operationcontract]

String getcategorybyid (INT categoryid );

[Operationcontract]

Northwind. Categories getcategorybyid1 (INT categoryid );

[Operationcontract]

Void getcategorybyid2 (INT categoryid, out northwind. Categories OBJ );

[Operationcontract]

String dowork ();

[Operationcontract]

Student getstudent (string ID)

of course, for custom DTO objects (getperson (), dowork (), the call is normal. Add a console client and create a proxy to call the above method. getstudent () is normal and the ef-related method is called, but an error is reported, and I do not know how to debug it, when wcftestclient is used. you can debug the SVC service directly. These questions not only make me depressed, but also make me very strange. Why? Where did I do wrong? Later, I found a besocia open-source project in codeplex (about EF and WCF). However, I opened the code and found that the traditional method was used, and the custom transmission object was completed, but that's not what I want to see), but I thought it was because of the returned value, so I defined getcategorybyid (), getcategorybyid1 (), getcategorybyid2 () three methods with different return types, and 1 is displayed under wcftestclient.

However, due to work reasons, I had no idea about this. In the new company, I took over the work left by my predecessors and gradually began to access the application environment of Silverlight + WCF + LINQ to SQL, seeing those DTO objects defined by the predecessors, I secretly felt that the DTO object defined for the WCF transmission was really a troublesome process, and later in zeeshanhirani (http://weblogs.asp.net/zeeshanhirani) I saw an article about entityref in the discussion of LINQ to SQL, it is mentioned that you can set the serialization mode of datacontext to unidireal Al to enable the entities generated by LINQ to SQL to be able to serialize in WCF (you can clearly see [datacontract ()] Before object classes). tags), and this feature is enhanced in EF, that is, the entity generated by EF has its own serialization capability. After learning about this, I found out the previous exercises and saw the application of WCF on EF on stackoverflow. I am very depressed why I have the error in Figure 1, I tried to write a client in the console to call it. Actually, the getcategorybyid1 () operation was successful (I called proxy in this way. getcategorybyid1 (1); no error), it seems that the previous failure was caused by laziness. In EF, [Global: system. runtime. serialization. datacontractattribute (isreference = true)], [Global: system. runtime. serialization. datamemberattribute ()], these two tags are used to support WCF transmission. Of course, this is what I realized later. So after work, I started to go over again. In the Silverlight forum, I saw some people discussing the application of Silverlight + WCF + Entity Framework. Some people encountered the same problems as they encountered, no one asked me why, but I still got some suggestions, such as adding

<System. Web>

<Compilation DEBUG = "true"> </compilation>

</System. Web> to start remote debugging. Debugging is much easier.

 

The following are some of the most common features of EF in WCF. At least I will compare it with LINQ to SQL and EF, because when the serialization mode is set to unidirectional, in WCF, the application is no different from the custom DTO. I am just a cainiao, but I just like it, so I can only list it;

1.The EF object type cannot be directly used on the WCF client: the transfer object defined for the WCF is usually serialized in servicereference, and getcategorybyid1 () is called on it (), I want to assign the returned result to a categories object. However, no entity types related to EF can be found in categoryreference (except for custom student objects ), I even used the svcutil tool in the generated. the axd file does not display the definition, so we have to return the result to VaR, such as VAR cc = proxy. getcategorybyid1 (1); C = cc. categoryname;, do you have to manually add reference to the edmx file? At least I am doing this now, but it feels a little strange,

However, this ensures the consistency between the server and client entities. It seems that DTO is analyzed in the n-tier-improvements-for-entity-framework article on efdesign blog, dataset has advantages and disadvantages in application scenarios. Specifically, DTO is completely independent on the client and does not depend on the server. This constitutes a trust boundary and is suitable for different companies, EF can be applied to multi-layer applications with full trust, which is not necessarily correct, but can be inspired by me..

2. out, the returned ref parameter will be replaced (I do not know how to describe it), as shown in the preceding example void getcategorybyid2 (INT categoryid, out northwind. categories OBJ); I'm using proxy .. getcategorybyid2 (), which is clearly two parameters, is changed to one I view. I view the definition of getcategorybyid2 in categoryreference:

Public northwind. Categories getcategorybyid2 (INT categoryid ){

Return base. Channel. getcategorybyid2 (categoryid );

} It is basically the same as getcategorybyid1, but I tried it later using LINQ to SQL. This is also the case for custom DTO. It seems that this is the internal processing of WCF.

3. object graphs (I do not know how to call it). Someone asked this topic in the stackoverfolw community and the Silverlight forum. in net3.5sp1, EF is fully supported. I also conducted a small test:

 [Operationcontract]

Northwind. Orders getorderbyid1 ();

[Operationcontract]

Northwind. Orders getorderbyid ();

Public northwind. Orders getorderbyid1 ()

{

Using (northwind. northwindefentities context = new northwind. northwindefentities ())

{

Return context. Orders. firstordefault <northwind. Orders> ();

}

}

Public northwind. Orders getorderbyid ()

{

Using (northwind. northwindefentities context = new northwind. northwindefentities ())

{

Return context. Orders. Include ("orderdetails"). firstordefault (); // <northwind. Orders>

}

}

Client:

Orderreference. orderscontractclient proxy = new client. orderreference. orderscontractclient ();

VaR order = proxy. getorderbyid ();

Console. writeline ("orderdetailes {0}", order. orderdetails. Count );

Order = proxy. getorderbyid ();

Console. writeline ("orderdetailes {0}", order. orderdetails. Count );

 

4. curd: In curd exercises, the most emotional thing is the update of entities. In fact, this is also the lack of ef1.0 in n-layer applications. It was published in the magazine of Ms December.Flexible data access with LINQ to SQL and the Entity Framework,N-tier improvements for Entity Framework

In the two articlesEF has been discussed in n-layer applications. The most difficult part of EF in n-layer applications is that when updating an object, the original value (with the object before the new version) and the current value (the current object) must be given.ProgramWe do not know that the attributes of these objects have been updated. We can only update the attributes of each current object compared to the original values. Obviously, object clone is also a solution. Of course, if you use entitydatasource, you will not have to worry about it. Another problem is concurrency. Of course, this is quite clear for me now. Of course, in the previous blog of dsimmons, it was namedAttachasmodified-a small step toward simplifying ef n-tier patterns article seems to provide a good solutionTo understand some comments of dsimmonss, I have read more than once the articles mentioned above. In my web project (MVC + EF), I will write this part in detail later.

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.