Guest User

Untitled

a guest
Nov 12th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. //import Linq for performing database methods to retrieve/create/update and delete data from your context class.
  5. using System.Linq;
  6. //import your Domain class library for using your Entity classes.
  7. using Games.Domain.Games;
  8.  
  9. namespace Games.Data.Repositories.Games.Impl
  10. {
  11. //Have your Games Repository class follow your IGamesRepository interface signature.
  12. public class GamesRepository : IGamesRepository
  13. {
  14. //Define a private variable of type GamesCOntext used to retrieve data from database.
  15. //Have it private since you are not using it outside this class.
  16. private GamesContext _context;
  17. //Define a constructor that will pass a context variable as a argument.
  18. public GamesRepository(GamesContext context)
  19. {
  20. //Have your private variable which will be responsible retrieving data from database.
  21. _context = context;
  22. }
  23. //Now define your methods that will be responsible for retrieving data..
  24. public IEnumerable<Game> GetGames()
  25. {
  26. //Assign a variable of your return type.
  27. //The toList method will convert your Games returned to a IEnumerable instead of List.
  28. IEnumerable<Game> games = _context.Games.ToList();
  29. //Now return the games
  30. return games;
  31. }
  32. //Now define a method that will responsible for retrieving one game by using a id as a argument.
  33. public Game GetGame(Guid gameId)
  34. {
  35. //Assign a variable of your return type.
  36. //Have it find a specific games based on a id via argument.
  37. Game gameToReturn = _context.Games.FirstOrDefault(game => game.Id == gameId);
  38. //Return the game retrieved.
  39. return gameToReturn;
  40. }
  41. //Now define a method that will be responsible for creating a new game via argument.
  42. public Game CreateGame(Game createdGame)
  43. {
  44. //We will add our created game to our Games property in our context which will insert it into our database.
  45. _context.Games.Add(createdGame);
  46. //Then we will save our changes in our context classes.
  47. _context.SaveChanges();
  48. //Now just return the created game.
  49. return createdGame;
  50. }
  51. //Now define a method that will be responsible for updating a game via argument, our service will return void .
  52. public void UpdateGame(Game updatedGame)
  53. {
  54. //Now update the game using your argument.
  55. _context.Games.Update(updatedGame);
  56. //Now save your changes to your context classes.
  57. _context.SaveChanges();
  58. return;
  59. }
  60. //Now define a method that will be responsible for deleting a game via a Guid argument, and will return void since our service is returning void.
  61. public void DeleteGame(Guid gameId)
  62. {
  63. //Find the game your are gonna delete using linq first or default method that will return the first element if it exists.
  64. //Compare each game's id to your gameId argument.
  65. Game gameToDelete = _context.Games.FirstOrDefault(game => game.Id == gameId);
  66. //Then if your game is not null delete that game.
  67. if (gameToDelete != null) _context.Games.Remove(gameToDelete);
  68. //Then save changes.
  69. _context.SaveChanges();
  70. //Return out of the function.
  71. return;
  72. }
  73. }
  74. }
Add Comment
Please, Sign In to add comment