Git Product home page Git Product logo

Comments (10)

mariodivece avatar mariodivece commented on August 17, 2024 1

Ok, @bufferUnderrun could you please give it a shot now?
Aslo @geoperez could you please create a few unit tests?

from embedio.

mariodivece avatar mariodivece commented on August 17, 2024

Hey, you are correct, and this is precisely the purpose of this project being public. I think I have a few ideas. Let me work on them

from embedio.

mariodivece avatar mariodivece commented on August 17, 2024

Just as a side note regarding the extension method to parse request: I admit I wrote this really quickly and I was surprised to see how low quality it was (my apologies). Hopefully this new version alleviates a lot of the problems some users may be having. I have obsoleted the methods currently in use.

from embedio.

bufferUnderrun avatar bufferUnderrun commented on August 17, 2024

@mariodivece i'm away from computer all this week-end... I could only test it on monday.

Before your fix, i was starting to use the same approach by replacing Dict<string,string> with Dict<string, object>(), so same idea ;)

IMO, the solution is not obvious because when using the new RequestFormDataDictionary() method, it implies parsing the value to check if it's a string or list. I'm wondering if there is a better way to do this...

Finally, for the rare use case (select/multiple), it seems to works.

from embedio.

mariodivece avatar mariodivece commented on August 17, 2024

I left the Dictionary of string, string methods available for users to still access. The difference is that it now converts arrays into NewLine-separated values of strings. While not ideal, it still works much better than the previous method. Furthermore, I need to find some time to support deep object parsing with something like a dynamic (i.e. Expando object) result. This way I could easily serialize into JSON and deserialize into a user-specified type.

from embedio.

bufferUnderrun avatar bufferUnderrun commented on August 17, 2024

Last post before week-end ;)

A few months ago, i wrote an UpdateModel() Extensions which is very similar to the UpdateModel() of the Asp.net MVC project. The basic idea was to simplify migration from my Asp.net MVC 5 projects to the new stack composed of embedio+riotjs. I've attach the snippet to this post.

The UpdateModel() will map all postdata to an object properties. It takes optionnals parameters to include/exclude properties or force additionnals settings to the json method used for this mapping (Newtonsoft.Json).

Once the UpdateModel() extension is loaded, this is how i use it :

/**
* User Controller (create/read/update/delete users)
**/
public class UserController : WebApiController {

  // repositories (deal with sqlserver database)
  private UserRepository _user = new UserRepository();

  /**
  * POST: create
  **/
  [WebApiHandler(HttpVerbs.Post, RestApi.RelativePath + "user/create")]
  public bool Create(WebServer server, HttpListenerContext context) {
    var user = new User(); // new empty instance of user class from user repository

    try {
      // map post date to user class instance
      context.UpdateModel(user);
      // save to db
      _user.Add(user);
      _user.Save();
      // return to ajax caller
      return context.JsonResponse(user);
    }
    catch (Exception ex) {
      return context.JsonResponse(ex);
    }
  }

  /**
  * POST: user/update/{id}
  **/
  [WebApiHandler(HttpVerbs.Post, RestApi.RelativePath + "user/update/{id}")]
  public bool Update(WebServer server, HttpListenerContext context, int id) {

    // Retrieve existing user from User Repository
    var user = _user.Get(id);
    if (user == null) {
      return NotFound(context);
    }        

    try {
      // updatemodel will populate user with post data except for usr_id and usr_login which i don't want to be changed
      context.UpdateModel(user, null, new[] { "usr_id", "usr_login" }, new Newtonsoft.Json.JsonSerializerSettings() {
        // force some settings to the json serialiser (ie french date format)
        Culture = new System.Globalization.CultureInfo("fr-FR")
      });
      // save to db
      _user.Save();
      // return to ajax caller
      return context.JsonResponse(user);
    }
    catch (Exception ex) {
      return context.JsonResponse(ex);
    }
  }

} // end class

The UpdateModel() support many arguments and can even map List<Object>(). Suppose you have many users to add in the same post data :

<input type="text" name="user[0].usr_name" value="foo" />
<input type="text" name="user[0].usr_mail" value="[email protected]" />
<input type="text" name="user[1].usr_name" value="bar" />
<input type="text" name="user[1].usr_mail" value="[email protected]" />
var post = context.RequestFormData();
var users = new List<User>();
post.UpdateModel(users, "user"); // "user" is the input name prefix to all fields related to users in the html
// users then contains 2 User object map with the post data

UpdateModel.txt

from embedio.

bufferUnderrun avatar bufferUnderrun commented on August 17, 2024

@mariodivece I'm late but i've tested and, of course, your patch fix the issue. I will modify my extend method UpdateModel() to handle the new type Dict<string, object>().

Thanks for your support

from embedio.

mariodivece avatar mariodivece commented on August 17, 2024

I really need to get deep object parsing right on this one. I will work on it as soon as I find some spare time

from embedio.

mariodivece avatar mariodivece commented on August 17, 2024

Example implementation
https://github.com/mariodivece/amcresthttp/blob/master/Amcrest.HttpApi/Models/Node.cs

from embedio.

stale avatar stale commented on August 17, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

from embedio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.