Webservice must return only the few fields of Table entry from Database Object Model




hello!

i have database, called "mycompany". convert object-model via add new item --> ado.net entity data model, , object-representation of database. say, name mycompanymodel.


for example, 1 of tables in database customer, have following fields:
id
name
login
password
country


in webservice, implement, need return in xml format customer table entry.

the code following:

[webmethod] 
public list<mycompanymodel.customer> getcustomers( ) 
    m_mycompanyentities = new mycompanymodel.mycompanyentities( ); 
 
    var query = from m in m_mycompanyentities.customer select m; 
                 
    return query.tolist<mycompanymodel.customer>( ); 



all objects automatically serialized xml. need.

but, need implement 2 versions of web-service:
  • one - administrative using, includes fields of customer table entries
  • another - public using, includes few fields of customer table entries. version of webservice called silverlight or flash browser clients. actually, clients visitors of website.
in examples of source-code above, client returned fields of customer table entries. how can return few ??
the solution, seek must natural, without tricks.

sure, can create new class, have restricted set of public properties, , instantiate mycompanymodel.customer, so:
[webmethod] 
public list<customer_private> getcustomers( ) 
    m_mycompanyentities = new mycompanymodel.mycompanyentities( ); 
    var query = from m in m_mycompanyentities.customer 
                select m; 
 
 
    list<customer_private> result; 
    foreach( mycompanymodel.customer c in query.tolist<mycompanymodel.customer>() ) 
    { 
        result.pushback( new customer_private(c) ); 
    } 
 
    return result; 
 
 
internal class customer_private 
    public customer( mycompanymodel.customer c ) 
    { 
        this.name = c.name; 
        this.country = c.country; 
         
    } 
 
    public string name { get; set; } 
    public string country {get; set; } 
 

but, hope, there better solution can found...

need 2 different customer entities because different. may cause administrativecustomer entity derive publiccustomer entity, in order avoid duplication of effort.
john saunders | use file->new project create web service projects


Archived Forums A-B  >  ASMX Web Services and XML Serialization



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'