FastSpring学习笔记三(创建新闻发布信息的相关类)
熟悉了“FastSpring学习笔记二” 的介绍后,我们来实践一下吧,我们先在SQL Server中创建一个表,代码如下:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FS_PT_Announcement]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[FS_PT_Announcement]
GO
CREATE TABLE [dbo].[FS_PT_Announcement] (
[ItemID] [int] IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL ,
[ModuleID] [int] NOT NULL ,
[CreateByUser] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[CreatedDate] [datetime] NOT NULL ,
[Title] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[MoreLink] [nvarchar] (150) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[ExpireDate] [datetime] NOT NULL ,
[Description] [text] COLLATE Chinese_PRC_CI_AS NOT NULL ,
[Frequency] [int] NOT NULL ,
[IsActive] [int] NOT NULL ,
[Priority] [int] NOT NULL ,
[ClassID] [int] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
drop table [dbo].[FS_PT_Announcement]
GO
CREATE TABLE [dbo].[FS_PT_Announcement] (
[ItemID] [int] IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL ,
[ModuleID] [int] NOT NULL ,
[CreateByUser] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[CreatedDate] [datetime] NOT NULL ,
[Title] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[MoreLink] [nvarchar] (150) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[ExpireDate] [datetime] NOT NULL ,
[Description] [text] COLLATE Chinese_PRC_CI_AS NOT NULL ,
[Frequency] [int] NOT NULL ,
[IsActive] [int] NOT NULL ,
[Priority] [int] NOT NULL ,
[ClassID] [int] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
PT_Announcement:公告信息表(在公告信息模块中用) | |||
字段名 | 类型 | 含义 | 备注 |
ItemID | Int | 链接Id | 主键(自动编号) |
ModuleID | Int | 所属模块ID | 决定该公告在那个模块中显示(模块ID:用户配置文件PortalCfg.xml中的模块表ID,以下皆同) |
CreatedByUser | Nvarchar(100) | 创建者 | 记录创建和修改该公告的用户 |
CreatedDate | Datetime | 创建时间 | 记录创建和修改该公告的时间 |
Title | Nvarchar(100) | 名称 | |
MoreLink | Nvarchar(150) | 更多细节的链接 | |
MobileMoreLink | Nvarchar(150) | 移动细节链接地址 | 在“移动设备浏览器”上的更多细节链接地址 |
ExpireDate | Datetime | 有效日期 | |
Description | Nvarchar(2000) | 描述 | |
Frequency | Int | 点击率 | |
IsActive | Int | 是否激活 | 0为不显示,否则显示 |
Priority | Int | 优先权 | 0为最低优先级,越大优先级越高 |
ClassID | Int | 分类标识 |
一、我们用上一章的方法,生成了2个文件:Announcement.cs和Announcement.hbm.xml
二、我们在DZ_Portal.App项目中添加几个文件夹,分别是:“BLL”、“DAL”、“Model”,它们分别代表不同的层,分别是:业务逻辑层、获取业务实体层、数据实体层(业务实体)。
三、修改Announcement.cs文件,最后结果如下:
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using FastSpring.Base.Model;
using FastSpring.Core.Model;
using FastSpring.Common;
namespace DZ_Portal.App.Model
...{
/**//// <summary>
///
/// </summary>
[Serializable]
public sealed class Announcement: BaseVersionModel
...{
私有成员#region 私有成员
private bool m_IsChanged;
private bool m_IsDeleted;
private int m_ItemID;
private int m_ModuleID;
private string m_CreateByUser;
private DateTime m_CreatedDate;
private string m_Title;
private string m_MoreLink;
private DateTime m_ExpireDate;
private string m_Description;
private int m_Frequency;
private int m_IsActive;
private int m_Priority;
private int m_ClassID;
#endregion
GetEntityType()#region GetEntityType()
public override Type GetEntityType()
...{
return typeof(Announcement);
}
#endregion
默认( 空 ) 构造函数#region 默认( 空 ) 构造函数
/**//// <summary>
/// 默认构造函数
/// </summary>
public Announcement()
...{
m_ItemID = 0;
m_ModuleID = 0;
m_CreateByUser = null;
m_CreatedDate = DateTime.MinValue;
m_Title = null;
m_MoreLink = null;
m_ExpireDate = DateTime.MinValue;
m_Description = null;
m_Frequency = 0;
m_IsActive = 0;
m_Priority = 0;
m_ClassID = 0;
}
#endregion
公有属性#region 公有属性
/**//// <summary>
///
/// </summary>
public int ItemID
...{
get ...{ return m_ItemID; }
set ...{ m_IsChanged |= (m_ItemID != value); m_ItemID = value; }
}
/**//// <summary>
///
/// </summary>
public int ModuleID
...{
get ...{ return m_ModuleID; }
set ...{ m_IsChanged |= (m_ModuleID != value); m_ModuleID = value; }
}
/**//// <summary>
///
/// </summary>
public string CreateByUser
...{
get ...{ return m_CreateByUser; }
set
...{
if (value != null)
if (value.Length > 50)
throw new ArgumentOutOfRangeException("Invalid value for CreateByUser", value, value.ToString());
m_IsChanged |= (m_CreateByUser != value); m_CreateByUser = value;
}
}
/**//// <summary>
///
/// </summary>
public DateTime CreatedDate
...{
get ...{ return m_CreatedDate; }
set ...{ m_IsChanged |= (m_CreatedDate != value); m_CreatedDate = value; }
}
/**//// <summary>
///
/// </summary>
public string Title
...{
get ...{ return m_Title; }
set
...{
if (value != null)
if (value.Length > 100)
throw new ArgumentOutOfRangeException("Invalid value for Title", value, value.ToString());
m_IsChanged |= (m_Title != value); m_Title = value;
}
}
/**//// <summary>
///
/// </summary>
public string MoreLink
...{
get ...{ return m_MoreLink; }
set
...{
if (value != null)
if (value.Length > 150)
throw new ArgumentOutOfRangeException("Invalid value for MoreLink", value, value.ToString());
m_IsChanged |= (m_MoreLink != value); m_MoreLink = value;
}
}
/**//// <summary>
///
/// </summary>
public DateTime ExpireDate
...{
get ...{ return m_ExpireDate; }
set ...{ m_IsChanged |= (m_ExpireDate != value); m_ExpireDate = value; }
}
/**//// <summary>
///
/// </summary>
public string Description
...{
get ...{ return m_Description; }
set
...{
if (value != null)
if (value.Length > 2147483647)
throw new ArgumentOutOfRangeException("Invalid value for Description", value, value.ToString());
m_IsChanged |= (m_Description != value); m_Description = value;
}
}
/**//// <summary>
///
/// </summary>
public int Frequency
...{
get ...{ return m_Frequency; }
set ...{ m_IsChanged |= (m_Frequency != value); m_Frequency = value; }
}
/**//// <summary>
///
/// </summary>
public int IsActive
...{
get ...{ return m_IsActive; }
set ...{ m_IsChanged |= (m_IsActive != value); m_IsActive = value; }
}
/**//// <summary>
///
/// </summary>
public int Priority
...{
get ...{ return m_Priority; }
set ...{ m_IsChanged |= (m_Priority != value); m_Priority = value; }
}
/**//// <summary>
///
/// </summary>
public int ClassID
...{
get ...{ return m_ClassID; }
set ...{ m_IsChanged |= (m_ClassID != value); m_ClassID = value; }
}
/**//// <summary>
/// 对象的值是否被改变
/// </summary>
public bool IsChanged
...{
get ...{ return m_IsChanged; }
}
/**//// <summary>
/// 对象是否已经被删除
/// </summary>
public bool IsDeleted
...{
get ...{ return m_IsDeleted; }
}
#endregion
公有函数#region 公有函数
/**//// <summary>
/// 标记对象已删除
/// </summary>
public void MarkAsDeleted()
...{
m_IsDeleted = true;
m_IsChanged = true;
}
#endregion
重写Equals和HashCode#region 重写Equals和HashCode
/**//// <summary>
/// 用唯一值实现Equals
/// </summary>
public override bool Equals(object obj)
...{
if (this == obj) return true;
if ((obj == null) || (obj.GetType() != GetType())) return false;
Announcement castObj = (Announcement)obj;
return (castObj != null) &&
(m_ItemID == castObj.ItemID);
}
/**//// <summary>
/// 用唯一值实现GetHashCode
/// </summary>
public override int GetHashCode()
...{
int hash = 57;
hash = 27 * hash * m_ItemID.GetHashCode();
return hash;
}
#endregion
public override bool EqualsID(int ID)
...{
return this.ItemID == ID;
}
}
}
using System.Data;
using System.Collections.Generic;
using System.Text;
using FastSpring.Base.Model;
using FastSpring.Core.Model;
using FastSpring.Common;
namespace DZ_Portal.App.Model
...{
/**//// <summary>
///
/// </summary>
[Serializable]
public sealed class Announcement: BaseVersionModel
...{
私有成员#region 私有成员
private bool m_IsChanged;
private bool m_IsDeleted;
private int m_ItemID;
private int m_ModuleID;
private string m_CreateByUser;
private DateTime m_CreatedDate;
private string m_Title;
private string m_MoreLink;
private DateTime m_ExpireDate;
private string m_Description;
private int m_Frequency;
private int m_IsActive;
private int m_Priority;
private int m_ClassID;
#endregion
GetEntityType()#region GetEntityType()
public override Type GetEntityType()
...{
return typeof(Announcement);
}
#endregion
默认( 空 ) 构造函数#region 默认( 空 ) 构造函数
/**//// <summary>
/// 默认构造函数
/// </summary>
public Announcement()
...{
m_ItemID = 0;
m_ModuleID = 0;
m_CreateByUser = null;
m_CreatedDate = DateTime.MinValue;
m_Title = null;
m_MoreLink = null;
m_ExpireDate = DateTime.MinValue;
m_Description = null;
m_Frequency = 0;
m_IsActive = 0;
m_Priority = 0;
m_ClassID = 0;
}
#endregion
公有属性#region 公有属性
/**//// <summary>
///
/// </summary>
public int ItemID
...{
get ...{ return m_ItemID; }
set ...{ m_IsChanged |= (m_ItemID != value); m_ItemID = value; }
}
/**//// <summary>
///
/// </summary>
public int ModuleID
...{
get ...{ return m_ModuleID; }
set ...{ m_IsChanged |= (m_ModuleID != value); m_ModuleID = value; }
}
/**//// <summary>
///
/// </summary>
public string CreateByUser
...{
get ...{ return m_CreateByUser; }
set
...{
if (value != null)
if (value.Length > 50)
throw new ArgumentOutOfRangeException("Invalid value for CreateByUser", value, value.ToString());
m_IsChanged |= (m_CreateByUser != value); m_CreateByUser = value;
}
}
/**//// <summary>
///
/// </summary>
public DateTime CreatedDate
...{
get ...{ return m_CreatedDate; }
set ...{ m_IsChanged |= (m_CreatedDate != value); m_CreatedDate = value; }
}
/**//// <summary>
///
/// </summary>
public string Title
...{
get ...{ return m_Title; }
set
...{
if (value != null)
if (value.Length > 100)
throw new ArgumentOutOfRangeException("Invalid value for Title", value, value.ToString());
m_IsChanged |= (m_Title != value); m_Title = value;
}
}
/**//// <summary>
///
/// </summary>
public string MoreLink
...{
get ...{ return m_MoreLink; }
set
...{
if (value != null)
if (value.Length > 150)
throw new ArgumentOutOfRangeException("Invalid value for MoreLink", value, value.ToString());
m_IsChanged |= (m_MoreLink != value); m_MoreLink = value;
}
}
/**//// <summary>
///
/// </summary>
public DateTime ExpireDate
...{
get ...{ return m_ExpireDate; }
set ...{ m_IsChanged |= (m_ExpireDate != value); m_ExpireDate = value; }
}
/**//// <summary>
///
/// </summary>
public string Description
...{
get ...{ return m_Description; }
set
...{
if (value != null)
if (value.Length > 2147483647)
throw new ArgumentOutOfRangeException("Invalid value for Description", value, value.ToString());
m_IsChanged |= (m_Description != value); m_Description = value;
}
}
/**//// <summary>
///
/// </summary>
public int Frequency
...{
get ...{ return m_Frequency; }
set ...{ m_IsChanged |= (m_Frequency != value); m_Frequency = value; }
}
/**//// <summary>
///
/// </summary>
public int IsActive
...{
get ...{ return m_IsActive; }
set ...{ m_IsChanged |= (m_IsActive != value); m_IsActive = value; }
}
/**//// <summary>
///
/// </summary>
public int Priority
...{
get ...{ return m_Priority; }
set ...{ m_IsChanged |= (m_Priority != value); m_Priority = value; }
}
/**//// <summary>
///
/// </summary>
public int ClassID
...{
get ...{ return m_ClassID; }
set ...{ m_IsChanged |= (m_ClassID != value); m_ClassID = value; }
}
/**//// <summary>
/// 对象的值是否被改变
/// </summary>
public bool IsChanged
...{
get ...{ return m_IsChanged; }
}
/**//// <summary>
/// 对象是否已经被删除
/// </summary>
public bool IsDeleted
...{
get ...{ return m_IsDeleted; }
}
#endregion
公有函数#region 公有函数
/**//// <summary>
/// 标记对象已删除
/// </summary>
public void MarkAsDeleted()
...{
m_IsDeleted = true;
m_IsChanged = true;
}
#endregion
重写Equals和HashCode#region 重写Equals和HashCode
/**//// <summary>
/// 用唯一值实现Equals
/// </summary>
public override bool Equals(object obj)
...{
if (this == obj) return true;
if ((obj == null) || (obj.GetType() != GetType())) return false;
Announcement castObj = (Announcement)obj;
return (castObj != null) &&
(m_ItemID == castObj.ItemID);
}
/**//// <summary>
/// 用唯一值实现GetHashCode
/// </summary>
public override int GetHashCode()
...{
int hash = 57;
hash = 27 * hash * m_ItemID.GetHashCode();
return hash;
}
#endregion
public override bool EqualsID(int ID)
...{
return this.ItemID == ID;
}
}
}
四、修改Announcement.hbm.xml文件,最后代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="DZ_Portal.App.Model.Announcement,DZ_Portal.App" table="PT_Announcement" lazy="false">
<id name="ItemID" column="ItemID" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property column="ModuleID" type="Int32" name="ModuleID" />
<property column="CreateByUser" type="String" name="CreateByUser" not-null="true" length="50" />
<property column="CreatedDate" type="DateTime" name="CreatedDate" not-null="true" />
<property column="Title" type="String" name="Title" not-null="true" length="100" />
<property column="MoreLink" type="String" name="MoreLink" not-null="true" length="150" />
<property column="ExpireDate" type="DateTime" name="ExpireDate" not-null="true" />
<property column="Description" type="String" name="Description" not-null="true" length="2147483647" />
<property column="Frequency" type="Int32" name="Frequency" not-null="true" />
<property column="IsActive" type="Int32" name="IsActive" not-null="true" />
<property column="Priority" type="Int32" name="Priority" not-null="true" />
<property column="ClassID" type="Int32" name="ClassID" not-null="true" />
</class>
</hibernate-mapping>
<!--1。HBM映射配置文件中“urn:nhibernate-mapping-2.0 ”需改为“urn:nhibernate-mapping-2.2”
2。 <class name="类名,程序集名" table="Archives" >需改为<class name="类名,程序集名
" table="Archives" lazy="false">,需要添加“lazy="false"”,不然会出现错误:
he following types may not be used as proxies: xxxxx: method get_CreateDate should be virtual
3。Spring.Data.NHibernate程序集变更为:Spring.Data.NHibernate12 (注意:Spring.Data.NHibernate
程序集仍然存在,是对NHibernate1.0X的支持,Spring.Data.NHibernate12 为新增的支持NHibernate1.2.0GA的程序集)
-->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="DZ_Portal.App.Model.Announcement,DZ_Portal.App" table="PT_Announcement" lazy="false">
<id name="ItemID" column="ItemID" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property column="ModuleID" type="Int32" name="ModuleID" />
<property column="CreateByUser" type="String" name="CreateByUser" not-null="true" length="50" />
<property column="CreatedDate" type="DateTime" name="CreatedDate" not-null="true" />
<property column="Title" type="String" name="Title" not-null="true" length="100" />
<property column="MoreLink" type="String" name="MoreLink" not-null="true" length="150" />
<property column="ExpireDate" type="DateTime" name="ExpireDate" not-null="true" />
<property column="Description" type="String" name="Description" not-null="true" length="2147483647" />
<property column="Frequency" type="Int32" name="Frequency" not-null="true" />
<property column="IsActive" type="Int32" name="IsActive" not-null="true" />
<property column="Priority" type="Int32" name="Priority" not-null="true" />
<property column="ClassID" type="Int32" name="ClassID" not-null="true" />
</class>
</hibernate-mapping>
<!--1。HBM映射配置文件中“urn:nhibernate-mapping-2.0 ”需改为“urn:nhibernate-mapping-2.2”
2。 <class name="类名,程序集名" table="Archives" >需改为<class name="类名,程序集名
" table="Archives" lazy="false">,需要添加“lazy="false"”,不然会出现错误:
he following types may not be used as proxies: xxxxx: method get_CreateDate should be virtual
3。Spring.Data.NHibernate程序集变更为:Spring.Data.NHibernate12 (注意:Spring.Data.NHibernate
程序集仍然存在,是对NHibernate1.0X的支持,Spring.Data.NHibernate12 为新增的支持NHibernate1.2.0GA的程序集)
-->
五、把这两个文件都拷贝到DZ_Portal.App项目下的Model文件夹下(因为它们都属于Model层)。
下一章我们将介绍另外两个层的类。
原创作品出自努力偷懒,转载请说明文章出处:http://blog.csdn.net/kfarvid或 http://www.cnblogs.com/kfarvid/