Lập trình .net 4.0 và visual studio 2010 part 25 pps

7 173 0
Lập trình .net 4.0 và visual studio 2010 part 25 pps

Đang tải... (xem toàn văn)

Thông tin tài liệu

CHAPTER 7  WINDOWS COMMUNICATION FOUNDATION 172 HTTP Caching One of the biggest advantages to using RESTful services is that you can take advantage of HTTP caching features to improve performance and reduce load on the service. Caching wasn’t too easy to implement prior to WCF4 however it is very easy in WCF4 with the simple addition of the AspNetCache profile attribute to your methods: [AspNetCacheProfile("MyCachingProfile")] You then need to create a caching profile using the following configuration: <outputCacheSettings> <outputCacheProfiles> <add name=" MyCachingProfile " duration="30" varyByParam="format" varyByHeader="Accept" /> </outputCacheProfiles> </outputCacheSettings> Misc Changes What follows are a number of other enhancements that have been introduced in WCF4. Improved Integration with WF Microsoft has put a big focus on improving the integration between WF and WCF, and this release sees the introduction of a new project type called WF Service (a combined WCF and WF service represented entirely in XAML). Please refer to Chapter 6 for full details. Default Performance-Related Settings Changed A number of default settings for WCF services have been tweaked for better performance: • MaxConcurrentSessions was 10, and is now 100 x processor count. • MaxConcurrentCalls was 16, and is now 16 x processor count. • MaxConcurrentInstances was 26, and is now the total of MaxConcurrentSettions and MaxConcurrentCalls. Low Memory A new minFreeMemoryToActivateService option has been added that defines the percentage of memory that needs to be free in order for a service to be activated (defaulted to 5%). If less than the specified memory is available then WCF will throw an exception. CHAPTER 7  WINDOWS COMMUNICATION FOUNDATION 173 Other changes The following lists some other changes that have been made. • Event Tracing for Windows is now used for tracing offering better performance. • There’s a new DataContractResolver class that allows control over the serialization process of types (http://msdn.microsoft.com/en-us/library/system.runtime. serialization.datacontractresolver(VS.100).aspx). • There’s a new byte stream encoder (a new encoder for sending binary content over WCF—see samples again). • Automatic decompression over HTTP for client has been added (server not currently supported). • Extended protection of secure channels has been added (a new Windows feature that binds one security system to another making attacks harder). • The addition of non-destructive queue receive (an alternative way of processing messages that makes a queue item invisible to other consumers and creates a transaction for performing work on it that if rolled back will return the item to its place on the queue – for more info please refer to: http://blogs.msdn.com/drnick/ archive/2008/12/04/an-alternative-queuing-model.aspx). Dublin/Windows Application Server It is important to note that Microsoft are working on a new technology code named Dublin (which will probably become Windows Application Server when the marketing guys get their way) that aims to make it easy to deploy and manage your WCF and WF applications and services and will probably become the preferred means of hosting services. At the time of writing Dublin will ship as an add-in to Windows Server 2008 and will be contained in future versions of Windows as part of the application server role. Dublin is an extension to IIS and Microsoft say will provide the following functionality: • Allows easier deployment of workflow and services. • Provides an overview of service health and dashboard for monitoring services. • Has tools for easier management of services. • Comes with forwarding/routing functionality. • Has built-in tracking profiles for monitoring common events. Further reading • http://msdn.microsoft.com/en-gb/library/dd456789(VS.100).aspx • http://blogs.thinktecture.com/cweyer/default.aspx • http://msdn.microsoft.com/en-us/library/ee354381.aspx • http://blogs.msdn.com/drnick/ • http://blogs.msdn.com/endpoint/ • http://msdn.microsoft.com/en-us/netframework/aa663324.aspx CHAPTER 8    175 Entity Framework Entity Framework (EF) is Microsoft’s Object Relational Mapping (ORM) solution and was first released with .NET 3.5SP1. Entity Framework received much criticism when it was first released and the team at Microsoft has been hard at work to address some of these criticisms in the latest version.  WARNING This chapter is written using a preview version of EF4, so final functionality may differ. It is also worth noting that EF is likely to have releases out of band. For the examples in this chapter you will need to install the EF CTP 2. EF and LINQ to SQL Some developers are understandably confused by the overlap between EF and LINQ to SQL. EF and LINQ to SQL were developed by two different teams—hence the overlap. LINQ to SQL is a great piece of technology and very suitable as a simple, light-weight wrapper to SQL. It is pretty clear, however, that Microsoft is pushing developers to use EF. This is a sensible (although no doubt irritating) move as LINQ to SQL is fundamentally flawed as a generic ORM solution in that • It only works with SQL Server. • Generated classes must have a 1 to 1 relationship with database objects. EF provides an abstraction above the database layer and a number of enhancements that make it superior to LINQ to SQL. Is LINQ to SQL Dead? Er, probably not. In October 2008 Microsoft’s Tim Mallalieu (Program Manager, LINQ to SQL and LINQ to Entities) stated: “We’re making significant investments in the Entity Framework such that as of .NET 4.0 the Entity Framework will be our recommended data access solution for LINQ to relational scenarios.” http://blogs.msdn.com/adonet/archive/2008/10/29/update-on-linq-to-sql-and-linq-to- entities-roadmap.aspx CHAPTER 8  ENTITY FRAMEWORK 176 However, after feedback from a large number of LINQ to SQL customers, Microsoft seemed to back off on this a bit, when Tim said: “We will continue make some investments in LINQ to SQL based on customer feedback. This post was about making our intentions for future innovation clear and to call out the fact that as of .NET 4.0, LINQ to Entities will be the recommended data access solution for LINQ to relational scenarios “ http://blogs.msdn.com/adonet/archive/2008/10/31/clarifying-the-message- on-l2s-futures.aspx LINQ to SQL changes In VS2010/.NET 4.0 LINQ to SQL has a number of welcome performance enhancements and bug fixes. It is slightly troublesome that there is very little information on this at the time of writing, but for a full list please see the blog post by Damien Guard (who works at Microsoft within the data programmability team) at http://damieng.com/blog/2009/06/01/linq-to-sql-changes-in-net-40. Why Use EF? The sections below cover the benefits of using an ORM solution, although please note that some of these advantages are not EF specific—excellent (and more mature) alternatives do exist. Probably the best- known and one of the most mature ORM in the .NET world is NHibernate. You can, however, view an extensive list at http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software. Abstraction Data is generally held in a relational manner that is optimized for storage and quick retrieval (well, in most cases it should be). However, this format tends to not map that easily to how we want to work with objects. ORMs can provide a conceptual mapping that allows you to organize your entities and their properties in a manner that is different than how they are physically stored. For example, I worked on an application for a hospital that stored data about patients in two tables: Person and Patient. These tables had a one-to-one relationship. Person held demographic details and Patient held clinical-specific information. Whether this is the best database design is not important, but you can probably see that when you want to work with a “patient,” it is tedious to have to work with two separate objects. EF allows you to create an object that is spread across two tables, allowing you to write more intuitive code, such as the following: Patient.Firstname Patient.BloodType Code Generation A great advantage of using an ORM solution is that it provides basic CRUD (created, read, update, delete) functionality saving you from having to write this part. As code is automatically generated, the potential for errors is also reduced. Using an ORM can also make it easier to adhere to naming conventions as classes and methods are automatically generated. CHAPTER 8  ENTITY FRAMEWORK 177 It is worth noting that by utilizing an ORM framework for data access, you are introducing additional overhead to data access, and that for high performance applications, you may be better off working with the database directly. Generally, however, the performance implications will probably be pretty minimal and outweighed by the other advantages an ORM solution can bring. Support for Different Databases EF provides support for a number of different databases in addition to SQL Server, such as Oracle, MySQL, SQLAnywhere, and, recently, Synergex’s Synergy and a few more. EF contains its own query language and mechanisms that provide you with a consistent query mechanism that can be used across many different data sources. EF doesn’t care whether your database uses T-SQL or PL/SQL—querying is the same. Design Time Support EF has excellent design-time support that developers will quickly get to grips with. Yes, it was a bit ropey in the original version, but this is much improved in VS2010. I don’t think the majority of ORMs provide this level of GUI functionality, so this is definitely one of EF’s strengths. Utilize LINQ LINQ to Entities allows you to construct some very complex queries utilizing the .NET Framework that would not be possible with standard SQL. N-Tier Application Development EF has a number of features (particularly in the latest release), such as support for POCO (Plain old CLR object e.g.net classes!) and self-tracking of change templates, that make it a great solution for the development of n-tier applications. Where is EF Used? Several areas of .NET 4.0 and VS2010 support or are dependent on EF: • WCF Data Services • Windows Azure • Dynamic data framework • Microsoft Ajax libraries You can, of course, use EF anywhere in your application, but you may find it is particularly suitable to some areas, such as: • ASP.NET MVC • Silverlight/WPF (many controls have inbuilt EF support) • ASP.NET (many controls have built-in support for binding to EF objects) CHAPTER 8  ENTITY FRAMEWORK 178 EF 101 Many developers may be unfamiliar with EF as it was released in between VS2008 and VS2010, or others may have heard bad things about it (not all of which are true). Let’s take a quick look at how to work with EF before looking at the new features in EF4. Entity Data Model All EF applications contain an entity data model (EDM) that describes the objects in the underlying data source. The EDM can be further divided into three layers: • Conceptual model (the view your users see) • Storage model (how data is actually stored) • Mapping model (links the conceptual and storage models) EDMs are stored as XML and are composed of three main sections (which link to the three conceptual layers already described): • CSDL (Conceptual Schema Definition Language) • SSDL (Store Schema Definition Language) • MSL (Mapping Specification Language) It is important to understand the format of the EDM file. EF contains good GUI support, but for more advanced customizations it is necessary to modify the XML file directly. The exact format of your EDM file is also dependent on how it is generated (described next). If you generate your model using the wizard in Visual Studio, the EDM will be held in one file with the extension .edmx with the conceptual, storage, and mapping sections split under the following nodes: • edmx:StorageModels • edmx:ConceptualModels • edmx:Mappings If, however, you generate your model using EDMGen.exe (discussed next) then the model will be split into three separate files: .SSDL, .CSDL, and .MSL. Creating an EDM You can create an EDM in three different ways: • The EDMGen command-line tool • By using the ADO.NET data model wizard in Visual Studio • By creating the model in Visual Studio and then having VS generate your database structure from this model (new to EF4) . the fact that as of .NET 4. 0, LINQ to Entities will be the recommended data access solution for LINQ to relational scenarios “ http://blogs.msdn.com/adonet/archive/ 200 8/ 10/ 31/clarifying-the-message-. Microsoft within the data programmability team) at http://damieng.com/blog/ 200 9 /06 /01 /linq-to-sql-changes-in -net- 40 . Why Use EF? The sections below cover the benefits of using an ORM solution,. e.g .net classes!) and self-tracking of change templates, that make it a great solution for the development of n-tier applications. Where is EF Used? Several areas of .NET 4. 0 and VS 201 0 support

Ngày đăng: 01/07/2014, 21:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan