Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 2e9c43658e374d290a2de15d25134ac8 > files > 829

db4o-doc-8.0-1.fc15.i686.rpm

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Platform Specific Issues" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem">
    <head><title>WCF Data Services (aka ADO.NET Data Services)</title>
        <script type="text/javascript">/* <![CDATA[ */
window.onload = function(){
	var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf';
	ZeroClipboard.setMoviePath(pathToFlash);
			
	function bindToClipBord(element,content){
		var clip = new ZeroClipboard.Client();
		clip.setText(content);
		clip.glue(element);
	};
		
	if(location.protocol==='file:'){
		$('.copylink-marker').remove();
	} else{
			$('.copylink-marker').each(function(){
				var text = $(this).parent().parent().children('.prettyprint').html();
				$(this).hover(function(){
					bindToClipBord(this,text);
				},
				function(){});
			});	
	}		
	prettyPrint();	
};
                /* ]]> */</script>
        <link href="../SkinSupport/MadCap.css" rel="stylesheet" />
        <link href="../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" />
        <script src="../Resources/Code/prettify.js">
        </script>
        <script src="../Resources/Code/lang-vb.js">
        </script>
        <script src="../Resources/Code/jquery.min.js">
        </script>
        <script src="../Resources/Code/ZeroClipboard.js">
        </script>
        <script src="../SkinSupport/MadCapAll.js" type="text/javascript">
        </script>
    </head>
    <body>
        <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../index_CSH.html#platform_specific_issues/wcf_data_services.htm" style="">Open topic with navigation</a>
        </p>
        <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../platform_specific_issues.htm">Platform Specific Issues</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">WCF Data Services (aka ADO.NET Data Services)</span>
        </div>
        <p>
            <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">');
				document.write("Direct Link");
			document.write('</a>');/*]]>*/</script>
        </p>
        <p>
        </p>
        <h1><a name="kanchor109"></a>WCF Data Services (aka ADO.NET Data Services)</h1>
        <p>The WCF Data Services allows you to easily expose your data as a web service. db4o supports this framework. Look up the <a href="http://msdn.microsoft.com/en-us/data/bb931106.aspx" target="_blank">MSDN documentation on WCF Data Service</a> for more details about it and its features.</p>
        <p>The are two things which the data service need. An IQueryable implementation and a IUpdatable implementation. db4o provides both. For this you need to reference the Db4objects.Db4o.Linq.dll and Db4objects.Db4o.Data.Services.dll. The first one contains the LINQ-provider, the second contains the IUpdatable.</p>
        <h2>Preparing The Persistent Classes </h2>
        <p>The first thing you need to do is to add a key to your objects. And you need specify at least one key for each object.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">[DataServiceKey("Email")]
public class Person
{
    private string name;
    private string email;

    // Note that a default constructor is required for WCF
    public Person()
    {
    }

    public Person(string email, string name)
    {
        this.email = email;
        this.name = name;
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public string Email
    {
        get { return email; }
        set { email = value; }
    }

}</pre>
            <div class="codesnippet-meta">Person.cs: Add at least one key
			<div class="codedownload"><a href="../CodeExamples/WCFDataServices/WCFDataServices/Example-WCFDataServices-WCFDataServices-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">[DataServiceKey("TeamName")]
public class Team
{
    private string teamName;
    private string motivation;
    private IList&lt;Person&gt; members = new List&lt;Person&gt;();

    public Team()
    {
    }

    public Team(string teamName)
    {
        this.teamName = teamName;
    }

    public string TeamName
    {
        get { return teamName; }
        set { teamName = value; }
    }

    public string Motivation
    {
        get { return motivation; }
        set { motivation = value; }
    }

    public IList&lt;Person&gt; Members
    {
        get { return members; }
        set { members = value; }
    }
}</pre>
            <div class="codesnippet-meta">Team.cs: The Team has also a key
			<div class="codedownload"><a href="../CodeExamples/WCFDataServices/WCFDataServices/Example-WCFDataServices-WCFDataServices-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <h2>Create A Context</h2>
        <p>After that, you can build a data context. For this, inherit from the Db4oDataContext. You need to overwrite the OpenSession() to provide the right object container. A good practice is to use a object container per request. For example you can use the <a href="web/web_isolation.htm">OpenSession-operation</a> for creating a container per request. Take also a look how you can provide a object container for     <![CDATA[ ]]><a href="web/asp.net_request_example.htm">each request</a>.</p>
        <p>After that, you can add operations and properties you want to expose. Remember to use the IQueryable interface to expose query options to the client.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">public class TeamDataContext : Db4oDataContext
{
    // Provide access to your data via properties
    public IQueryable&lt;Person&gt; Persons
    {
        get { return Container.AsQueryable&lt;Person&gt;(); }
    }
    public IQueryable&lt;Team&gt; Teams
    {
        get { return Container.AsQueryable&lt;Team&gt;(); }
    }



    /// You need to implement the open-session and return a object container
    /// The best practise is to use a separate object-container per request.
    /// For example use the 
    /// &lt;see cref="IObjectContainer"/&gt;.&lt;see cref="IObjectContainer.Ext"/&gt;.&lt;see cref="IExtObjectContainer.OpenSession"/&gt;
    /// to open session-containers for each request.
    protected override IObjectContainer OpenSession()
    {
        return Db4oEmbedded.NewSession();
    }

}</pre>
            <div class="codesnippet-meta">TeamDataContext.cs: An concrete context
			<div class="codedownload"><a href="../CodeExamples/WCFDataServices/WCFDataServices/Example-WCFDataServices-WCFDataServices-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <h2>Create the Service</h2>
        <p>The last step is to actually create a service. Visual Studio can assist to do this. Right click on the Project, choose 'Add'-&gt; 'New Item'. Then Choose the 'WCF Data Service'. In older releases it's called 'ADO.NET Data Service'. </p>
        <p>After that, you can rename the created classes and parameterize it with the previously created context. Then you need to specify which operation are allowed. Read more about how the configure the allowed operation in the <a href="http://msdn.microsoft.com/en-us/data/bb931106.aspx">data service documentation</a>.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">public class TeamDataService : DataService&lt;TeamDataContext&gt;
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
    }
}</pre>
            <div class="codesnippet-meta">TeamDataService.svc.cs: Build the concrete service
			<div class="codedownload"><a href="../CodeExamples/WCFDataServices/WCFDataServices/Example-WCFDataServices-WCFDataServices-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <h2>Consuming The Service</h2>
        <p>Now you can startup this service and you're ready to consume it. For example you can create a simple console application. There you import the service. Right click on the project choose 'Add Service Reference'. Then point the assistant to the URL of the running web service. After that you can use the service.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">var serviceURL = new Uri("http://localhost:8080/TeamDataService.svc");    
var serviceContext = new TeamDataContext(serviceURL);

var teams = serviceContext.Teams;
foreach (var team in teams)
{
    Console.Out.WriteLine(team.TeamName);
}</pre>
            <div class="codesnippet-meta">SimpleConsoleClient.cs: Now the service can be used
			<div class="codedownload"><a href="../CodeExamples/WCFDataServices/ConsoleClient/Example-WCFDataServices-ConsoleClient-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <p>&#160;</p>
        <script type="text/javascript" src="../SkinSupport/MadCapBodyEnd.js">
        </script>
    </body>
</html>