• Using SQL Server Data Services

    Published by aneisen on July 29th, 2008 1:42 pm under C#, Code, On-the-cloud services, SQL

    5 Comments

    In my previous post I’ve presented my first impressions of SQL Server Data Services. Now it’s time to start coding. In this post, I’ll show how to use the basic operations from SSDS.

    Getting started

    Remember that SSDS is a service provided by Microsoft. So, the first step is creating an account. You can register here: http://www.microsoft.com/sql/dataservices/default.mspx

    After receiving the confirmation you’ll be able to access the services.

    Adding the service reference

    In order to use the SSDS instance, we need to add a reference to the SOAP service. The URL is http://data.beta.mssds.com/soap/v1?wsdl. Remember that this is a beta instance, so the URL can be changed in any moment.

    Creating entities

    SSDS has a defined structure: authorities, containers and entities. Then, we need to create those entities in order to use the service.

    Creating the proxy

    When creating the proxy, you should only submit the client credentials.

    Note: Sitka is SSDS’ codename.

    public static SitkaSoapServiceClient GetProxy()
    {
        SitkaSoapServiceClient proxy = new SitkaSoapServiceClient("SitkaSoapEndpoint");
        proxy.ClientCredentials.UserName.UserName = "---"; //TODO: Add username
        proxy.ClientCredentials.UserName.Password = "----"; //TODO: Add password
        return proxy;
    }

    Creating authorities

    Creating an authority is just a simple call to the service. In this case the Scope will be empty, because we’re creating a new one.

    public static Authority CreateAuthority(string id)
    {
        Scope scope = new Scope();
        Authority authority = new Authority();
        authority.Id = id;
        GetProxy().Create(scope, authority);
    
        return authority;
    }

     

    Creating containers

    Creating a container is very similar to creating an authority. The only difference is that now the container is going to be scoped to an authority, so we need to specify that in the Scope.

    public static Container CreateContainer(string id, Authority authority)
    {
        Scope scope = new Scope();
        scope.AuthorityId = authority.Id;
        Container container = new Container();
        container.Id = id;
        GetProxy().Create(scope, container);
    
        return container;
    }

     

    Creating entities

    Now we’re all set to add entities to our container. Given that the model is very flexible, in order to create entities we only need to provide the id and the values.

    Note that the entity will be scoped to an authority and a container.

    public static Entity CreateEntity(string id, Dictionary<string, object> values,
            Authority authority, Container container)
    {
        Entity entity = new Entity();
        entity.Kind = "default"; //Optional metadata
        entity.Id = id;
        entity.Properties = new Dictionary<string, object>(values);
        SitkaSoapServiceClient proxy = GetProxy();
        Scope scope = new Scope();
        scope.AuthorityId = authority.Id;
        scope.ContainerId = container.Id;
        proxy.Create(scope, entity);
    
        return entity;
    }

     

    Querying entities

    Taking into account that the queries are text-based, querying over entities is just a call to the service.

    public static Entity[] Query(string query, Authority authority, Container container)
    {
        SitkaSoapServiceClient proxy = GetProxy();
        Scope scope = new Scope();
        scope.AuthorityId = authority.Id;
        scope.ContainerId = container.Id;
    
        return proxy.Query(scope, query);
    }

     

    Putting all together

    That’s it. We have our data layer implemented without having an SQL server. We’re just using an on-the-cloud service and we don’t have to worry about deploying, maintenance, etc.

    Putting all together, we can use the methods defined in this post like this:

    static void SSDSSample()
    {
        // Creating authority
        Authority authority = CreateAuthority("testauthority");
        // Creating container
        Container container = CreateContainer("testauthority_container", authority);
    
        // Adding entities
        for (int i = 0; i < 10; i++)
        {
            Dictionary<string, object> props = new Dictionary<string, object>(3) {
                    {"name", "name" + i}, {"address", "address" + i}, {"tel", "tel" + i}};
            CreateEntity("person" + i, props, authority, container);
        }
    
        // Querying entities
        Entity[] retrievedEntities = Query("from e in entities select e", authority, container);
        foreach (Entity entity in retrievedEntities)
        {
            Console.WriteLine(entity.Id);
            foreach (KeyValuePair<string, object> val in entity.Properties)
            {
                Console.WriteLine(val.Key + ": " + val.Value.ToString());
            }
            Console.WriteLine();
        }
        Console.ReadKey();
    }

  • 5 Comments:

    1. Vibro.NET said on May 16, 2009:

      More details about the Identity Developer Training Kit…

      digg_url = “http://blogs.msdn.com/vbertocci/archive/2009/05/15/more-details-about-the-identity-developer-training-kit.aspx”;digg_title…

    2. Web and Cloud said on May 16, 2009:

      More details about the Identity Developer Training Kit…

      digg_url = "http://blogs.msdn.com/vbertocci/archive/2009/05/15/more-details-about-the-identity-developer…

    3. Azure on Vietnam said on May 16, 2009:

      More details about the Identity Developer Training Kit…

      digg_url = "http://blogs.msdn.com/vbertocci/archive/2009/05/15/more-details-about-the-identity-developer…

    4. Vittorio Bertocci said on May 16, 2009:

      More details about the Identity Developer Training Kit…

      digg_url = "http://blogs.msdn.com/vbertocci/archive/2009/05/15/more-details-about-the-identity-developer…

    5. Azure on Philippines said on May 16, 2009:

      More details about the Identity Developer Training Kit…

      digg_url = "http://blogs.msdn.com/vbertocci/archive/2009/05/15/more-details-about-the-identity-developer…

    Leave a comment

    Your email address will not be published.