NET core 2 0 by example

485 164 0
NET core 2 0 by example

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

NET Core 2.0 By Example Learn to program in C# and NET Core by building a series of practical, crossplatform projects Rishabh Verma Neha Shrivastava BIRMINGHAM - MUMBAI .NET Core 2.0 By Example Copyright © 2018 Packt Publishing All rights reserved No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews Every effort has been made in the preparation of this book to ensure the accuracy of the information presented However, the information contained in this book is sold without warranty, either express or implied Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals However, Packt Publishing cannot guarantee the accuracy of this information Commissioning Editor: Aaron Lazar Acquisition Editor: Chaitanya Nair Content Development Editor: Rohit Kumar Singh Technical Editor: Romy Dias Copy Editor: Safis Editing Project Coordinator: Vaidehi Sawant Proofreader: Safis Editing Indexer: Pratik Shirodkar Graphics: Jason Monteiro, Tom Scaria Production Coordinator: Deepika Naik First published: March 2018 Production reference: 1160318 Published by Packt Publishing Ltd Livery Place 35 Livery Street Birmingham B3 2PB, UK ISBN 978-1-78839-509-0 www.packtpub.com mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career For more information, please visit our website Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content PacktPub.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy Get in touch with us at service@packtpub.com for more details At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks Contributors About the authors Rishabh Verma is a Microsoft certified professional and works at Microsoft as a development consultant, helping to design, develop, and deploy enterprise-level applications He has 10 years' hardcore development experience on the NET technology stack He is passionate about creating tools, Visual Studio extensions, and utilities to increase developer productivity His interests are NET Compiler Platform (Roslyn), Visual Studio Extensibility, and code generation No words can describe my gratitude to my parents, Shri Rakesh Chandra Verma and Smt Pratibha Verma, who supported me during the writing of this book Their hard work over the years has been the inspiration behind me taking up this challenging work I would also like to offer special thanks to my brother, Shri Rishi Verma, who kept motivating me day in and day out I also want to thank my colleagues, managers, and team at Microsoft for their support Neha Shrivastava is a Microsoft certified professional and works as a software engineer for the Windows Devices Group at Microsoft India Development Center She has 7 years' development experience and has expertise in the financial, healthcare, and e-commerce domains Neha did a BE in electronics engineering Her interests are the ASP.NET stack, Azure, and cross-platform development She is passionate about learning new technologies and keeps herself up to date with the latest advancements I would like to thank my parents, Shri O P Shrivastava and Smt Archana Shrivastava, for their continuous support and motivation Their "Never Say Die" mantra keeps me up and running Heartfelt thanks to my brother, Dr Utkarsh Shrivastava, my sister-in-law, Dr Samvartika Shrivastava, and my little angel Sarvagya, for their continuous support and words of encouragement This book wouldn't have been possible without the blessings of my beloved Dadi! Install -Package Newtonsoft.Json Data access tools We discussed data type providers earlier F# also has dynamic API support for faster and dynamic data retrieval It contains CSV, HTML, JSON parsers, and also tools for HTTP request parsing Let's briefly discuss each of them: CSV parser: To access data dynamically, we can use the CSV parser The afore mentioned CSV provider is built on top of the F# CSV parser The FSharp.Data namespace has the CsvFile type, which provides two methods for loading data: the Parse method for string data, and the Load method for reading data from a file or any web source (example: CsvFIle.Load()) HTML parser: It parses HTML documents into the DOM When it gets parsed into DOM, F# supports many extension functions for HTML DOM elements, to extract information from the web page Let's see an example where we will search NET Core in Google and parse the first search result page, getting all URLs and hyperlinks: open FSharp.Data let resultsDocument = HtmlDocument.Load("http://www.google.co.in/search?q=.NET Core") In the previous code, using HtmlDocument.Load(), we are parsing the web page into DOM resultsDocument contains all data from the page, as this method will make a synchronous web call We can also make an asynchronous call using the method, HtmlDocument.AsyncLoad() To extract data from the result document, we first find all HTML anchor tags and then find all href tags to get the link and its text: let x = resultsDocument.Descendants ["a"] |> Seq.choose (fun x -> x.TryGetAttribute("href") |> Option.map (fun a -> x.InnerText(), a.Value()) ) let Z = x |> Seq.filter (fun (name, url) -> name "Cached" && name "Similar" && url.StartsWith("/url?")) |> Seq.map (fun (name, url) -> name, url.Substring(0, url.IndexOf("&sa=")).Replace("/url?q=", "")) |> Seq.toArray The output will show all first page search results for NET Core in Google The result looks like this: JSON Parser: The same as the CSV provider, the JSON provider is built on top of the JSON parser We need the same library for all the parsers: FSharp.Data.dll It has the JsonValue type for parsing Here is an example: open FSharp.Data let empInfo = JsonValue.Parse(""" { "name": "Neha", "Company": "Microsoft","Projects": [ "Proj1", "Proj2" ] } """) supports many extension methods such as value.Properties() and gives a list of properties of a record node FSharp.Data.Extensions HTTP Utilities: In the FSharp.Data namespace, we have HTTP utilities, which are easy and quick for HTTP requests, post data or responses such as get status code HTTP has a few overloaded methods, requestString and AsyncRequest or AsyncRequestString and AsyncRequest; these can create a simple request synchronously or asynchronously, respectively Here is an example: open FSharp.Data Http.RequestString("http://rishabhverma.net ") // Download web site asynchronously async { let! html = Http.AsyncRequestString("http://rishabhverma.net ") printfn "%d" html.Length } |> Async.Start // Verifying the response: let response = Http.Request("http://rishabhverma.net/algorithmics-science-and-art/", silentHttpErrors = tr // Examine information about the response response.Headers response.Cookies response.ResponseUrl response.StatusCode Here is the result: SQL data access In F#, there are multiple libraries for SQL data access We can browse them in NuGet; a few of them are discussed as follows: : This library provides SqlCommandProvider, which gives type safe access to FSharp.Data.SqlClient transactional SQL languages SqlProgrammabilityProvider provides quick access to a SQL server stored procedure (SP), tables, and functions, and SqlEnumProvider generates an enum type on the ground of static lookup data from an ADO.NET-compliant source To install the FSharp.Data.SqlClient library from NuGet, use the following command: PM> Install-Package FSharp.Data.SqlClient : is a general NET/Mono SQL database provider This library supports automatic constraint navigation, CRUD operations with identity support, asynchronous operations, LINQ query, functions, SP support, record types mapping, NET Core/.NET Standard, and so on SQLProvider has explicit implementation for SQL Server, SQLite, Oracle, MySQL, MSAccess, Firebird, and so on SQL Server and MS Access don't require third-party ADO.NET connector objects, the rest all require this To install the FSharp.Data.SqlProvider library from NuGet, use the following command: FSharp.Data.SQLProvider SQLProvider PM> Install-Package SQLProvider The SqlDataConnection: SqlDataConnection type provider generates types, for data in SQL DB on live connections It is for accessing SQL using LINQ queries It requires a Microsoft SQL server We need three references in a F# project—FSharp.Data.TypeProviders, System.Data, and System.Data.Linq Here is an example: type dbSchemaExample = SqlDataConnection

Ngày đăng: 02/03/2019, 10:46

Từ khóa liên quan

Mục lục

  • .NET Core 2.0 By Example

  • Title Page

  • Copyright and Credits

  • .NET Core 2.0 By Example

  • Packt Upsell

  • Why subscribe?

  • PacktPub.com

  • Contributors

  • About the authors

  • About the reviewer

  • Packt is searching for authors like you

  • Preface

  • Who this book is for

  • What this book covers

  • To get the most out of this book

  • Download the example code files

  • Download the color images

  • Conventions used

  • Get in touch

  • Reviews

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

Tài liệu liên quan