I've developed a Restful API in PHP, now the API makes different resources available like: Article, User, Service, Trace etc...
.
I'm going to implement each resource in a portable DLL, so I can use the API method in each platform as Xamarin, XBox, Windows etc…
I want to ask how I should setup the structure of each DLL.
I'm thinking to use the following stack:
The project name within the solution is the name of the resource, for example Article
.
Now I have a class called Article
. The Article
resource implements other sub-resources such as Fam
and List
. So in the same DLL I can also use the method of other sub-resources.
In the solution, for simplicity, I separated each sub resource in their class as:
Article, Fam and List
My RestFull API makes these verbs available: GET - DELETE - POST - PUT
. So in my DLL I've created 4 folders with the smen name as the available verbs.
In each folder, for example (GET), I have the class resource and sub_resources:
Article_GET, Fam_GET and List_GET
In the classes above I have only the GET method request.
In the main class, for example Article
I implement all GET - DELETE - POST - PUT
classes methods, little example:
public class Article_GET
{
public class Article
{
public string codice { get; set; }
public string descrizione { get; set; }
}
public class RootObject
{
public List<Article> article { get; set; }
}
public List<Article> GetArticle()
{
var obj = JsonConvert.DeserializeObject<RootObject>("json");
return obj.article;
}
}
}
and this is the Article
class:
public class Article
{
public static List<Article_GET.Article> GetArticles()
{
return new Article_GET().GetArticle();
}
}
so when I import the DLL that has this namespace: CompanyName.Product.Article
I can simply use: Article.GetArticles();
or Article.AddArticle()
etc…
Image stack example:
I don't know if is this a good stack classes hierarchy. Could someone tell me how I can improve this? Thanks in advance.
Best Answer
This looks like it will eventually bite you in the behind, as in that it's very cluttered and doesnt really provide good maintainability.
I would have organized it like this:
Each resource (model) gets its own class file containing its attributes, like:
Model/Article.cs
Each resource collection has a class containing methods for handling that resource, like:
Resources/Articles.cs
Here is a dotnetfiddle example: https://dotnetfiddle.net/YE1hc1