Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would probably take the service approach; You build your site with three main components:</p> <ul> <li>Presentation Layer<br> This is the MVC website (but could be a mobile site, and application, whatever)</li> <li>Service Layer<br> this handles the calls between the Presenation layer and the Data layer, applying business logic or whatever other checksums may be necessary (and keeping it away from the presentation layer)</li> <li>Data Layer<br> Here your <code>Entities</code> reside and the context to the database.</li> </ul> <p>For the sake of simplicity, you can keep this in one project, but if this becomes a big application you probably want to refactor it out in to separated libraries.</p> <p>Now, as far as how to refactor your situation <em>now</em>, we add the service layer in there. I like to use interfaces so it makes things easy to test later on I can implement a "dummy" <code>IWhateverService</code> when I go to unit test, but keep the current implementation when running the actual application. Then you implement the interface to interface with the data and return what you need (or perform whatever actions are necessary [CRUD]). e.g.</p> <pre><code>public interface IEventService { IEnumerable&lt;Event&gt; GetActive(); } public class EventService : IEventService { private readonly Entities entities; public EventService(Entities entities) { this.entities = entities; } public IEnumerable&lt;Event&gt; GetActive() { DateTime now = DateTime.Today; return this.entities.Events .Where(x =&gt; !x.removed) .Where(x =&gt; x.start_date &lt;= now &amp;&amp; x.end_date &gt;= now) .AsEnumerable(); } } </code></pre> <p>Now that we have our service, we can plumb it through to the controller:</p> <pre><code>public class EventsController : Controller { private readonly IEventService eventService; public EventsService() { this.eventsService = new EventsService(new Entities()); } // action that gets and views the active events public ActionResult Active() { var activeEvents = this.eventsService.Getactive(); return View(activeEvents); } } </code></pre> <p>As the project evolves, you can update your <code>IEventService</code> with CRUD operations (like I mentioned earlier):</p> <pre><code>public interface IEventService { IEnumerable&lt;Event&gt; All { get; } void AddOrUpdateEvent(Event event); IEnumerable&lt;Event&gt; GetActive(); void RemoveEvent(Event event); } </code></pre> <p>Of course plumbing it through to <code>EventService</code>, then finally having that access within the <code>EventsController</code>.</p> <p>To take it [several] steps further, you can look at Dependency Injection where you specify (one time) how to build an <code>IEventsService</code> then, when you need it, pass it as an argument to your controller's constructor (like so):</p> <pre><code>public OtherController : Controller { private readonly IUserService; private IEventService eventService; public OtherController(IUserService userService, IEventService eventService) { this.userService = userService; this.eventService = eventService; } /* actions */ } </code></pre> <p>Then, you can use something like Castle Windsor, ninject, or any of the other solutions that would involve a single map to these interfaces and then (magically) provide them to your controller's constructor for use. To give an example, here's a Castlewindsor configuration:</p> <pre><code>container.Register( Component.For&lt;IEventService&gt;().ImplementedBy&lt;EventService&gt;() .LifestyleSingleton() ); </code></pre> <p>Which essentially says that every time I need <code>IEventService</code> supply <code>EventService</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload