I can not figure out how everything is correct, from the controller to transfer data from the database to the class or still in the class to get access to the database? For example: My class:
public class RssFeeds { [Key] public int RssFeedsId { get; set; } public string Indeficator { get; set; } [Display(Name = "Источник" )] public string Source { get; set; } [Display(Name = "Название новости")] public string TitleNew { get; set; } [Display(Name = "Описание новости")] public string News { get; set; } [Display(Name = "Дата публикации")] public string Date { get; set; } }
My context:
public class DataBaseContext : DbContext { public DataBaseContext(DbContextOptions<DataBaseContext> options) : base(options) { } public virtual DbSet<RssFeeds> RssFeeds { get; set; } }
And here I am implementing a class that performs sorting, and I would like it to get data from the database, sort it and put it in the collection, and then return these collections to me.
public class Sorted : ISorted { public List<RssFeeds> getSortDateHabr() { List<RssFeeds> list = db.RssFeeds.OrderBy(p => p.Date).Where(m => m.Indeficator == "H").ToList(); return list; } public List<RssFeeds> getSortDateInterFax() { List<RssFeeds> list = db.RssFeeds.OrderBy(p => p.Date).Where(m => m.Indeficator == "I").ToList(); return list; } public List<RssFeeds> getSortSourceHabr() { List<RssFeeds> list = db.RssFeeds.OrderBy(p => p.Source).Where(m => m.Indeficator == "H").ToList(); return list; } public List<RssFeeds> getSortSourceInterFax() { List<RssFeeds> list = db.RssFeeds.OrderBy(p => p.Source).Where(m => m.Indeficator == "I").ToList(); return list; } public List<RssFeeds> getSortSDateAll() { List<RssFeeds> list = db.RssFeeds.OrderBy(p => p.Date).ToList(); return list; } public List<RssFeeds> getSortSourceAll() { List<RssFeeds> list = db.RssFeeds.OrderBy(p => p.Source).ToList(); return list; } }
Only I do not understand how to make it so that the class could access the database? Or should I transfer the data from the database through the controller to this class? THOSE. in the controller, get the entire database, put it into the collection and transfer this collection to the class? How should this be implemented correctly in ASP.NET? It would be a good example of a real one, because in all the textbooks that I saw, all the logic is implemented in the controller.