c# - Where to call database in method on model? -
In an MVC app with unit framework 4, I try to add some more code to schedule
I am the ideal. At present, a method on this model is called the code to get the list of dates that are not at the end of the week. The model is called the controllers in this way var freedata = schedule. GateDettyTiltressDid ()
.
To do the above, no database call is necessary, I want to add a method which checks that the date is "Structural Free Day" (free every year). This method needs to create a database call because the free day will be stored in the Structural Friede
model via String Day Mean
(maybe I use date time The tax and the year will ignore the part, though the leap will look out for the year - this is another problem) and string reason
.
// Get the list of dates for the next 30 days) who are eligible for duty, static static ICollection & lt; DateTime & gt; GetDatesNextThirtyDays () {ICollection & lt; DateTime & gt; PlannableDates = New List & lt; DateTime & gt; (); Var date = DateTime.Today; For (var i = 0; i & lt; 29; i ++) {if (!!! (date!) And; easertical free day (date)) // & lt; = New method {PlannableDates.Add (date); } Date = date Add days (1); } Return planable dates; } // Return is true that the day is not Saturday, Sunday Public Static Bull ISKCend (Datetime Day) {Return day.Date.DayOfWeek == DeFoyeke Saturday || day. date. Diaf wake == day off }
I like to add new method bool ISStructuralFreeDate
to the GetDatesNextThirtyDays
method with the database call, because it is used . Is it OK to instantify dBContext
and calls the database in the model? Or is there a great solution?
Great question user2609980 (great name too;)) As with any meta question, Depends very much on timeline and internal standards because it treats any "best practice".
What matters to it, my tendency to be in place will be the controller. The model for me should be as close to the property bag as possible. If there is a reason to argue in a model, then focus on changing the existing data on the model through an additional reading-only parameter. I also find the controller to keep all UOW management cleaner where the DI can be used to provide a proper UOW example, whether from the site or from the test.
Hope that helps!
Comments
Post a Comment