locked
New to .NET - Is this the correct way? RRS feed

  • Question

  • User-1339735780 posted

    After many hours on Youtube, I have started my first test project. (.NET Core MVC) But I find it hard to 'connect the dots'.
    The following code works, but I assume it could be (greatly?) improved...?

    Basically I want to show a product category and products within.
    I think loading the data could be improved? And I can't figure out how to implement an interface.

    Any thoughts/pointers?

    Controller:

    public IActionResult Index()
    {
       var data = CollectionData.GetCollectionById(1);
       return View(data);
    }
    

    DataAccess:

    public class CollectionData
    {
       public static CollectionModel GetCollectionById(int id)
       {
           SqlDataAccess sql = new SqlDataAccess();
           var p = new { id = id };
    
           var output = sql.LoadData<CollectionModel, dynamic>("collection_get_by_id", p, "DefaultConnection");
           output[0].CollectionProducts = sql.LoadData<CollectionProducts, dynamic>("products_get_by_collectionid", p, "DefaultConnection");
    
           return output[0];
       }
    }

    Models:

    public class CollectionModel
    {
        public string Title { get; set; }
        public string SubTitle { get; set; }
        public List<CollectionProducts> CollectionProducts { get; set; }
    }
    
    public class CollectionProducts
    {
        public string Title { get; set; }
        public decimal Price { get; set; }
    }

    I like the code to be 'best practice' for a complex solution - but without going 'over the top'
    All pointers are greatly appreciated!

    Thursday, January 28, 2021 9:39 PM