Let's rokk! [Tudor Cret's blog]

November 13, 2009

Developing ASP.NET web applications with SharePoint 2007 and CSLA.NET(part1)

Filed under: Technologies — Tudor Cret @ 3:49 pm
Tags: , , , , ,

I will try to summarize my experience with SharePoint development. We’ll see how we can develop a custom ASP.NET web application that is built using CSLA.NET. CSLA.NET is an application development framework  intended to help in reducing the cost of building and maintaining applications, but it depends where  and when it is used. With CSLA.NET we easily can use the same business tier for different presentation tiers (windows, web, web services ).

 SharePoint Development

 SharePoint is nothing but a smart ASP.NET 2.0 application which extends the ASP.NET framework by virtue of installing couple of custom components such as HTTP module, HTTP handler, Virtual Path provider, etc.

The end result? We have a provisioning engine that could be used for creating web sites based on the templates that come with it. Of course SharePoint offers more features but those are for some other day.

 So what do we exactly mean by “SharePoint Development”?  That means we want to write some code to extend the out-of-the-box functionality that comes with SharePoint. We’ll focus on adding  custom ASPX pages that help users accomplish a business function. In other words we’ll focus on integrating our custom business pages into SharePoint. Behind those pages we have business rules and processes. For example, a set of Order Entry pages which walk the user through a series of custom ASPX pages for gathering some data. In the example I choose to implement, I simulated the minimal functionality of a shopping cart, concentrating more on the code behind and the business processes.

 CSLA Development

 CSLA stands for Component-based, Scalable Logical Architecture and it is a software development framework that can help in building a maintainable business logic layer for Windows, Web, service-oriented and workflow applications. CSLA.NET enables you to create an object-oriented business layer that abstracts and encapsulates your business logic and data. The business objects supports binding for all major technologies like WPF, Web Forms, Windows Forms, Silverlight etc. Also by using CSLA.NET gives you the possibility to use easily the low coupling principle for the business, presentation and data access tiers. CSLA.NET is perfect to use when you need the same business tier for multiple presentation tiers, or when your code runs against multiple machines, you work in a distributed environment. But all these benefits are  time costing. The adoption of CSLA.NET is not easily made and requires some time. Becoming efficient with CSLA requires more time, time in which you can use alternatives which depends on the business process and environment you are going to work with. More about CSLA.NET can be found on the official site, here. The best resource to learn CSLA development is Rockford Lhotka’s (creator of CSLA.NET) book Expert C# 2008 Business Objects, also available in VB.NET version. Demo application presented in these book is available in CSLA.NET distribution.

Tools, technologies and environment preparations:

Before start proceeding with development be sure that SharePoint is up and running on your server and also Windows SharePoint Service works fine. You can do this by running your Central Administration Panel from SharePoint start menu item and access your installed sites.

Also be sure that you installed CSLA.NET Visual Studio templates. They come with the CSLA distribution and it helps in creating CSLA projects and entities.

  Developing ASP.NET applications using CSLA.NET

 In the next section I will focus on developing an ASP.Net application that uses CSLA.NET and that will be deployed on SharePoint. The application simulates the functionality of a shopping cart (adding products, ordering and submitting orders) for an online store. We’ll start up by building our database:

  a1

Figure 1 Tables diagram

 Product table contains data about the available products, size, price, name and description. A product can be ordered by an user and it will be associated to an order, represented by Order table. Products are associated to an order using the OrderDetail table. The shopping cart is represented by an Order with status “N”. In Order table we can not have more than one order having the status set to “N”, since we have only one shopping cart. When the products from shopping cart are submitted we’ll just change the status of the order to “P”.

I did not included here any reference to users and user roles because I just wanted to simplify the business model, in order to understand better the new paradigms provided by CSLA.NET, but the model can be easily extended at any time.

Having the database already done, we’ll continue by creating a new Web Application within a new solution using Visual Studio 2008. Let’s call the solution FirstCSLA  and the web application MyShop2. Also we’ll add two more class library MyShop.Lib  and MyShop.DalLinq which represents the business layer, respectively the data access layer. We’ll try to obtain a low coupling relationship between those layers. In the figure below you can see the solution and the projects we’ve created until now:

 a2

