Tequilalime
Tequilalime

Reputation: 621

Ideas to implement generic web service

I'm thinking of a mid / large scale project, which will need to store different data types and present them to different clients.

What I'm struggling now is, how to build a data and service layer that can capable of storing different types of objects and query them when needed.

As an example, think of a client - server application in which, clients can only read each individual server's broadcasts, and now think of a scenario where a flower shop and restaurant broadcasts their data to a person on the street with a smart phone.

class SoCalledServer
{
    public AccessibleClientData Broadcast(ClientData broadcastMessage)
    {
        Broadcast(broadcastMessage)
    }
}

class RestaurantClient : AbstractClient
{
    public SomeGenericDataType menu;
    public RestaurantClient()
    {
        menu = new SomeGenericDataType<List>();
        menu.Add("Sushi");
        menu.Add("Fried potatoes");
    }

    public override void BeginBroadcast()
    {
        server.Broadcast(menu);
    }
}

class FlowerShopClient : AbstractClient
{
    public SomeGenericDataType flowersOnSale;
    public FlowerShopClient()
    {
        flowersOnSale = new SomeGenericDataType<List>();
        flowersOnSale.Add("Daisy");
        flowersOnSale.Add("Rose");
    }

    public void BeginBroadcast()
    {
        server.Broadcast(flowersOnSale);
    }
}

In this example, I have two different types of data (one is a restaurant's menu, and the other is flower shop's flowers) which can have different members of its own (eg. menu has prices and ingredients, flower shop's data has flower names and a description, quantity and / or price etc...) and this "client" samples can be extended.

How should I model such type of application? What kind of database schema I should use to store unidentified and various types of data? How my server and client application should communicate with each other? And the most important how should client get the broadcasted data type (from the generic type)?

Upvotes: 0

Views: 1137

Answers (2)

Marco Klein
Marco Klein

Reputation: 699

Another idea is to de/serialize them as XML or Json. Some hints:

        // get stuff here
        String json = GetJsonString(expression));

        List<T> result;
        using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
        {
            var serializer = new DataContractJsonSerializer(typeof(List<T>));
            result = (List<T>)serializer.ReadObject(ms);
        }   

Or XML: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

You can convert the objects to XML/Json for transmission. Then, for storing, deserialize them as anonymous objects (no specified class) on the side where the class is unknown.

With this, you have always the possiblity to store all of the data even when the classes are unknown. Everywhere, everytime.

Upvotes: 1

Euphoric
Euphoric

Reputation: 12849

How will this service manipulate this data? Will it only save it? Will it do some computations with this data? Your description is too generic.

Assuming you only want to write/persist/read data, you can simply save strings and let client do the parsing themselves. You can query based on id. Key/value and document databases work like this.

For anything more, you should think what the responsibility of the service should be and design the internal structure accordingly.

Upvotes: 1

Related Questions