2

Without a doubt I know what the controllers and models are used for. However, I am able to write code that interacts with my db, for example adding users to a table, on either the controller or model. At what times should I write code in the controller vs. in model? Even though both work, what would be a more organized or practical way. Could you please post examples if the answer is ambiguous?Thx

1
  • 1
    "Without a doubt I know what the controllers and models are used for"... sorts of makes the question redundant, no? Commented Aug 16, 2013 at 22:13

3 Answers 3

5

For that, you should add a logic layer or logic classes. The controller should determine wants to do and can do, shuffle them in the right direction (logic layer), then determine what to show the user after the logic. Putting the logic in a separate layer will help keep your controllers lean and promote code reuse.

In the domain core, we only have models with properties. All logic is performed in a different layer, except for things like a property that returns fields concatenated in a format.

Sign up to request clarification or add additional context in comments.

1 Comment

Very good answer, as you say your approach promotes DRY as well as the ability to add other mechanisms, such as Singleton over Session/Application storage as well as worker pattern if you so wish. I added my $0.10 with an edit that reinforces your assertion of the role of the controller, I hope you don't mind.
4

Code to access the database should be in service layer instead of keeping in Controller or Model.

Accessing Database Entities from Controller

Here is my answer for the above question, you can also read others answers why you should keep in separate layer.

namespace MyProject.Web.Controllers
{
   public class MyController : Controller
   {
      private readonly IKittenService _kittenService ;

      public MyController(IKittenService kittenService)
      {
         _kittenService = kittenService;
      }

      public ActionResult Kittens()
      {
          // var result = _kittenService.GetLatestKittens(10);
          // Return something.
      }
   }  
}

namespace MyProject.Domain.Kittens
{
   public class Kitten
   {
      public string Name {get; set; }
      public string Url {get; set; }
   }
}

namespace MyProject.Services.KittenService
{
   public interface IKittenService
   {
       IEnumerable<Kitten> GetLatestKittens(int fluffinessIndex=10);
   }
}

namespace MyProject.Services.KittenService
{
   public class KittenService : IKittenService
   {
      public IEnumerable<Kitten> GetLatestKittens(int fluffinessIndex=10)
      {
         using(var db = new KittenEntities())
         {
            return db.Kittens // this explicit query is here
                      .Where(kitten=>kitten.fluffiness > 10) 
                      .Select(kitten=>new {
                            Name=kitten.name,
                            Url=kitten.imageUrl
                      }).Take(10); 
         }
      }
   }
}

2 Comments

Did you mean Where(kitten=>kitten.fluffiness >= fluffinessIndex)? :)
@TiesonT. Good catch. I just copied the code from the original question for demo. ;)
3

ASP.NET MVC and MVC, in general, is a presentation layer pattern; thus your interaction with the database should be in a layer beyond the presentation layer, usually a data-access layer, but it could be a service layer or business layer as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.