Figure 2 Created solution and projects

Data Access Layer(DAL)

At this time I’ve chosen to implement DAL  using Linq, by simply adding a new Linq To SQL Classes  item to MyShop.DalLinq project as in the figure below:

a3

Figure 3 DAL Linq implementation

 To generate MyShop.dbml  create a new database connection from Server Explorer and  just  drag and drop tables and stored procedures into the designer window.

Business Layer(BL)

This layer contains one of the more interesting parts and it uses the new CSLA.NET elements and paradigms. Until now we have done DAL and database and we know that we need a low coupling relationship between  BL, DAL and Presentation Layer. This problem will be resolved using CSLA.NET. This framework is created in a such way that will help developers and architects to split the architecture of the entire application  to accomplish their needs and requirements with minimum costs.  As an example let’s suppose that the BL  it’s so complex that we need to run it on multiple machines, in a distributed environment. But in the same time we can keep our DAL and Presentation Layer on the same or different machines. More about the applicability of CSLA.NET can be found in the first chapters of  Expert C# 2008 Business Objects book.

So, I started to create the classes that I needed in dealing with ordering process and shopping cart. First of all we need to retrieve the list of products. Without CSLA.NET probably we would have been using the MVC pattern and we would have been created a controller to retrieve this list. Or more easily using a SQLDataSource or LinQDataSource in the Presentation Layer. With CSLA we’ll create a read only list that will contain the business objects, in our case the products that exists in database. I choose to implement a read only list since I had no intention to implement the edit feature for the products. Also we can use a simple read only business object in this case. So, I’ve created ProductLIst  class that handles ProductInfo business objects, as you can see below.

a21

GetProductList() method returns all the existing products in database and watching the fetch method we can see how DAL is used to retrieve the products. Also we observe that  in class definition

“public class ProductList : ReadOnlyListBase<ProductList, ProductInfo>”

  we have specified the type of the business objects – ProductInfo, which contains the properties associated for a product.

a22

I continued by creating the Order class and the list that handles the order business ojects. This time we have to edit an Order because we have to implement the submission process that suppose an order update. For this reasons it is necessary to register  its properties, in order to let CSLA to maintain the object’s state:

a23

More details about property registration can be found in Expert C# 2008 Business Objects book.

In the end I implemented the classes necessary to hold the relation between an order and its products. I named the business class OrderProduct and the list class OrderProducts,  which in fact logically maps on the many to many relation between Product and Order tables in database.

The complete class diagram for the BL is shown below:

 a24

Figure 4 BL classes

 

Presentation Layer(PL)

I will concentrate on data bindings and how we use classes from BL to display and operate with data. First of all it’s important to mention that CSLA.NET provides a built-in data source object, CSLADataSource that extends the functionality of .NET built-in ObjectDataSource. More about CSLADataSource object can be found in the Expert C# 2008 Business Objects book.

But why CSLA provides this built-in data source object ? As you could have seen I have implemented some lists in BL that handles different business objects. Now using CSLADataSource is very easy to bind these lists to controls : DataGrids, DataViews, DataLists. I choose to use a DataGrid. For example the binding of the products to a grid view looks like:

 a4

Binding a list of business objects to a grid is as simple as making two drag and drops and setting up the type name and the assembly of the list for the data source. If we watch closer to the datasource definition we observe that  TypeName=”MyShop.Lib.ProductList, MyShop.Lib”  “tells” what type of objects will have to handle with. OnSelectObject  event binds the list with the business objects to the data source. Behind the design, the only thing we must assure is that during the  page load we call grid’s databind method and that we select the business objects datasource.

a5

 I used the CslaDataSource for the other bindings too. The rest of the code, with the entire solution is available for download here.

In the second part we’ll see how to customize and how to deploy the application we’ve just created.

1 Comment »

  1. Hi,

    This artical is very useful for me. I am a Share Point developer and always looking to learn something new. I would like to introduce another good SharePoint blog, Have a look.

    http://SharePointBank.com

    lazy

    Comment by lazy — January 7, 2010 @ 5:29 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a reply to lazy Cancel reply

Blog at WordPress.com.