More Complex Operations in ADO.NET Data Services
page 2 of 5
by Sergey Zwezdin
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 37231/ 209

Data Model Instance Control

When you build a service based on ADO.NET Data Services platform you should specify a data model that will be used as a parent for a creating web service. For REST-service a data model is an essential component because a web service gives access to these data.

A data model is defined in a generic-parameter of DataService<> base class. In this case when you access to a service infrastructure, ADO.NET Data Services instances a data model automatically and works with it. As a result, a default constructor is used without any parameters.

Listing 1 - Simple web service

public class WebDataService1 : DataService<NorthwindEntities>
{
}

If a data model doesn't have a constructor without parameters, infrastructure of ADO.NET Data Services can't create any instance of this type. It is the reason you'll get an error message when try to access to such service. This situation is well-spread. That's why many developers prefer to avoid default constructors.

To resolve this problem you should override a way to instantiate a data model and use a necessary constructor of a data model. You should override CreateDataSource method to get own algorithm of instantiate of a data model. As result, this method returns a type that is specified in a generic-parameter of a base class at a web service definition. By default this method accesses to CreateDataSource method of a base class which tries to find a default constructor and create an object.

Listing 2 - The definition of instance mode of data model

protected override NorthwindEntities CreateDataSource()
{
  return base.CreateDataSource();
}

If a correct work of a web service demands of redefine logic of a data model creation then you need to build a model manually and use a suitable constructor. Also in this method it is possible to use a reference to IoC-container or implement based on Dependency Injection principle. In the simplest case an instance of a data model can be created and get a string of a connection with a server of databases.

Listing 3 - The definition of instance mode of data model

protected override NorthwindEntities CreateDataSource()
{
      return new NorthwindEntities(@"some connection string");
}

Thus, CreateDataSource method will be called every time when hosting-environment needs to instantiate a data model. 


View Entire Article

User Comments

Title: fasdfa   
Name: asdfasdf
Date: 2012-08-25 7:46:11 AM
Comment:
asdfsdf
Title: developer   
Name: developer
Date: 2011-07-31 1:24:23 PM
Comment:
When I try this in new version of WCF Data Services,
public System.IO.Stream GetCustomerPhoto(string customerId)
generates an exception and says that Stream cannot be serialized and returned.
Do you know why?
Title: System.IO.Stream GetCustomerPhoto?   
Name: Dana
Date: 2010-03-11 2:46:40 PM
Comment:
You can return an image from a service operation? I couldn't figure this out. Can you elaborate on that stub? When I tried to create the service operation and then "update service reference" from my client app, I got an error to the effect that the meta data couldn't be created.
Great article and any further info is appreciated. Thanks,
Dana
Title: moreclick   
Name: moreclick
Date: 2009-12-07 2:17:58 AM
Comment:
Thanks for sharing, I love reading you should post more often...

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2018 ASPAlliance.com  |  Page Processed at 2018-08-14 7:21:51 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search