Flexible CSV exports over web-api with server side MEF – discover the export formats. (part 4)

Introduction

Our previous post showed how we can consume an export by clicking a button. But let’s presume now that we want to present the user with a list of available export formats. Since we use MEF server side that’s a piece of cake. At the end of the post you’ll find a zip with the full code.

I’ll focus here only on the server side. I simply provide the list of potential export formats in a textbox. I leave it to the reader to invent a decent client side implementation :)

My client side implementation goes as follows:

list

 

I provide a list of available exports for the customer type.

The controller

In order to keep things a bit clean, I designed a separate controller for this:

using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Details;
using Microsoft.LightSwitch.Server;
using Microsoft.VisualStudio.ExtensibilityHosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Net.Http;
using System.Net;

namespace LightSwitchApplication.LightSwitchToolbox.ExportModule
{
    public class ExportMetaDataController : ApiController
    {
        [HttpPost]
        public HttpResponseMessage GetProjectors(GetMetaDataCommandParameters requestParams)
        {

            string dataServiceName = requestParams.DataServiceName;
            string entitySetName = requestParams.EntitySetName;
            using (IServerApplicationContext ctx = ServerApplicationContextFactory.CreateContext())
            {
                IDataService dataService =
                    ctx.DataWorkspace.Details.Properties[dataServiceName].Value as IDataService;

                var entitySet = dataService.Details.Properties.All()
                    .OfType<IDataServiceEntitySetProperty>()
                    .Where(n => n.Name == entitySetName).SingleOrDefault().Value;
                Type entityType = entitySet.Details.EntityType;

                var projections = VsExportProviderService
                    .GetExports<IProjection, IProjectionMetaData>()
                    .Where(p => p.Metadata.SourceEntityType.Equals(entityType));

                List<string> result = new List<string>();
                foreach (var item in projections)
                {
                    result.Add(item.Metadata.ProjectionName);
                }

                var response = Request.CreateResponse<IEnumerable<string>>(HttpStatusCode.Accepted, result);

                return response;
            }
        }
    }
}

The VsExportProviderService gives us a list of potential export types (projection strategies) for our source type (in my case here: customers).

This time I get in the request parameters via a dedicated class:

 public class GetMetaDataCommandParameters
    {
        public string DataServiceName { get; set; }
        public string EntitySetName { get; set; }
    }

How can we consume this in the SilverLight client?

I’m basically using my nuget package for web-api based commanding. By doing so the client side call is very simple is robust:

 

partial void GetProjectors_Execute()
        {
            this.StartWebApiCommand<IEnumerable<string>>("api/ExportMetaData/GetProjectors",
                new GetMetaDataCommandParameters { DataServiceName = "ApplicationData", EntitySetName = "Customers" },
                 (error, response) =>
                 {
                     if (error != null)
                     {
                         this.ShowMessageBox("error = " + error.Message);
                     }
                     else
                     {
                         string text = "Available export formats : " + Environment.NewLine;
                         foreach (var item in response)
                         {
                             text = text + item.ToString() + Environment.NewLine;
                         }
                         this.ShowMessageBox(text);
                     }
                 }
                 );
        }

Conclusion

Here is the full code: CSVExport

The above is a prototype. Of course, what you want is that the user can select a certain export projection strategy by means of a fancy selection screen.  But it should be clear the MEF has also for LightSwitch a very prominent place.