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

November 13, 2009

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

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

In the first part we have seen which are the requirements for developing an ASP.NET application for SharePoint 2007 and we build a demo application using CSLA.NET. We’ll continue by customizing and deploying the application we’ve created.

Customize ASP.NET application for SharePoint 2007

Before we start it is good to know what is our purpose, what we have until now and what we are going to do :

  • We want to deploy our ASP.NET application created earlier using CSLA.NET on SharePoint 2007
  • We’ll have to use Visual Studio extenstions for Windows SharePoint Services in order to create an Empty project that will help with automatic packaging and deployment to SharePoint Server
  • Link the pages from the Web Application we already have with the newly created SharePoint project
  • Do some configurations on the projects and config files to work with SharePoint

 A good resource about developing custom ASP.NET pages with SharePoint 2007 is Jamil Haddadin’s blog.

We’ll start by creating a new Empty project that I named CSLADemo:

 a6

Figure 5 Creating VSeWSS project

 In this moment the solution contains applications projects and the empty VSeWSS project:

 a6

Figure 6 Solution explorer before customization

The next step requires adding a new web form to the web application MyShop2. I called this web form MyDemo.aspx. It will implement demo functionalities plus some special configurations specific to SharePoint.

 a7

Figure 7 Adding a new web form

 Having created our demo web form a new Module  project item must be added to the CSLADemo project. Also the recent created MyDemo.aspx web will be added to this module. In order to avoid any duplications we’ll add this using Add as link feature. This module is specific to SharePoint and it represents the element through which our custom web forms will be deployed to the server. Be sure that you modify the namespace in MyDemo.aspx.cs, MyDemo.aspx.Designer.cs and MyDemo.aspx to CSLADemo.

 a8

Figure 8 Creating a new module

 a9

Figure 9 Add MyDemo.aspx as link

Now our solution looks like:

 a10

Figure 10 Solution state

 As you it can be observed I  used a key to sign dlls we’ve created earlier: libkey.snk, dallibkey.snk. If not, SharePoint will throw a runtime error. Csla.dll is already signed.  Also these dlls must be added at CSLADemo project, as link, if not they will not be copied to the deployment directory targets.

In  Module.xml a File element has to be included. Update the Path and Url attributes to the file name of the .aspx file. This deploys the page when the SharePoint solution is deployed, in my simple case; the Module.xml will look like:

Path attribute refers to the reference path of the file in visual studio project and URL refers to the URL of the page in the SharePoint site, in this case our page will be deployed here http://ServerName:port/MyDemo.aspx

  a11

Figure 11 Module.xml editing

 In the Configuration Manager of the solution, clear the Build for theCSLADemo check box, see the picture below..

 a12

Figure 12 Build configuration

Add Safe control  entry for each deployed dll in manifest.xml. The file can be found in solution’s WSPview .

 a13

Figure 13 Adding safe controls

 To deploy the solution change the Start browser with URL in the CSLADemo properties page in the debug pane with your SharePoint root site, in my case http://vpc2003/

 a14

Figure 14 Project starting settings

 Before deploying the application and testing it we must comment the SqlDataSource tagMapping in web.config  of the SharePoint application, the web.config associated to http://vpc2003/. It can be found in the virtual directory root.

 a15

Figure 15 Comment data source tag mapping

More about customizing asp.net pages can be found on Jamil Haddadin’ blog.

 Hoping that no errors occur, deploy the solution and test it calling http://vpc2003/MyDemo.aspx

 a16

Figure 16 Running application(1)

 a17

Figure 17 Running application(2)

 The solution is available for download here: http://bitstarsolutions.com/FirstCSLA.rar

Resources:

Advertisements

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.

Blog at WordPress.com.