aliselmes / myrecipebook

A project to create a Recipe Book web app using .NET Core as part of my mentorship at Blend Interactive.
GNU General Public License v3.0
0 stars 0 forks source link

Build a Database Service #6

Closed Nhawdge closed 4 years ago

Nhawdge commented 4 years ago

When we work with .NET MVC we have several layers. The front end is split into MVC, Model View Controller. We worked with knockout to handle the Model. We're using HTML/KO to handle the View, and we've got .NET MVC Handling the controller portion. That covers the bulk of the front end of the site.

Now our back end is a bit different and we need 2 key layers. The code portion, and the data portion. A couple weeks ago we (you) build out a couple database tables, Now we want to make our data layer accessible via code. Normally the best way to handle this is with a service. A service isn't some magically word or tool, it's just a way to describe code.

Take this example service below

// Our database should handle CRUD
public static class DataService {
  public static Get(string id) {
    // Code to get data C _R_ U D
  }

  public static Save (object data) {
    // Code to save data _C_ R U D
    // OR If data already exists, C R _U_ D
  }

  public static Delete (string id) {
    // Code to Delete data C R U _D_
  }

}

When we work with abstracting data, it's important to use Interfaces They allow another developer to mimic your code for another system. For example, what if we switched from Mysql, to SQL Server, But we wanted both to be available. Well we can use an interface to handle interactions without rewriting every connected piece of code.

YOUR GOAL

is to implement the following interface to any degree of success

public interface IDatabaseConnection {
  static bool Save (object data);
  static object Get (string id);
  static bool Delete(string id);
}
Hints (Spoilers) * [x] Create a new file for your interface and paste the above code * [x] Create a new folder and file for your service. * Note where the words 'static' are at * [x] Add the interface to your class `class MyDataBaseClase : IDatabaseConnection ` * [x] Handle the errors until it builds.
aliselmes commented 4 years ago

ok so I've created a new branch called "interface" to work on the database service. I also did a bit of reading around CRUD and have a few questions before launching into the code:

  1. Am I right in thinking that I will need to use POST, GET, PUT, and DELETE methods in the service?
  2. For the new interface file, which folder should this be created in? The Services folder?
  3. When Adding the interface to our class, this code should be written in at the top of the Services file in the same line that we declare the class?

Still getting my head around this but I think everything is starting to coalesce in my brain (?!)

Thanks!

Nhawdge commented 4 years ago
  1. Yes, but normally we just use post/get requests, if you want to get fancy we can do more.
  2. Yes.
  3. Yes.

Also, haha you're learning!

aliselmes commented 4 years ago

haha uh oh learning?! Just getting into the process a bit more:

Before writing the Get/Post methods in the DatabaseServices file, will i need to create some new View Models in the Models folder? e.g RecipeViewModel, InstructionsViewModel, and IngredientsViewModel

Then in each one create a model class? e.g. :

public static class RecipeViewModel
{
public string id { get; set; }
public string name { get; set; }
public date dateAdded { get; set; }
public string author {get; set;}

public InstructionsViewModel Instructions { get; set; }
public IngredientsViewModel Ingredients { get; set }
}

I may be waaay off track but I found a tutorial online I am trying to follow/apply to our recipe tracker...

Thanks!

Nhawdge commented 4 years ago

Yes, this is totally correct and is pretty much our next step. We need C# models to connect to the Knockout models we built.

aliselmes commented 4 years ago

Hi John, I'm afraid I haven't been very successful in the challenge this week...I have tried to read about creating GET and POST requests in C# but have ended up going round in circles and haven't managed to really get started :s Perhaps we could go through this on Sunday?

Nhawdge commented 4 years ago

That sounds great. We'll put it on the agenda!

Nhawdge commented 4 years ago

I'm calling this completed. I'm gonna open a new ticket for making the rest work