Wednesday, December 10, 2008

CSLA & ASP.NET MVC (part 1: listing & viewing detail)

The easiest implementation for ASP.NET MVC is usually via LINQ to SQL. Now, for larger projects that must be scalable, one will need object development framework that is also scalable and more robust. My framework of choice CSLA.NET. Assuming you have built your CSLA classes, wiring it in with MVC is a piece of cake.

I recommend to make your view to be strongly typed to the object of your controller. So in my example, I have a CSLA class for "Customer" and also its corresponding read-only list, read-only item, editable, etc. Hence, I name my controller to be "CustomerController". In it, I created a class to carry around stuff as a single object, instead of stuffing everything in ViewData and cast when needed.

    public class CustomerControllerViewData
{
// list of read-only customer available to me
public CustomerInfoList CustomerList { get; set; }
// current editable customer seleted
public Customer CurrentCustomer { get; set; }
// list read-only Title available to the Customer
public TitleInfoList Titles { get; set;}
}

Then in the controller class, I declared a global variable to hold the instance of the CustomerControllerViewData class. I initialized it right away.
    public class CustomerController : Controller
{
CustomerControllerViewData viewData = new CustomerControllerViewData();

public ActionResult Index()
{
return RedirectToAction("FullList");
}
...
}

During list, I put all the necessary data into CustomerControllerViewData class, rest the validation, and then I pass that instance into the viewer as a strongly typed model. I do not fill in the CurrentCustomer property nor the Titles - since I won't be needing those to display my list of customers.
        public ActionResult List()
{
viewData.CustomerList = CustomerInfoList.GetCustomerInfoList();
viewData.ValidationErrorFlag = false;
return View("~/Views/Customer/Controls/CustomerListing.ascx", viewData);
}

The viewer looks like this - it should be pretty simple with the exception that it is strongly typed. So in the code behind, I have to add the type in the generic constructor of the viewer:
    public partial class CustomerListing : System.Web.Mvc.ViewUserControl
{
...
}

Now for viewing detail if a customer, we also do not need to fill out CustomerList property, since at this point we only care about a specific customer. Titles is needed since to populate a look up/drop down field.
        public ActionResult Detail(int id)
{
viewData.CurrentCustomer = Customer.GetCustomer(id);
viewData.Titles = TitleInfoList.GetTitleInfoList();
viewData.ValidationErrorFlag = false;
return View("~/Views/Customer/Controls/CustomerDetail.ascx", viewData);
}

Do the same thing for the code behind of the viewer to make it strongly typed:
    public partial class CustomerDetail : System.Web.Mvc.ViewUserControl
{
...
}


Related Posts:
ASP.NET MVC, AJAX, jQuery
CSLA & ASP.NET MVC (part 2)
CSLA & ASP.NET MVC (part 3)

4 comments:

Christopher Slee said...

Customer eh?

Johannes Setiabudi said...

Yes, it is from our customer demo project, Chris. Hehe

Anonymous said...

I am looking to use CSLA.NET with ASP.NET MVC but I assumed that I would ultimately run into some problems. Have you come across any issues in mixing the two?

Johannes Setiabudi said...

I have used CSLA & MVC in 3 different projects right now and so far so good. In some respect it turns out to be much better than I expected.