由于项目需要,在门户页面显示WebPart来展示个性化信息和各类系统消息,开始的做法是为每一个WebPart做一个UserControl,然后在页面上显示,代码如下:
1 Dim mControl As Control = Page.LoadControl("../UserControl/WebPart1.ascx")
2 mControl.ID = "WebPart1"
3 Dim mGenericWebPart As GenericWebPart = Me.MyWebPartManager1.CreateWebPart(mControl)
4 mGenericWebPart.Title = "通知"
2 mControl.ID = "WebPart1"
3 Dim mGenericWebPart As GenericWebPart = Me.MyWebPartManager1.CreateWebPart(mControl)
4 mGenericWebPart.Title = "通知"
以上的链接信息和WebPart控件的ID也是写在数据库中的,但是每个页面要有自己的业务逻辑和数据访问逻辑。由于WebPart需要增强扩展性,所以决定将表示形式和数据访问的相关信息都放到数据库中做到可配置。
于是重新设计WebPart表的表结构,修改为如下几列:
WebPartId - WebPart控件的ID,如"001"
WebPartTitle- WebPart的标题
WebPartSQL - WebPart数据访问逻辑使用的SQL文
WebPartXslt - WebPart表示样式的XSLT转换内容
于是我们只需要一个业务逻辑(两个操作)和一个数据访问逻辑(两个操作),如下:
业务逻辑代码
1 Public Class DACWebPartUtility
2
3 Public Sub GetWebPartsInfo(ByRef htbWebParts As Hashtable)
4 Try
5 mDacWebPart.GetWebPartsInfo(mCommDacEntity)
6
7 htbWebParts = New Hashtable
8
9 For Each dr As DataRow In mCommDacEntity.DataSet.Tables(0).Rows
10 htbWebParts.Add(dr.Item("WebPartId").ToString, dr.Item("WebPartTitle").ToString)
11 Next
12
13 Transaction.Commit()
14 Catch ex As Exception
15 Transaction.Rollback()
16 End Try
17 End Sub
18
19 Public Sub GetWebPartInfo(ByVal strWebPartTitle As String, ByVal strStaffId As String, ByRef mDataSet As DataSet, ByRef strWebPartId As String, ByRef strXslt As String)
20
21 Try
22 mDacWebPart.GetWebPartInfo(mCommDacEntity, strWebPartTitle, strStaffId, strWebPartId, strXslt)
23 If mCommDacEntity Is Nothing Then
24 mDataSet = Nothing
25 Else
26 mDataSet = mCommDacEntity.DataSet
27 End If
28
29 Transaction.Commit()
30 Catch ex As Exception
31 Transaction.Rollback()
32 End Try
33 End Sub
34
35 End Class
2
3 Public Sub GetWebPartsInfo(ByRef htbWebParts As Hashtable)
4 Try
5 mDacWebPart.GetWebPartsInfo(mCommDacEntity)
6
7 htbWebParts = New Hashtable
8
9 For Each dr As DataRow In mCommDacEntity.DataSet.Tables(0).Rows
10 htbWebParts.Add(dr.Item("WebPartId").ToString, dr.Item("WebPartTitle").ToString)
11 Next
12
13 Transaction.Commit()
14 Catch ex As Exception
15 Transaction.Rollback()
16 End Try
17 End Sub
18
19 Public Sub GetWebPartInfo(ByVal strWebPartTitle As String, ByVal strStaffId As String, ByRef mDataSet As DataSet, ByRef strWebPartId As String, ByRef strXslt As String)
20
21 Try
22 mDacWebPart.GetWebPartInfo(mCommDacEntity, strWebPartTitle, strStaffId, strWebPartId, strXslt)
23 If mCommDacEntity Is Nothing Then
24 mDataSet = Nothing
25 Else
26 mDataSet = mCommDacEntity.DataSet
27 End If
28
29 Transaction.Commit()
30 Catch ex As Exception
31 Transaction.Rollback()
32 End Try
33 End Sub
34
35 End Class
数据访问逻辑
1 Public Class DACWebPart
2 Public Sub GetWebPartsInfo(ByRef dec As CommDacEntity)
3
4 Dim strSysDate As String = DacUtility.GetSysDate
5
6 If dec Is Nothing Then
7 Return
8 End If
9
10 Dim strBuilderSql As New StringBuilder
11
12 MyBase.MethodName = "GetWebPartsInfo"
13
14 strBuilderSql.Append("SELECT")
15 strBuilderSql.Append(" WebPartID,")
16 strBuilderSql.Append(" WebPartTitle")
17 strBuilderSql.Append(" FROM TblWebPart")
18
19 MyBase.ExecSql(dec, strBuilderSql.ToString, CommandType.Text)
20 dec.DataSet.Tables(0).TableName = "TblWebPart"
21
22 End Sub
23
24 Public Sub GetWebPartInfo(ByRef dec As CommDacEntity, ByVal strWebPartTitle As String, ByVal strStaffId As String, ByRef strWebPartId As String, ByRef strXslt As String)
25
26 Dim strSysDate As String = DacUtility.GetSysDate
27
28 Dim strBuilderSql As New StringBuilder
29
30 MyBase.MethodName = "GetWebPartInfo"
31
32 strBuilderSql.Append("SELECT")
33 strBuilderSql.Append(" WebPartId,")
34 strBuilderSql.Append(" WebPartSQL,")
35 strBuilderSql.Append(" WebPartXSLT")
36 strBuilderSql.Append(" FROM TblWebPart WHERE WebPartTitle='")
37 strBuilderSql.Append(ParaConvert(strWebPartTitle))
38 strBuilderSql.Append("'")
39
40 MyBase.ExecDataSet(dec, strBuilderSql.ToString, CommandType.Text)
41 dec.DataSet.Tables(0).TableName = "TblWebPart"
42
43 strXslt = dec.DataSet.Tables(0).Rows(0).Item("WebPartXSLT").ToString
44 strStaffId = dec.DataSet.Tables(0).Rows(0).Item("WebPartId").ToString
45
46 Dim decTemp As New CommDacEntity
47
48 Dim strSql As String = dec.DataSet.Tables(0).Rows(0).Item("WebPartSQL").ToString
49
50 If String.IsNullOrEmpty(strSql.TrimEnd) Then
51 dec = Nothing
52 Else
53 strSql = strSql.Replace("{StaffId}", ParaConvert(strStaffId))
54 strSql = strSql.Replace("{SysDate}", strSysDate)
55 MyBase.ExecDataSet(decTemp, strSql, CommandType.Text)
56 dec = decTemp
57 End If
58
59 End Sub
60 End Class
2 Public Sub GetWebPartsInfo(ByRef dec As CommDacEntity)
3
4 Dim strSysDate As String = DacUtility.GetSysDate
5
6 If dec Is Nothing Then
7 Return
8 End If
9
10 Dim strBuilderSql As New StringBuilder
11
12 MyBase.MethodName = "GetWebPartsInfo"
13
14 strBuilderSql.Append("SELECT")
15 strBuilderSql.Append(" WebPartID,")
16 strBuilderSql.Append(" WebPartTitle")
17 strBuilderSql.Append(" FROM TblWebPart")
18
19 MyBase.ExecSql(dec, strBuilderSql.ToString, CommandType.Text)
20 dec.DataSet.Tables(0).TableName = "TblWebPart"
21
22 End Sub
23
24 Public Sub GetWebPartInfo(ByRef dec As CommDacEntity, ByVal strWebPartTitle As String, ByVal strStaffId As String, ByRef strWebPartId As String, ByRef strXslt As String)
25
26 Dim strSysDate As String = DacUtility.GetSysDate
27
28 Dim strBuilderSql As New StringBuilder
29
30 MyBase.MethodName = "GetWebPartInfo"
31
32 strBuilderSql.Append("SELECT")
33 strBuilderSql.Append(" WebPartId,")
34 strBuilderSql.Append(" WebPartSQL,")
35 strBuilderSql.Append(" WebPartXSLT")
36 strBuilderSql.Append(" FROM TblWebPart WHERE WebPartTitle='")
37 strBuilderSql.Append(ParaConvert(strWebPartTitle))
38 strBuilderSql.Append("'")
39
40 MyBase.ExecDataSet(dec, strBuilderSql.ToString, CommandType.Text)
41 dec.DataSet.Tables(0).TableName = "TblWebPart"
42
43 strXslt = dec.DataSet.Tables(0).Rows(0).Item("WebPartXSLT").ToString
44 strStaffId = dec.DataSet.Tables(0).Rows(0).Item("WebPartId").ToString
45
46 Dim decTemp As New CommDacEntity
47
48 Dim strSql As String = dec.DataSet.Tables(0).Rows(0).Item("WebPartSQL").ToString
49
50 If String.IsNullOrEmpty(strSql.TrimEnd) Then
51 dec = Nothing
52 Else
53 strSql = strSql.Replace("{StaffId}", ParaConvert(strStaffId))
54 strSql = strSql.Replace("{SysDate}", strSysDate)
55 MyBase.ExecDataSet(decTemp, strSql, CommandType.Text)
56 dec = decTemp
57 End If
58
59 End Sub
60 End Class
在用户接口(UI)层,我们主要有一个门户页面(Homepage.aspx)和一个用户控件(WebPartControl.ascx)。第一个操作GetWebPartInfo,是用来取得WebPart的ID和Title,然后都将控件指向WebPartControl.ascx。第二个操作是用来给WebPartControl使用,在数据层取得WebPartSQL的内容,然后直接在数据访问层执行该SQL文,将执行后取得的DataSet和Xslt内容返回给WebPartControl.ascx,然后在页面上表示。
稍后,我会再写一篇随笔来解释为什么仍然使用用户控件,而不是LiteralControl生成控件;还有这样开发的时候遇到的问题和解决方案。
RSS订阅






收 藏
推 荐