2005/11/03 | ASP三层架构简单实例
类别(Develop) | 评论(0) | 阅读(871) | 发表于 09:31
1.数据模型层:
/class/Data/News.asp
<%
Class DataNews

    Private INewsID
    Private IClassID
    Private IClassName
    Private ITitle
    Private IContent
    Private IPicture
    Private IIsCommend
    Private IHits
    Private IPostDate

    'NewsID
    Public Property Let NewsID(ByVal value)
        INewsID = value
    End Property
    Public Property Get NewsID()
        NewsID = INewsID
    End Property

    'ClassID
    Public Property Let ClassID(ByVal value)
        IClassID = value
    End Property
    Public Property Get ClassID()
        ClassID = IClassID
    End Property

    'ClassName
    Public Property Let ClassName(ByVal value)
        IClassName = value
    End Property
    Public Property Get ClassName()
        ClassName = IClassName
    End Property

    'Title
    Public Property Let Title(ByVal value)
        ITitle = value
    End Property
    Public Property Get Title()
        Title = ITitle
    End Property

    'Content
    Public Property Let Content(ByVal value)
        IContent = value
    End Property
    Public Property Get Content()
        Content = IContent
    End Property

    'Picture
    Public Property Let Picture(ByVal value)
        IPicture = value
    End Property
    Public Property Get Picture()
        Picture = IPicture
    End Property

    'IsCommend
    Public Property Let IsCommend(ByVal value)
        IIsCommend = value
    End Property
    Public Property Get IsCommend()
        IsCommend = IIsCommend
    End Property

    'Hits
    Public Property Let Hits(ByVal value)
        IHits = value
    End Property
    Public Property Get Hits()
        Hits = IHits
    End Property

    'PostDate
    Public Property Let PostDate(ByVal value)
        IPostDate = value
    End Property
    Public Property Get PostDate()
        PostDate = IPostDate
    End Property

    Private Sub Class_Initialize()
    End Sub
    Private Sub Class_Terminate()
    End Sub

End Class
%>

2.数据访问层:
/Class/Dal/News.asp

<!--#include virtual="/Class/Data/News.asp"-->
<%
Class DalNews

    Private db
    Private news

    'NewsID
    Public Property Let NewsID(ByVal value)
        news.NewsID = value
    End Property
    Public Property Get NewsID()
        NewsID = news.NewsID
    End Property

    'ClassID
    Public Property Let ClassID(ByVal value)
        news.ClassID = value
    End Property
    Public Property Get ClassID()
        ClassID = news.ClassID
    End Property

    'ClassName
    Public Property Let ClassName(ByVal value)
        news.ClassName = value
    End Property
    Public Property Get ClassName()
        ClassName = news.ClassName
    End Property

    'Title
    Public Property Let Title(ByVal value)
        news.Title = value
    End Property
    Public Property Get Title()
        Title = news.Title
    End Property

    'Content
    Public Property Let Content(ByVal value)
        news.Content = value
    End Property
    Public Property Get Content()
        Content = news.Content
    End Property

    'Picture
    Public Property Let Picture(ByVal value)
        news.Picture = value
    End Property
    Public Property Get Picture()
        Picture = news.Picture
    End Property

    'IsCommend
    Public Property Let IsCommend(ByVal value)
        news.IsCommend = value
    End Property
    Public Property Get IsCommend()
        IsCommend = news.IsCommend
    End Property

    'Hits
    Public Property Let Hits(ByVal value)
        news.Hits = value
    End Property
    Public Property Get Hits()
        Hits = news.Hits
    End Property

    'PostDate
    Public Property Let PostDate(ByVal value)
        news.PostDate = value
    End Property
    Public Property Get PostDate()
        PostDate = news.PostDate
    End Property

    Public Function SelectOne()
        Dim rs : Set rs = db.ExecuteSp("News_SelectOne", NewsID)
        If Not (rs.BOF OR rs.EOF) Then
         With Me
         .NewsID = rs("NewsID")
         .ClassID = rs("ClassID")
         .ClassName = rs("ClassName")
         .Title = rs("Title")
         Dim tmpContent : tmpContent = rs("Content")
.Content = tmpContent
         .Picture = rs("Picture")
         .IsCommend = rs("IsCommend")
         .Hits = rs("Hits")
         .PostDate = rs("PostDate")
         End With
         SelectOne = True
        Else
         SelectOne = False
        End If
    End Function

    Public Function SelectTop(ByVal iCount, ByVal iClassID)
        Set SelectTop = db.Execute("SELECT TOP " & iCount & " NewsID, News.ClassID, Class.ClassName, Title, Content, Picture, IsCommend, Hits, PostDate FROM [News] LEFT JOIN [Class] ON [Class].[ClassID] = [News].[ClassID] WHERE News.[ClassID] = " & iClassID & " ORDER BY NewsID DESC")
    End Function

    Public Function SelectAll()
        Set SelectAll = db.ExecuteDataTableSp("News_SelectAll", Null)
    End Function

    Public Function SelectAllByClassID()
        Set SelectAllByClassID = db.ExecuteDataTableSp("News_SelectAllByClassID", ClassID)
    End Function

    Public Function Insert()
        NewsID = db.InsertSp("News_Insert", Array(ClassID, Title, Content, Picture, IsCommend, Hits))
        Insert = NewsID
    End Function

    Public Function Update()
        Update = db.ExecuteNonQuerySp("News_Update", Array(ClassID, Title, Content, Picture, IsCommend, Request.Form("Hits"), NewsID)) > 0
    End Function

    Public Function Delete()
        Delete = db.ExecuteNonQuerySp("News_Delete", NewsID) > 0
    End Function

    Public Function BatchDelete(ByVal NewsIDs)
        BatchDelete = db.ExecuteNonQuery("DELETE * FROM [News] WHERE NewsID IN (" & NewsIDs & ")")
    End Function

    Public Function TopPicNews(ByVal iClassID)
        Set TopPicNews = db.ExecuteDataTableSp("News_SelectTopPic", iClassID)
    End Function

    Private Sub Class_Initialize()
        Set db = New Oledb
        Set news = New DataNews
    End Sub
    Private Sub Class_Terminate()
        Set db = Nothing
        Set news = Nothing
    End Sub

End Class
%>

3.业务逻辑层:
/Class/Bll/News.asp

<!--#include virtual="/Class/Dal/News.asp"-->
<%
Class BllNews

    Private v
    Private e
    Private news

    'NewsID
    Public Property Let NewsID(ByVal value)
        If Not IsEmpty(value) And v.IsNum(v.SafeNo(value)) Then
            news.NewsID = CInt(v.SafeNo(value))
        Else
            news.NewsID = 0
            e.Message = "NewsID参数错误"
        End If
    End Property
    Public Property Get NewsID()
        NewsID = news.NewsID
    End Property

    'ClassID
    Public Property Let ClassID(ByVal value)
        If Not IsEmpty(value) And v.IsNum(v.SafeNo(value)) Then
            news.ClassID = CInt(v.SafeNo(value))
        Else
            news.ClassID = 0
            e.Message = "ClassID参数错误"
        End If
    End Property
    Public Property Get ClassID()
        ClassID = news.ClassID
    End Property

    'ClassName
    Public Property Let ClassName(ByVal value)
        news.ClassName = value
    End Property
    Public Property Get ClassName()
        ClassName = news.ClassName
    End Property

    'Title
    Public Property Let Title(ByVal value)
        If v.Limit(value, 1, 30) Then
            news.Title = value
        Else
            If IsNull(value) or IsEmpty(value) Or value = "" Then
                news.Title = ""
                e.Message = "标题不允许为空"
            Else
                news.Title = Left(value, 30)
                e.Message = "标题字符长度超过30"
            End If
        End If
    End Property
    Public Property Get Title()
        Title = news.Title
    End Property

    'Content
    Public Property Let Content(ByVal value)
        If IsNull(value) or IsEmpty(value) Or value = "" Then
            news.Content = ""
            e.Message = "内容不允许为空"
        Else
            news.Content = value
        End If
    End Property
    Public Property Get Content()
        Content = news.Content
    End Property

    'Picture
    Public Property Let Picture(ByVal value)
        news.Picture = value
    End Property
    Public Property Get Picture()
        Picture = news.Picture
    End Property

    'IsCommend
    Public Property Let IsCommend(ByVal value)
        news.IsCommend = CBool(value)
    End Property
    Public Property Get IsCommend()
        IsCommend = news.IsCommend
    End Property

    'Hits
    Public Property Let Hits(ByVal value)
        If Not IsEmpty(value) And v.IsNum(v.SafeNo(value)) Then
            news.Hits = CInt(v.SafeNo(value))
        Else
            news.Hits = 0
            e.Message = "点击数设置错误"
        End If
    End Property
    Public Property Get Hits()
        Hits = news.Hits
        If IsEmpty(news.Hits) Then Hits = 0
    End Property

    'PostDate
    Public Property Let PostDate(ByVal value)
        news.PostDate = value
    End Property
    Public Property Get PostDate()
        PostDate = FormatDateTime(CDate(news.PostDate), 1)
    End Property

    Public Sub Throw()
     e.Throw()
    End Sub

    Public Function SelectOne()
        NewsID = NewsID
        If Not IsEmpty(NewsID) Then
            SelectOne = news.SelectOne()
            If SelectOne = False Then
                e.Message = "参数错误,该信息不存在或已被删除"
            End If
        End If
        e.Throw()
    End Function

    Public Function SelectTop(ByVal iCount, ByVal iClassID)
        Set SelectTop = news.SelectTop(iCount, iClassID)
    End Function

    Public Function SelectAll()
        Set SelectAll = news.SelectAll()
    End Function

    Public Function SelectAllByClassID()
        Set SelectAllByClassID = news.SelectAllByClassID()
    End Function

    Public Sub Insert()
        ClassID = ClassID
        Title = Title
        Content = Content
        Picture = Picture
        IsCommend = IsCommend
        Hits = Hits
        e.Target = "/admin/NewsForm.asp"
        e.Throw()
        news.Insert()
        If Me.NewsID > 0 Then
            e.Message = "信息添加成功,正在转到列表"
            e.Target = "/admin/NewsList.asp"
        Else
            e.Message = "信息添加失败,请检查输入"
        End If
        e.Throw()
    End Sub

    Public Sub Update()
        e.Target = "/admin/NewsList.asp"
        If news.Update() Then
            e.Message = "信息更新成功,正在返回..."
        Else
            e.Message = "信息更新失败,请确认参数是否正确或信息是否存在"
        End If
        e.Throw()
    End Sub

    Public Sub UpdateCount()
        If Cookie("News" & Me.NewsID) = "" Then
            news.UpdateCount()
            Call Cookie.Add("News" & Me.NewsID, 1, 1)
        End If
    End Sub

    Public Sub Delete()
        e.Target = "/admin/NewsList.asp"
        If news.Delete() Then
            e.Message = "信息删除成功,正在返回..."
        Else
            e.Message = "信息删除失败,请确认参数是否正确或信息是否存在"
        End If
        e.Throw()
    End Sub

    Public Sub BatchDelete(ByVal NewsIDs)
        e.Target = "/admin/NewsList.asp"
        Dim Rows : Rows = news.BatchDelete(NewsIDs)
        If Rows > 0 Then
            e.Message = "成功删除信息<font color='red'> " & Rows & " </font>条,正在返回..."
        Else
            e.Message = "信息删除失败,请确认参数是否正确或信息是否存在" & NewsIDs
        End If
        e.Throw()
    End Sub

    Public Sub ShowTopNews(ByVal iCount, ByVal iClassID)
        Dim Rs : Set Rs = SelectTop(iCount, iClassID)
        If Not (Rs.BOF Or Rs.EOF) Then
%>
<TABLE cellSpacing=0 cellPadding=0 width=330 border=0>
<TBODY>
<%
            While Not Rs.EOF
%>
<TR>
<TD height=20><IMG style="MARGIN-RIGHT: 6px"
height=3 src="images/mt02_i01.gif" width=4
align=absMiddle><A
href="Detail.asp?ID=<%=Rs("NewsID")%>" target="_blank"><%=Rs("Title")%></A><FONT
color=#008f7e>/<%=FormatDate(Rs("PostDate"))%></FONT></TD>
</TR>
<%
                Rs.MoveNext()
            Wend
            Set Rs = Nothing
%>
</TBODY>
</TABLE>
<%
        End If
    End Sub


    Public Sub ShowTopPicNews(ByVal iCount, ByVal iClassID)
        Dim Rs : Set Rs = SelectTop(iCount, iClassID)
        Call ListRow(2, 2, Rs)        
    End Sub

    Public Function TopPicNews(ByVal iClassID)
        Set TopPicNews = news.TopPicNews(iClassID)
    End Function

    Private Sub Class_Initialize()
        Set v = New Validator
        Set e = New Exception
        Set news = New DalNews
    End Sub
    Private Sub Class_Terminate()
        Set v = Nothing
        Set e = Nothing
        Set news = Nothing
    End Sub

    Private Function FormatDate(ByVal daPostDate)
        daPostDate = CDate(daPostDate)
        FormatDate = Right(Year(daPostDate), 2) & "." & Month(daPostDate) & "." & Day(daPostDate)
    End Function

    Public Function ListRow(ByVal x,ByVal y,ByVal Rs)
     With Response
     If (Rs.Eof or Rs.Bof) Then Exit Function
     If x <=0 Then Exit Function End If
     If y<=0 Then y = 0 End If
     For i = 0 to x
        .Write "<tr>" & vbCrlf
        For j = 0 to y
            If Not Rs.Eof Then
                .Write "<TD width=""110"" height=100 align=""center""><img src=""" & rs("Picture") & """ width=""100"" height=""75""><br><a href=""Detail.asp?id=" & rs("NewsID") & """>" & rs("Title") & "</a></TD>" & vbCrlf
            Else
                .Write "<td width=""110"" height=""100"">&nbsp;</td>" & vbCrlf
            End If
            Rs.MoveNext()
            If (Rs.Eof or Rs.Bof) Then
                If j < y Then
                    .Write Replace(Space(y - j), " ", "<td>&nbsp;</td>" & vbCrlf)
                End If
                .Write "</tr>" & vbCrlf
                Exit Function
            End If
        Next
        .Write "</tr>" & vbCrlf
        Next
     End With
    End Function

End Class
%>
0

评论Comments

日志分类
首页[66]
Develop[14]
Plus[3]
Melody[7]
Essay[38]
Archive[4]