I have seen several Web - API projects samples for the last few days in order to improve my project's architecture.
What I have so far:
Project.WebApi - Mainly consists of controllers, scripts, etc.
Project.DAL - Contains my models and repository class.
And few more sectors which are irrelevant.
My questions are:
1. I have seen few projects which have their models AND repository classes together in DAL library, while others separate them. Is it a matter of opinion which one is better?
2. My controllers receive JObjects, therefore I found myself converting JObjects to data objects in my controllers more than once, and it seems so wrong to me. I would like to create a custom JSON convert class for each of my models. For such a class (and other similar classes to come), would it be fine to create a library named Common, or core? Or maybe I misunderstood the concept?
A code example:
var taskId = (int)taskstatus["Id"];
var status = (string)taskstatus["Status"];
var userCreated = (int)taskstatus["UserCreated"];
if (taskId == 0 || status == null || userCreated == 0)
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
var newTask = new Task{
Id = taskId,
userCreated = userCreated
...
}
Leaving all the code above in the controller looks wrong to me, the controllers should be more simple and cleaner.
3. The repository is as follows:
public class Context : DbContext
{
public DbSet<Tasks> Tasks { get; set; }
public DbSet<Projects> Projects { get; set; }
...
as simple as that. Again, back to my controllers - I find myself do many complex queries in my controllers, which again, I feel is wrong. I don't think that the controller should take care of such queries. What would be the best practice to deal with complex queries? Maybe I should make a new class which inherit my context class, overrides some of its parent class and do the complex queries there?