Tuesday, August 28, 2012

What is the best pratice to pass a list of (MVVM) Model class to web service

What is the best pratice to pass a list of (MVVM) Model class to web service

I have list of a custom class which is a Model in the mvvm pattern . I need to insert the list data in the DB . So can anyone suggest what is the best way to pass this list to the web service ?

Thanks

Answers & Comments...

Answer: 1

If list is not too large you could try it serializing the list and sending it "as is". If it is too large you may have problems with the MaxReceivedMessageSize and maxBufferSize properties of your service, you should set them as big as you can. Of course, I assume you have the same Model both sides (client and server) to serialize and deserialize it.

To reduce the data size you send you could try to use TCP binding instead HTTP.

If it's still too large, you may have to chunk the list and send it by calling several times the same service.

I've done several workarounds in many cases too. If for example you only need to update a list of objects changing just a couple of fields, I use to make a new list of object-id + new values and make changes server side instead of changing everything in the client and then sending it to the server.

Hope this helps you ;-)

by : zapicohttp://stackoverflow.com/users/694693

Answer: 2

We expose a WCF service that sends and receives DTO's, then create a service reference in Silverlight. We use EmitMapper in Silverlight to map model classes to the proxy-generated DTO's.

Updated with code examples.

DTO class on the server side:

public class CompanyDTO {     public string Name     {         get;         set;     } } 

WCF service interface:

[ServiceContract] public interface IUpdateService {     [OperationContract]     void InsertCompanies(List<CompanyDTO> companies); } 

WCF service implementation:

public class UpdateService : IUpdateService {     public void InsertCompanies(List<CompanyDTO> companies)     {         // insert stuff into database     } } 

Model class in Silverlight:

public class Company {     public string Name     {         get;         set;     } } 

Invoking the proxy:

public MainPageViewModel() {     var company = new Company { Name = "Amalgamated Co." };     var companyDTO = EmitMapper.ObjectMapperManager.DefaultInstance             .GetMapper<Company, UpdateServiceProxy.CompanyDTO>().Map(company);      UpdateServiceProxy.IUpdateService client = new UpdateServiceProxy.UpdateServiceClient();     client.BeginInsertCompanies(             new ObservableCollection<UpdateServiceProxy.CompanyDTO> { companyDTO }, null, null); } 

Emit Mapper is available here

by : borishttp://stackoverflow.com/users/874831




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog