Click here to Skip to main content
15,896,153 members
Articles / Programming Languages / C#

Introducing the Composite Data Service Framework

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
27 Nov 2011CPOL5 min read 30.9K   509   19  
Use a single Data Service to expose data from many sources with the Composite Data Service Framework!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Providers;
using System.Data.Metadata.Edm;
using System.Data.Objects;

namespace CompositeDataServiceFramework.Server
{
    public static class EntityToResourceMapping
    {

        public static ResourceType MapEntityType(EntityType entityType, ObjectContext context, string namespaceName)
        {
            //  *** How do we get the CLR tye/
            string parentNamespace = context.GetType().Namespace;
            var ss = context.GetType().Assembly;
            var type = (from t in ss.GetTypes() where t.Name == entityType.Name select t).FirstOrDefault();

            //  Create the resource type.
            ResourceType resourceType = new ResourceType(
              type,
              ResourceTypeKind.EntityType,
              null, // base types not supported.
              namespaceName,
              entityType.Name,
              entityType.Abstract
              );

            //  Add each property.
            //  *** TODO ***
            //  Complex types are not supported. We assume the first property is the key.
            //  Navigation types are not supported.
            bool first = true;
            foreach (var propertyType in entityType.Properties)
            {
                ResourcePropertyKind kind = ResourcePropertyKind.Primitive;
                if (first)
                {
                    kind |= ResourcePropertyKind.Key;
                    first = false;
                }

                var resourceProperty = new ResourceProperty(
                       propertyType.Name,
                       kind,
                       MapEdmType(propertyType.TypeUsage.EdmType)
                    );
                resourceType.AddProperty(resourceProperty);
            }
            
            return resourceType;
        }

        private static ResourceType MapEdmType(EdmType edmType)
        {
            if (edmType is PrimitiveType)
                return ResourceType.GetPrimitiveResourceType(((PrimitiveType)edmType).ClrEquivalentType);
            throw new Exception("Don't know type " + edmType.Name);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.

Comments and Discussions