is it possible to convert data retrived from REST to kentico defined class objects?

benyamin jain asked on September 5, 2016 08:02

HI I am working on a very simple windows application that uses kentico RESTFull to manage data.

I know it is possible to use Kentico cms API in windows form application but now i want to combine REST with kentico classes.

for example when i retrive user information i want to deserialize results into UserInfo class object and then us it.

now my question , is it posible such action?

for example :

public UserInfo GetUser(string username)
        {
            var request = new RestRequest();
            request.Resource = "cms.user?where=Username LIKE '" + username + "'";
            return Execute<UserInfo>(request);
        }

        public T Execute<T>(RestRequest request) where T : new()
        {
            var client = new RestClient();
            client.BaseUrl = new Uri(BaseUrl);
            client.Authenticator = new HttpBasicAuthenticator(mUsername, mPassword);

            var response = client.Execute<T>(request);                       

            if (response.ErrorException != null)
            {
                //ErrorHandler.HandleError(response.StatusCode);
                if (OnError != null)
                {
                    OnError(this, new EventArgs());
                }
            }
            return response.Data;
        }

Correct Answer

Richard Sustek answered on September 5, 2016 09:57

How about using AutoMapper to map your result from Rest and map it to UserInfo (or any other class for that matter) directly?

I've used auto mapper in other projects and it really is awesome.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Kristian Bortnik answered on September 5, 2016 08:51

The Kentico classes (such as UserInfo) still have plain properties, so yes - you can deserialize directly into objects of the UserInfo type.

As long as you have a UserID in the JSON you are deserializing, you will be able to update the UserInfo object.

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on September 5, 2016 08:51

Benuamin, this possible and quite easy to implement. See working example here.

2 votesVote for this answer Mark as a Correct answer

benyamin jain answered on September 5, 2016 09:49

But in that case (Client-server) using UserInfo needs ConnectionString and CMSContext.Init()

when i want to deserialize returned data to UserInfo it hangs.after some sort of research i found that when i try to deserialize data to UserInfo it try to check connection with SQL Server and becaus my windows application running locally it takes long time to return.

i test this with kentico cms 7 hotfix 91

0 votesVote for this answer Mark as a Correct answer

benyamin jain answered on September 5, 2016 14:08

I solved problem by using SQLQueryResult tools in conjunction with restsharp library

thank you all

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.