简体   繁体   English

REST-良好的设计规范

[英]REST - Good design practice

I am completely new in Windows Phone development and I am trying to write an app that retrieves the data from a server and displays them to the user. 我在Windows Phone开发中是一个全新的人,我正在尝试编写一个从服务器检索数据并将其显示给用户的应用程序。 I have several resources on the server, lets say User, Quest and Activity. 我在服务器上有几个资源,可以说用户,任务和活动。 I use RestSharp lib to retrieve the data from the server. 我使用RestSharp lib从服务器检索数据。

Example of Get User: 获取用户示例:

public void Get(String id, LifeHunt.MainPage.UserReady userReady)
{
   var client = new RestClient(Deployd.REST_URL);
   var request = new RestRequest(resource + "/{id}", Method.GET);

   request.AddUrlSegment("id", id);

   client.ExecuteAsync<User>(request, response =>
   {
      if (response.StatusCode == System.Net.HttpStatusCode.OK)
      {
         userReady(callback.Data);
      }

   });
}

Once the user is retrieved, I call the userReady method I passed as callback and get the user to MainPage to display it. 检索到用户后,我将调用作为回调传递的userReady方法,并将用户转到MainPage进行显示。

Now, I have to repeat the whole process for all CRUD (Insert, Get, GetAll, Update, Delete) functions for all Users, Quest and Activity. 现在,我必须对所有用户,任务和活动的所有CRUD(插入,获取,GetAll,更新,删除)功能重复整个过程。 That means I will need 15 different callback methods, which I think is not a good software design. 这意味着我将需要15种不同的回调方法,我认为这不是一个好的软件设计。

The other way would be just one callback method and check the type of parameter passed in the callback method. 另一种方法将只是一个回调方法,并检查在该回调方法中传递的参数的类型。 However I do not think this is a nice solution as well. 但是我也不认为这是一个很好的解决方案。

I was thinking about something like creating a generic interface for CRUD, implement it by all the User, Quest and Activity classes: 我正在考虑为CRUD创建通用接口,并通过所有User,Quest和Activity类实现该接口:

interface ICRUD<T>
{
   void GetAll(GenericCallback callback);
   void Get(GenericCallback callback);
   void Add(T item, GenericCallback callback);
   void Remove(String id, GenericCallback callback);
   void Update(T item, GenericCallback callback);
}

However I do not really know how to implement it and even if it is a good way. 但是,即使这是一个好方法,我也不知道如何实现它。 Could somebody suggest me a good design solution? 有人可以建议我一个好的设计解决方案吗?

Use MVVM, create a ViewModel that would hold all the data you need and bind it to the View. 使用MVVM,创建一个ViewModel,它将保存您需要的所有数据并将其绑定到View。 Then create Service class with methods directly returning the data you need (no callbacks). 然后使用直接返回所需数据的方法(无回调)创建Service类。 Create a instance of this Service in the ViewModel and call the methods to get and fill the data when needed. 在ViewModel中创建此Service的实例,并在需要时调用方法来获取和填充数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 部分类的使用是一个很好的实践/设计吗? - Is the usage of partial classes a good practice/design? 异常处理的良好实践设计模式 - Good practice design pattern for Exception handling REST API HttpClient返回响应的良好做法 - REST API HttpClient return response good practice 使用Interface +抽象类进行继承设计。 好的做法? - Inheritance design using Interface + abstract class. Good practice? 只有一个无参数的基类构造函数是一个很好的设计实践吗? - is it good design practice to only have a parameterless base class constructor? 在DDD中为每个服务类设置一个接口是一个很好的设计实践吗? - Is it a good design practice to have an interface for each service class in DDD? 在Windows 64位下设计c#应用程序是一种好习惯吗? - Is it good practice to design c# application under windows 64 bit? 这是好习惯吗? - Is this good practice? REST API在现有.NET C#网站中的设计实践 - Design practice for REST API into existing .NET C# website 如果您有多个应用程序,将用户存储在一个表中,并用一列区分应用程序是一种好的设计做法吗? - If you have multiple Applications, is it a good design practice to store users in one table with a column that differentiate the Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM