Question

I am using ASP.NET MVC, building an ERP with Framework 4.0 and SQL Server 2008.
My question is where should query be placed? I saw multiple methods while surfing. Few of them are as follows:

  • In Models
  • In Separate Files, that are in DAL Folder
  • In Separate DAL Project

I am still confused about using queries in Models after research.

Further Info:
I am using simple query method, not Linq and Entity Framework.
I have a plan to also convert this project to Desktop Application in future.

Another Question is that How business Logic works in ASP.NET MVC if I use it?

Detail About Project
It is a Website + Online Information System of a Company with around 40-50 pages of both.
User can enter in Info System by Signing in from website.

Before Voting to Close Leave a comment so that I can learn something.

Was it helpful?

Solution

Your question is about infrastructure of a project that depends on lots of factors.

Considering the information you have provided in your question:

I have a plan to also convert this project in Desktop Application in future.

for the sake of usability, you should have your Data Access Layer in a separate project.

If you are interested in obtaining more information about the infrastructure of a project, I recommend to you: Microsoft Spain - Domain Oriented N-Layered .NET 4.0 Sample App that is well documented and gives you much information that you need to produce an enterprise level application.

OTHER TIPS

The idea behind MVC is SEPARATION. I don't think very much IF ANY logic or data access should go in the controllers at all. Most sites and applications that I create end up using an API project so it makes it pretty easy to throw the DB access in there. It's really up to you in the end and comes down to one question: Do I want an organized solution?

It's a touchy subject with most, but I've found through my experience that having a more organized Solution/Project will make for a better practice later. Since you plan to convert to a desktop application later, I would highly suggest moving the DAL to a separate project using an API. This way you only have to write the code once.

I think there is no mustdo rule. Everyone has his own approach. In my applications, I'm creating Helpers folder with static helper classes. For example, create class DBLogic:

public static class DBLogic
{
   public static List<Clients> GetAllClient()
   {
     //**your query here
      return //**some result
    }
}

Than in your Controller, when you need list of Clients

public ActionResult Index()
{
  var clients = DBLogic.GetAllClient()

  return View(clients);
}

This is my approach. Enough logical as for me.

In all my jobs so far we create a seperate DAL project to put all our database calls in. But I mean I think its really just for organization and keeping things seperated. If its a smaller project I think just putting it in a seperate class would be fine. Im not sure about in the model though, doenst make sense to me.

You can create a DAL folder Or you can create a separate DAL Project to write your Data Access Code

Lets say I am in shopping cart scenario which involves following steps:

  1. Get Cart from database
  2. Calculate tax
  3. Total everything

Project.Core(Has all domain classes)

public CartItem
{
   public int Id { get; set; }
   public string Desc { get; set; }
}

public CartDetails
{
   public int Id { get; set; }
   public List<CartItem> CartItems { get; set; }
   public Decimal Tax { get; set; }
   public Decimal Total { get; set; }
}

Project.DAL

public class CartDAL  
{
  public List<CartItem> GetCart(int cartId)
  {
     //execute sql here, get datareader or datatable
     //loop thru the rows and mait into a List<CartItem>

     return List<CartItem>
  }
}

Look into Dapper or Massive, instead of manually like above.

http://code.google.com/p/dapper-dot-net/

https://github.com/robconery/massive

Project.BLL

public class CartBLL
{     
  CartDAL cartDAL = new CartDAL();
  public CartDetails GetCartDetails(int cartId)
  {
     var cartDetails = new CartDetails();
     cartDetails.CartItems = cartDAL.GetCart(cartId);
     cartDetails.Tax = cartDAL.GetCart(cartId);
     cartDetails.Total = cartDAL.GetCart(cartId); 
     return cartDetails;
  }
}

Project.Web

public class CartController
{
   CartBLL cartBLL = new CartBLL();
   public ActionResult Index(int Id)
   {
       var cartDetails = cartBLL.GetCartDetails(Id);

       // Make a cartViewModel

       View(cartViewModel);
   }
}

Project.WPF

//Nothing much changes in desktop comapared to Web, just call the BLL stuff and you are set
CartBLL cartBLL = new CartBLL();
var cartDetails = cartBLL.GetCartDetails(Id);
//Populate view

So, basically you have to reuse your BLL in all the front end projects

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top