locked
List in ASPNET Core MVC RRS feed

  • Question

  • User2041008840 posted

    Hello, 
    Just quick question, 
    I am always use Country State and City data with is stored in database. So I call through api which is come from database. 
    its already saved and at project level it  has model controller. 


    As we know that country state or city is static is does not change. So Can I use it as strongly type list of country state city  list and call them. 
    So I will not save this into database. 

    public class CountriesController : ControllerBase
        {
            List<Country> Countries = new List<Country>
            {
            new Country { ID = 1, Name = "Afghanistan" },
            new Country { ID = 2, Name = "India" },
            new Country { ID = 3, Name = "Pakistan" },
            new Country { ID = 4, Name = "China" },
            };
    
            List<State> States = new List<State>
            {
            new State { ID = 1,Name = "Baghdad", CountryID = 1 },
            new State { ID = 2,Name = "New Delhi" , CountryID = 2},
            new State { ID = 3,Name = "Karachi", CountryID = 3 },
            new State { ID = 4,Name = "Shanghai", CountryID = 4 },
            };
            // GET: api/<CountriesController>
            [HttpGet]
            public IEnumerable<CountryViewModel> Get()
            {
                return (from c in Countries
                        join s in States
                        on c.ID equals s.CountryID
                        select new CountryViewModel
                        {
                            CountryName = c.Name,
                            StateName = s.Name,
                        }).ToList();
    
    }
            }

    or I just save into database already and call them. So the my question which one is work faster. 
    strongly typed then call or Saved in database on server then call? 


    and btw I am saving Country as ID using its ID. If i am creating Customer and saving its info into database. 
    just saving ID of Country state or city because to load data faster maintain relation between country and customer



     

    Monday, September 28, 2020 6:00 PM

All replies

  • User475983607 posted

    Caching is used for persisting data that does not change often.

    Monday, September 28, 2020 6:06 PM
  • User-1330468790 posted

    Hi Prathamesh shende,

     

    So the my question which one is work faster. 
    strongly typed then call or Saved in database on server then call? 

    Obviously the strongly typed way would be working faster since you don't need to cost any time/resource on database connection and query (IO cost).

     

    There are two common ways to do cache in order to improve the performance for those data which are accessed frequently and not changed quickly.  

    • Data Caching : Data caching means caching data from a data source. As long as the cache is not expired, a request for the data will be fulfilled from the cache. When the cache is expired, fresh data is obtained by the data source and the cache is refilled.
    • Object Caching : Object caching is caching the objects in page or controller. The cached data is stored in server memory.

    For your scenario, since you don't need to change the data for countries and states (they are common sense), storing them directly in the objects is a good method as long as they are consistent with the database (like customer related data).

     

    Hope this can help you.

    Best regards,

    Sean

    Tuesday, September 29, 2020 2:12 AM