c# - How can i expose dto objects using wcf data service with ef code first? -


i trying make wcf data service dont want acces database models instead want use data transfer objects. have been reading lot on internet how accomplish cant answer problem. first time me doing wcf data services little inexperienced.

oke here models linked database using entity framework

public class user     {         [key]         public int userid { get; set; }         public string firstname { get; set; }         public string lastname { get; set; }         public string username { get; set; }         public string email { get; set; }          public string countrycode { get; set; }         public string phonenumber { get; set; }         public icollection<user> contacts { get; set; }          public virtual language language { get; set; }          public user()         {             contacts = new list<user>();         }     }  public class message     {         [key]         public int messageid { get; set; }         public datetime sentdate { get; set; }          public virtual user sender { get; set; }         public virtual user receiver { get; set; }          public string content { get; set; }          public string originalcultureinfoenglishname { get; set; }         public string foreigncultureinfoenglishname { get; set; }     }  public class language     {         [key]         public int languageid { get; set; }         public string cultureinfoenglishname { get; set; }     } 

now made service.svc has databasecontext can directly acces database models. want achieve instead of directly getting database models dto models when query against service.

a example of how dto's like

public class userdto     {         public int userid { get; set; }         public string firstname { get; set; }         public string lastname { get; set; }         public string username { get; set; }         public string email { get; set; }         public string country { get; set; }         public string phonenumber { get; set; }          public icollection<contactdto> contacts { get; set; }          public virtual languagedto language { get; set; }          public usermodel()         {             contacts = new list<contactdto>();         }     }  public class contactdto     {         public int userid { get; set; }          public string firstname { get; set; }         public string lastname { get; set; }         public string username { get; set; }         public string email { get; set; }          public string country { get; set; }         public string phonenumber { get; set; }          public virtual languagedto language { get; set; }     }  public class languagedto     {         public int languageid { get; set; }         public string cultureinfoenglishname { get; set; }     }  public class messagedto     {         public int messageid { get; set; }         public datetime sentdate { get; set; }          public virtual contactdto sender { get; set; }         public virtual contactdto receiver { get; set; }          public string content { get; set; }          public string originalcultureinfoenglishname { get; set; }         public string foreigncultureinfoenglishname { get; set; }     } 

now possible making different context can use in service.svc or there other way achieve this?

for example contactdto userid user less properties because not relevant in client application. see happening uri http://localhost:54895/service.svc/contactdto(1)

hopefully can clear me because frustrating :)

i'm not sure you're interested in possible, exactly. looking have multiple entity sets per type (aka mest), , don't know how that's supported.

beyond point, , discussion around dtos in general...

if use custom providers, can implement own idataservicemetadataprovider , idataservicequeryprovider. when service starts, can make calls idataservicemetadataprovider control entities , properties exposed or hidden -- including exposing properties not exist on entity. upshot end dto without coding dto class. exposed metadata dto. this resource creating own providers.

in case, isn't 100% solution, because can't selectively choose when property exposed , when it's not.

hope helps...


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -