I am gradually converting a Classic ASP website to ASP.Net MVC. Within my Models folder I have further subdivided it into Business Objects (BO), Business Logic (BLL) and Data Access Layer (DAL) for basically a 3-tier architecture within Models. This seems to be working well so far with respect to database operations and it is keeping my controllers skinny. For example a typical controller would look like this:

public ActionResult Index()
{
    UserName UserDetails = UserManager.GetUserName();
    CarrierList CarrierList = CarrierManager.GetCarrierList();
    return View(new CarrierViewModel(CarrierList, UserDetails));                     
}

Basically I just use the controller to pass a function to the BLL layer which in turn interacts with the DAL & BO layers.

Where I am confused is the correct place for logic relating to non database operations such as file uploads. For example I am currently writing code to upload an Excel file and import its contents into a database table. If I were to follow the same pattern above, my controller should just contain a function to pass the file upload request to the BLL layer like this:

[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{   
    FileManager.ImportExcel(file);
    return RedirectToAction("Index");
}

The problem is that every tutorial or example I see for file uploads and Excel manipulation put their file and Excel code directly in the controller. eg here. Even Scott Hanselman's post here seems to imply that it is OK to put file upload logic in the controller.

My question is, given my 3-tier ASP.NET MVC architecture and fat model/skinny controller design goal, am I on the right track by offloading the upload and Excel manipulation logic to the BLL within the model? It seems consistent with what I am trying to do, yet since all the examples I see online just put the file manipulation logic in the controller, I feel like I am going against the grain.

有帮助吗?

解决方案

You should just get the stream and pass the stream to your BLL. That way your BLL is not depending on HttpPostedFileBase but just on a Stream.

Anyway, as it seems, your BLL code is inside your MVC application, which is not a very good idea. You should create a class library for the BLL, one for BO and one for DAL, and if possible wire them together loosely.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top