baigiang chuyendephattrienphanmem linq remoting 23-10-2010

Upload: nguyen-van-teo

Post on 14-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    1/77

    Week 4: LINQ

    Basic concepts

    LINQ requirements

    Concepts

    Types LINQ to objects

    LINQ to SQL

    LINQ to Entity LINQ to XML

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    2/77

    2. New features in langauge

    Generics

    Implicitly Typed Variables

    Object Initializers

    Anonymous Types

    Extension Methods

    Lambda Expressions

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    3/77

    2.0 Generics

    the creation of various types ofcollection

    type safety

    Binary Code Reuse

    using System.Collections.Generic;

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    4/77

    2.1 Implicitly Typed Variables Implicitly Typed Variables

    Declare variables without specifying their type Strongly type, not variant, not object

    VS will determinr type

    Predict what the compiler will choose Intellisense support

    Type inference -> most general

    3/10/2010 -> string, not date

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    5/77

    2.1 Implicitly Typed Variables

    Example

    var x = new OledbConnection();

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    6/77

    2.1 Implicitly Typed Variables

    Note

    Alwayls declare the type if we know

    Implicitly Typed Variables are suited for LINQ and

    anonymous type

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    7/77

    2.2 Object Initializers

    Constructor

    Allow to assign values to object properties(fields) when create object

    We do not have to explicitly invoke aconstructor

    Usefull in any context

    Especially userfull in LINQ expressions

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    8/77

    2.2 Object Initializers

    Example

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    9/77

    2.3 Anonymous Types Implicitly type functionality for objects

    Set property values to object without writingclass definition

    The resulting class has no usable name

    Class name is generated by compiler, inheritsfrom Object

    The result: an anonymous type that is notavailable at source code level

    Also called Projections

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    10/77

    2.3 Anonymous Types When to user anonymous types

    Need a temporary object to hold related data

    Dont need method

    If we need a different set of properties for each

    declaration

    If we need to change the order of properties for

    each declaration

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    11/77

    2.3 Anonymous Types When not to user anonymous types

    Need to define methods

    Need to define another variable

    Need to shared data across methods

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    12/77

    2.4 Extension Methods Special kind of Static method

    Allow the addition of methods to an existingclass

    Without creating a new derived type

    Without re-compiling or modifying the original type

    Called The Same way regular methods are called

    Define in static class

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    13/77

    2.4 Extension Methods Example

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    14/77

    2.4 Extension Methods Example

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    15/77

    2.4.a Delegate Delegate

    refers to method.

    When initialize a delegate, we initialize it with

    method.

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    16/77

    2.4.a delegate: example

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    17/77

    2.4.a delegate: why

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    18/77

    2.4.a delegate: why

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    19/77

    2.4.a delegate: with anonymous

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    20/77

    2.4.a delegate: with anonymous:

    return to example

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    21/77

    2.5 Lambda Expressions Lambda Expressions is an Anonymous function

    A funciton without a name Perform action , return a single value

    Can be used wherever a delegate type is valid

    Why? Allow a functions caller to define the action

    Not just pass parameters

    Lambda expressions provide a clearer syntax foranonymous delegates

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    22/77

    2.5 Lambda Expressions: delegate Syntax

    parameters => expression a functional superset of anonymous methods

    Lambda expressions can "infer" parameter types,

    even if you omit them

    Lambda expressions can use both statement

    blocks and expressions

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    23/77

    2.5 Lambda Expressions in C# Use lambda operator =>

    Read as goes to x=>x+3 if x=1 then 4 is returned

    X=> x==5: if x=5, True is returned

    (x,y)=> x+y if x=2, y=4 then 6 is returned

    (x,y)=>x==y if x=2, y=4 then False is returned

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    24/77

    2.4.a lambda example

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    25/77

    2.4.a delegate: turn back to

    example: lambda

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    26/77

    3. LINQ Language Integrated Query

    LINQ does create a new way of looking at enumerations of

    data, and gives out-of-the-box functionality for them

    L2O

    L2S

    L2E

    L2X

    L2D

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    27/77

    3. LINQ example 1

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    28/77

    3. LINQ requirements .Net framework 3.5

    VS 2008

    Work with VS 2005: not recommended

    C#

    VB.NET

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    29/77

    3. What is LINQ a programming model that introduces

    queries as a first-class concept SQL-like syntax used in LINQ is called a query

    expression

    provides a set of standard query operators

    common tasks: query, ordered, grouped oraggregated from

    an existing in-memory collection, a database, an XML file or

    many other sources

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    30/77

    1. What is LINQ How LINQ works?

    compiler generates code More detail: later

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    31/77

    1. What is LINQ: Language Integration

    Language in query expression

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    32/77

    1. What is LINQ: LINQ Flavors covers many data domains

    these implementations is defined through a set ofextension methods

    The access to these features is controlled by the

    imported namespaces can be extended to support other data domains

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    33/77

    3. What is LINQ: Everywhere LINQ to Objects, LINQ to SQL, and LINQ to

    XML LINQ is an extendable technology

    open-source product: LINQExtender

    build your own LINQ extensions

    Other providers

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    34/77

    3. What is LINQ: Everywhere

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    35/77

    3. LINQ new features of.NET framework support for

    LINQ: Extension methods, lambda expressions

    are used extensively to build queries.

    anonymous types : to allow data to be returnedfrom a query without the need to first define

    a class or structure,

    generics : creation of various types ofcollection

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    36/77

    3. LINQ basic 2 types of Syntax

    Query syntax Method syntax

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    37/77

    4. LINQ syntax example ??????????

    ??????????

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    38/77

    3. LINQ basic: Query syntax vs

    Method syntax

    have to "redeclare" person each time in the lambda expressions

    Query syntax => Method syntax

    certain queries can only be expressed in method syntax and some of

    those require lambda expressions.

    Performance???

    query syntax is a lot more readable than method syntax

    Tips

    Use the Query Expression syntax wherever possible, its easier to read;

    If you need to mix the Extension Methods with Query Expressions, put them at the

    end;

    Keep each part of the Query Expression on separate lines

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    39/77

    3. Exer Convert CVS (comma separated values )

    string to int array S =20;10;2;0;1;0

    => array of int {20,10,2,0,1,0}

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    40/77

    5. LINQ Execution deferred execution, or lazy loading

    Queries that return a single value or object doexecute immediately. However, those that return

    a list of items are usually not executed until the

    first time that the results are used.

    Lazy means "don't do the work until you absolutely have to."

    Deferred means "don't compute the result until the caller

    actually uses it."

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    41/77

    5. LINQ Execution Example deferred execution

    If you want a query to

    be realized

    immediately,

    call ToList, ToArray orany of the other

    operators that need to

    enumerate the entire

    sequence to return a

    result.

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    42/77

    7. LINQ to SQL Topics

    Concepts Mapping object

    Datacontext

    Actions Query

    Insert, update, delete

    Join

    Stored procedure

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    43/77

    7. LINQ to SQL: concepts Created to bridge the differences between

    relational data and CLR object Mapping Classes to Tables

    Using code or Graphical Designer for Mapping

    Creating Entity Classes

    Tableclass, Rowobject

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    44/77

    7. LINQ to SQL Using the Graphical Designer for Mapping

    Server connection

    Adding a designer file (dbml): LINQ to SQL

    Classes

    Drag drop from Server Explorer ->design surface

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    45/77

    7. LINQ to SQL: concepts The DataContext

    retrieve objects from database

    Track changes

    Update from object to database

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    46/77

    7. LINQ to SQL: concepts Translating LINQ to SQL

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    47/77

    7. LINQ to SQL: actions Select data

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    48/77

    7. LINQ to SQL : actions Modifying and Saving Entities

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    49/77

    7. LINQ to SQL : actions: insert

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    50/77

    7. LINQ to SQL : actions: delete

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    51/77

    7. LINQ to SQL : actions: update

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    52/77

    7. LINQ to SQL: actions: join

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    53/77

    7. LINQ to SQL : actions Using stored procedure

    In the Server Explorer-> select stored node->drag drop on design interface

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    54/77

    7. LINQ to SQL : actions Join tables

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    55/77

    7. LINQ to SQL : actions Relationships

    Querying Across Relationships

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    56/77

    6.1 LINQ exercise Change database to MS SQL Server

    Using L2S Display data in Listview

    Insert new contact record

    Update contact record Delete selected records in list

    Button Search contact

    L2S L2O

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    57/77

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    58/77

    LINQ to Object Restriction Operators

    Projection Operators

    Partitioning Operators

    Ordering Operators Grouping Operators

    Set Operators

    Conversion Operators

    Element Operators

    Generation Operators Quantifiers

    Aggregate Operators

    Miscellaneous Operators

    Custom Sequence Operators

    Query Execution Join Operators

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    59/77

    6.1 Example: select controls

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    60/77

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    61/77

    Week 7: NET REMOTING Modern applications are no longer stand-alone

    applications

    What Is .NET Remoting?

    provides a solution for communication between

    application domains

    ASP.NET Web services (WS) vs

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    62/77

    ASP.NET Web services (WS) vs

    .NET Remoting (NR) 1. WS: only be accessed over HTTP; NR various

    protocols like TCP, HTTP etc.

    2.WS stateless environment, NR state management 3. ES more reliable than .NET Remoting.

    4. WS are easy to create and use, NR are complex tobe created.

    5. WS across platforms, NR requires client to be builtusing .NET

    6. WS support datatypes defined in the XSD type

    system while .NET remoting provides support for richtype system using binary communication.

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    63/77

    .Net Remoting components A remotable object.

    A host application. listen to requests for the hosted remotable

    object.

    A client application. requests for the remotable object.

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    64/77

    How Does .NET Remoting Works? In the client

    create a new remotable object =>creates a proxyobject

    When client call method => call method on

    proxy=> server=> return to proxy => client

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    65/77

    Remoting makes an object in server available

    to code in client => marshalling 2 ways to marshal an object

    Marshal by value

    the server creates a copy of the object passes thecopy to the client

    Marshal by reference

    the client creates a proxy for the object and then usesthe proxy to access the object

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    66/77

    Marshaling marshaling by value vs marshaling by

    referenceexecutable functionalitydirectly within its own

    process

    must implement the

    ISerializable interface

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    67/77

    Marshal by reference: multi

    Marshal by reference object. Clients in other app domains interact with marshal by

    reference objects using a proxy. Clients in the same app domain as the object have a

    direct reference on the object.

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    68/77

    Marshal by reference 2 typed

    Server-Activated Objetcs (SAO)

    Client-Activated Objects (CAO) Server-Activated

    lifetime is controlled by server

    Use when there are clients sharing the same server object

    Its a one-to-many relationship between the server object and clients

    Singleton SingleCall

    Client-Activated Objects lifetime is controlled by the client

    Use when multiple server instances of the same kind need to be run onthe server at the same time

    one-to-one relationship between each server object and itscorresponding client

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    69/77

    Client-activated objects

    Client-activated objects. Each client gets an independent object for its use. Like the

    classic client-server model, clients can still share objects.

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    70/77

    Client-activated objects: singlecall

    In the single-call activation model, .NET creates an object for each call, and disposes

    of it between calls.

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    71/77

    Client-activated objects: singleton

    With a server-activated singleton object, all clients share the same well-known

    object.

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    72/77

    Channel the medium through the which the messages

    would be transferred The remote objects are to communicate with

    one another and channels do this transport

    job of communication. The two existing channels

    HttpChannel and TCPChannel

    Implementing .NET Remoting

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    73/77

    Creating remote object

    Create host Listen

    Channel

    Type of activated object

    uri

    A client application domain

    Getobject from server

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    74/77

    A. Creating a Remote Object Creating a Remote Object

    declare a remotable class inherit the MarshalByRefObject

    build the project, generate DLL file.

    public class Class1:MarshalByRefObject

    {

    public string layten( )

    {

    return server + Now.date.toString();}

    }

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    75/77

    B. Build host server

    TcpServerChannel chnl = new TcpServerChannel(3999);

    ChannelServices.RegisterChannel(chnl,false);

    RemotingConfiguration.RegisterWellKnownServiceType(typeof(

    Class1), "class1.soap", WellKnownObjectMode.SingleCall);

    registers our created class and a channel for remoting

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    76/77

    C. Client application

    TcpChannel chnl = new TcpChannel();

    ChannelServices.RegisterChannel(chnl,false);

    Class1 obj = (Class1)Activator.GetObject(typeof(Class1),

    "tcp://192.168.210.34:3999/class1.soap");

    this.Text = obj.layten();

  • 7/30/2019 BaiGiang ChuyenDePhatTrienPhanMem LINQ Remoting 23-10-2010

    77/77

    6.1 Remoting exercise Change to Remoting App

    Host server Contact remoting object

    Client display contact in Listview