0

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)?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Tequilalime
  • 611
  • 9
  • 29

2 Answers2

1

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.

Euphoric
  • 12,645
  • 1
  • 30
  • 44
  • Service has the responsibility of reading / writing data and server role applications should be able to query this service to access their stored objects. Computations may or may not be required on the service, but server applications can modify their own data. I don't like the idea of storing pure strings, was hoping to achieve it by binding this data to generic objects / classes or types. – Tequilalime Dec 29 '12 at 12:31
  • @Alaminut Generic types are only good for compile-time checking. Are you gonna define all the datatypes at compile time? Are you gonna extend the service by simply adding dlls? No? Then I see no problem with strings. Or maybe Json documents. – Euphoric Dec 29 '12 at 12:34
  • How will the client know what type of data it gets from the server then? Let's say I've launched this application for restaurants to broadcast their menus and promotions to clients, how will I add support for pharmacies later? Like, they can announce their contracted hospitals etc..? – Tequilalime Dec 29 '12 at 12:38
  • @Alaminut Client must tell you what kind of data it intends to read/write/modify. You should definitely check out document databases like RavenDB. – Euphoric Dec 29 '12 at 12:44
  • It'd be correct if this was the case. But your response gave me the idea. Contrary to what you said, my server should tell what kind of data it's sending so that the client will know what to expect. I'll check RavenDB thanks. – Tequilalime Dec 29 '12 at 12:49
  • @Alaminut I really don't see how client like that works. It's like getting arbitrary data from SQL database instead of what you ask for. – Euphoric Dec 29 '12 at 13:00
  • Think of client as an iPhone app which requests data of an object when you hold it's camera to it. So the client doesn't know what kind of object or data it's trying to query. Database queries will be held by service, which, server application can access. – Tequilalime Dec 29 '12 at 16:13
  • @Alaminut So it requests all data or what? It still needs to know WHAT object it requests data off. And if it knows this, then already knows what kind of object it is. – Euphoric Dec 29 '12 at 16:29
  • Server is continously broadcasting, client is basically suppose to act like a receiver. So a client's job is (aside of parsing data etc...) scan for server's broadcast message around and parse the data it sends. That's why it should be -sort of- a generic client. If possible of course. – Tequilalime Dec 29 '12 at 16:58
  • @Alaminut "continuously broadcasting" Now thats interesting. Are you sure you want webservice, that is pretty much definition of request/response ?? – Euphoric Dec 29 '12 at 18:38
  • I think yes, servers get their broadcast data from a web service (this request is not continuous) and then start broadcasting this data once they run. Clients get this broadcast message when they get in range of a server (eg: when a person gets in 100m of a restaurant) and displays a message like "x broadcasts available near you" and then display a list of "objects". Here, server's broadcasting message have a common property called "BroadcastTitle" this is the -presumably- only common property of a server broadcast. Rest of the broadcast data is up to server itself. – Tequilalime Dec 29 '12 at 19:20
1

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.

Marco Klein
  • 683
  • 5
  • 19
  • 1
    Hmm, yeah nice idea. And for the extended security, i can encrypt the stored string etc... Thanks for the advice. – Tequilalime Dec 29 '12 at 12:52