Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server side and client side method

I'm using ASP.NET with C# 2.0. I have created some objects for a database and each of these objects has properties which can be called natively or are called in a similar manner and create a RESTful JSON API from them.

I have a lot of tab-like things I like to call 'modules' on this site - the function of a module is to convert data to HTML to be displayed on the page. Idealy this needs to be done in both server side C# code for the first tab to load, then use Ajax to load the others when the tabs are clicked, however for old browsers and search engines, the tab is still a link that will load the same HTML code server side.

Currently I've writting the JavaScript code completely separately from the C# code that converts each module to HTML, but the method is virtually the same, just a different language. Similar to this example.

C# code

public override string GetHtml()
{
    IJsonObjectCollection<Person> response = ((Village)page).People;
    string html = "<div id=\"test\">";
    foreach (Person person in response)
    {
        html += "<div class=\"person\">";
        html += person.Name;
        if(canEdit) html += "*";
        html += "</div>";
    }
    return html + "</div>";
}

JavaScript code

function getHtml() {
    JsonRequest('/json/villages/1/people', function(response) {
        var html = '<div id="test">';
        for (int i = 0; i < response.length; i++)
        {
            var person = response[i];
            html += '<div class="person">';
            html += person.name;
            if(canEdit) html += '*';
            html += '</div>';
        }
        return html + '</div>';
    });
}

You can probably see where I'm going with this question. What would be the most efficient way of doing this? I was thinking of a few different alternatives -

1. Each ModuleToHtmlMethod could be a class that defines the method of turning this data object into HTML. I attempted this, but I stopped because I was getting too complicated.

2. Write my own scripting language that can be interpreted as C# but also 'compiled' into JavaScript code.

3. Just write the lot in C# and use Ajax to simply request the HTML content from C#

4. Keep the code separated and write every method twice.

I'd like to eventually allow other developers to write these 'modules', so maybe option 2 is the best option?

like image 597
Connell Avatar asked Jun 07 '11 09:06

Connell


People also ask

What is the difference between server and client?

In general, and in a simplest form, a server is a machine or a program (software application) that waits for incoming requests, and a client is a machine or program sending requests to another client or a server in order to take action.

What are the examples of client-side?

In web development, 'client side' refers to everything in a web application that is displayed or takes place on the client (end user device). This includes what the user sees, such as text, images, and the rest of the UI, along with any actions that an application performs within the user's browser.

What is client-side framework and server-side?

A server side framework typically uses a programming language with a compiler and runs on a Web server, such as Node, PHP and ASP.NET. The client-side framework is usually a JavaScript library and runs in a Web browser, such as React, Angular and Vue.


2 Answers

I would discard option 4 as it will make maintenance more difficult and you may end up out of synch between the HTML generated via the Javascript and the one from the C# code. I would also discard the option 2 as that may make the code more difficult for other developers and also probably unnecessary.

I would definitely generate the HTML in one place and probably expose RESTful HTML API that uses the C# existing function to return the HTML snippets. So from your Javascript you would call:

function getHtml() {
    MyHtmlRequest('/html/villages/1/people', function(response) {
        var html = response.Text;
        return html;
    });
}
like image 119
Giuseppe Romagnuolo Avatar answered Oct 17 '22 19:10

Giuseppe Romagnuolo


A couple of suggestions.

  • Have a generic GetHtml method that reflects the html. This can be hard as UI is not something that maps easily and uniformly to data fields.
  • Have a meta description of your 'modules', use this to create your generic GetHtml methods
  • Finally try this: It will allow you just to create JavaScript methods, you can then call them from C#

I would go for the second, meta description option as this is what I do for my data layers. I basically have a file defining my domain model. I use this to generate all my data acccess pocos, nhibernate config files, etc. I then have a meta data file which adds information to these objects, like UI rendering information and field validation info.

Tnx

Guido

like image 39
gatapia Avatar answered Oct 17 '22 20:10

gatapia