打造3层架构在线电影网站的方法
打造3层架构在线电影网站的方法
1、 数据层
数据层由四个类组成,其中两个位信息承载类,MovieDetails and CategoryDetails,只有属性,无任何方法,分别代表表movies和categories中某行。另外两个为基本数据操作类,提供相应的具体数据操作,分别为:Movies and Categories。
以下分别用类图表示四个类的具体情况:
MovieDetails |
+ movie_id:int
+ category_id:int
+ level:string
+ title:string
+ intro:string
+ uptime: DateTime
+ showtime: DateTime
+ viewcount:int
+ image:string
+ address:string |
|
CategoryDetails |
+ category_id:int
+ name:string
+ category_intro:string |
|
Movie |
|
+Movies(in connectionString:string)
+GetMovies():<unspecified>
+GetAllMovies():<unspecified>
+GetMoviesByCount():<unspecified>
+GetHeadlines():<unspecified>
+GetGetMovieDetails():<unspecified>
+GetDetailsRow():<unspecified>
+Add():int
+Update():bool
+Delete():bool
+AddViewcount():bool |
Categories |
|
+Categories(in connectionString:string)
+GetCategories():<unspecified>
+GetCategoryName():<unspecified>
+GetCategoryDetails():<unspecified>
+GetDetailsRow():<unspecified>
+Add():int
+Update():bool
+Delete():bool |
以下详细描述两个基本数据操作类的方法:
Movies:
方法详情 |
描述 |
public Movies( string newConnectiionString) |
类构造函数,以连接字符串作为参数 |
public DataSet GetMovies(int category_id) |
返回指定类型的所有电影条目 |
public DataSet GetHeadines(int category_id) |
返回指定类型条目的所有电影条目的movie_id、title、uptime、viewcount、image |
public DataSet GetAllMovies() |
以上传时间为顺序返回所有电影信息 |
public DataSet GetMoviesByCount() |
以点击率为顺序返回所有电影信息 |
public DataSet GetHeadlines(int category_id) |
返回某一分类的电影头信息 |
public MoviesDetails GetMovieDetails(int movie_id) |
返回一个由movie_id指定的电影条目的具体内容 |
public DataRow GetDetailsRow(int movie_id) |
返回电影详细信息的DataRow |
public int Add(int category_id,string level,string title,string intro,datetime uptime,datetime showtime,string image,string address) |
添加新的电影条目,如果成功则返回新的movie_id,如果有重复记录,则返回-1 |
public bool Update(int movie_id,int category_id,string level,string title,string intro,datetime showtime, string image,string address) |
更新指定电影条目的相关信息 |
public bool Delete(int movie_id) |
删除指定的电影条目 |
public bool AddViewcount(int movie_id) |
给指定的电影条目增加1单位访问量 |
Categories:
方法详情 |
描述 |
public Categories( string newConnectiionString) |
类构造函数,以连接字符串作为参数 |
public DataSet GetCategories() |
返回包含所有分类的DataSet |
public DataSet GetCategoryName() |
返回所有分类的名称和编号 |
public CategoryDetails GetCategoryDetails(int category_id) |
返回描述指定分类的CategoryDetails实例 |
public DataRow GetDetailsRow(int category_id) |
返回指定ID分类条目的DataRow |
public int Add(string name,string category_intro) |
添加新的分类条目,如果成功则返回新的category_id,如果有重复记录,则返回-1 |
public bool Update(int category_id,string name,string category_intro) |
更新指定分类条目的详细信息 |
public bool Delete(int category_id) |
删除指定分类条目 |
Movies:
using System;
using System.Data;
using System.Data.SqlClient;
namespace Coofucoo.Data
{
public class MovieDetails
{
public int movie_id;
public int category_id;
public string level;
public string title;
public string intro;
public DateTime uptime;
public DateTime showtime;
public int viewcount;
public string image;
public string address;
}
public class Movies : Coofucoo.Core.DbObject
{
public Movies(string newConnectionString) : base(newConnectionString)
{}
// return all the Movie of the specified category
public DataSet GetMovies(int category_id)
{
// create the parameters
SqlParameter[] parameters = {
new SqlParameter("@category_id", SqlDbType.Int, 4)
};
// set the values
parameters[0].Value = category_id;
return RunProcedure("GetMovies", parameters, "movies");
}
// return all the Movie order by upload time
public DataSet GetAllMovies()
{
return RunProcedure("GetAllMovies",new IDataParameter[]{},"movies");
}
public DataSet GetMoviesByCount()
{
return RunProcedure("GetMoviesByCount",new IDataParameter[]{},"MoviesByCount");
}
// return the headlines for the current and approved Movie
public DataSet GetHeadlines(int category_id)
{
// create the parameter
SqlParameter[] parameters = { new SqlParameter("@category_id", SqlDbType.Int, 4) };
parameters[0].Value = category_id;
return RunProcedure("GetHeadines", parameters, "Headlines");
}
// return only the record with the specified ID
public MovieDetails GetMovieDetails(int movie_id)
{
// create the parameter
SqlParameter[] parameters = { new SqlParameter("@movie_id", SqlDbType.Int, 4) };
parameters[0].Value = movie_id;
using(DataSet Movie = RunProcedure("GetMovieDetails", parameters, "MovieDetails"))
{
MovieDetails details = new MovieDetails();
// if the record was found, set the properties of the class instance
if (Movie.Tables[0].Rows.Count > 0)
{
DataRow rowMovie = Movie.Tables[0].Rows[0];
details.movie_id = Convert.ToInt32(rowMovie["movie_id"]);
details.category_id = Convert.ToInt32(rowMovie["category_id"]);
details.level = rowMovie["levell"].ToString();
details.title = rowMovie["title"].ToString();
details.intro = rowMovie["intro"].ToString();
details.uptime = Convert.ToDateTime(rowMovie["uptime"]);
details.showtime = Convert.ToDateTime(rowMovie["showtime"]);
details.viewcount = Convert.ToInt32(rowMovie["viewcount"]);
details.image = rowMovie["image"].ToString();
details.address = rowMovie["address"].ToString();
}
else
details.movie_id = -1;
return details;
}
}
// return only the record with the specified ID
public DataRow GetDetailsRow(int movie_id)
{
// create the parameter
SqlParameter[] parameters = { new SqlParameter("@movie_id", SqlDbType.Int, 4) };
parameters[0].Value = movie_id;
using(DataSet Movie = RunProcedure("GetMovieDetails", parameters, "MovieDetailsRow"))
{
return Movie.Tables[0].Rows[0];
}
}
// delete the record identified by the specified ID
public bool Delete(int movie_id)
{
int numAffected;
// create the parameter
SqlParameter[] parameters = { new SqlParameter("@movie_id", SqlDbType.Int, 4) };
parameters[0].Value = movie_id;
RunProcedure("DeleteMovie", parameters, out numAffected);
return (numAffected == 1);
}
// update the Movie identified by the specified ID
public bool Update(int movie_id, int category_id, string level, string title, string intro,
DateTime showtime, string image, string address)
{
int numAffected;
// create the parameters
SqlParameter[] parameters = {
new SqlParameter("@movie_id", SqlDbType.Int, 4),
new SqlParameter("@category_id", SqlDbType.Int, 4),
new SqlParameter("@levell", SqlDbType.NVarChar, 50),
new SqlParameter("@title", SqlDbType.NVarChar, 50),
new SqlParameter("@intro", SqlDbType.Text),
new SqlParameter("@showtime", SqlDbType.DateTime),
new SqlParameter("@image", SqlDbType.NVarChar, 100),
new SqlParameter("@address", SqlDbType.NVarChar, 100),
};
// set the values
parameters[0].Value = movie_id;
parameters[1].Value = category_id;
parameters[2].Value = level.Trim();
parameters[3].Value = title.Trim();
parameters[4].Value = intro.Trim();
parameters[5].Value = showtime;
parameters[6].Value = image.Trim();
parameters[7].Value = address.Trim();
RunProcedure("UpdateMovie", parameters, out numAffected);
return (numAffected == 1);
}
// add a Movie
public int Add(int category_id, string level, string title, string intro,
DateTime uptime, DateTime showtime, string image, string address)
{
int numAffected;
// create the parameters
SqlParameter[] parameters = {
new SqlParameter("@category_id", SqlDbType.Int, 4),
new SqlParameter("@level", SqlDbType.NVarChar, 50),
new SqlParameter("@title", SqlDbType.NVarChar, 50),
new SqlParameter("@intro", SqlDbType.Text),
new SqlParameter("@uptime", SqlDbType.DateTime),
new SqlParameter("@showtime", SqlDbType.DateTime),
new SqlParameter("@image", SqlDbType.NVarChar, 100),
new SqlParameter("@address", SqlDbType.NVarChar, 100),
new SqlParameter("@movie_id", SqlDbType.Int, 4)
};
// set the values
parameters[0].Value = category_id;
parameters[1].Value = level.Trim();
parameters[2].Value = title.Trim();
parameters[3].Value = intro.Trim();
parameters[4].Value = uptime;
parameters[5].Value = showtime;
parameters[6].Value = image.Trim();
parameters[7].Value = address.Trim();
parameters[8].Direction = ParameterDirection.Output;
RunProcedure("InsertMovie", parameters, out numAffected);
return (int)parameters[8].Value;
}
// set the Viewcount++
public bool AddViewcount(int movie_id)
{
int numAffected;
// create the parameters
SqlParameter[] parameters = {
new SqlParameter("@movie_id", SqlDbType.Int, 4)
};
// set the values
parameters[0].Value = movie_id;
RunProcedure("AddViewcount", parameters, out numAffected);
return (numAffected == 1);
}
}
}
Categories:
using System;
using System.Data;
using System.Data.SqlClient;
namespace Coofucoo.Data
{
public class CategoryDetails
{
public int category_id;
public string name;
public string category_intro;
}
public class Categories : Coofucoo.Core.DbObject
{
public Categories(string newConnectionString) : base(newConnectionString)
{}
// return all the Categories
public DataSet GetCategories()
{
return RunProcedure("GetCategories", new IDataParameter[]{}, "Categories");
}
// return all name of the Categories
public DataSet GetCategoryName()
{
return RunProcedure("GetCategoryName", new IDataParameter[]{}, "CategoryName");
}
// return only the record with the specified ID
public CategoryDetails GetCategoryDetails(int category_id)
{
// create the parameter
SqlParameter[] parameters = { new SqlParameter("@category_id", SqlDbType.Int, 4) };
parameters[0].Value = category_id;
using(DataSet categories = RunProcedure("GetCategoryDetails", parameters, "CategoryDetails"))
{
CategoryDetails details = new CategoryDetails();
// if the record was found, set the properties of the class instance
if (categories.Tables[0].Rows.Count > 0)
{
DataRow rowCategory = categories.Tables[0].Rows[0];
details.category_id = (int)rowCategory["category_id"];
details.name = rowCategory["name"].ToString();
details.category_intro = rowCategory["category_intro"].ToString();
}
else
details.category_id = -1;
return details;
}
}
// return only the record with the specified ID
public DataRow GetDetailsRow(int category_id)
{
// create the parameter
SqlParameter[] parameters = { new SqlParameter("@category_id", SqlDbType.Int, 4) };
parameters[0].Value = category_id;
using(DataSet categories = RunProcedure("GetCategoryDetails", parameters, "CategoryDetails"))
{
return categories.Tables[0].Rows[0];
}
}
// delete the record identified by the specified ID
public bool Delete(int category_id)
{
int numAffected;
// create the parameter
SqlParameter[] parameters = { new SqlParameter("@category_id", SqlDbType.Int, 4) };
parameters[0].Value = category_id;
RunProcedure("DeleteCategory", parameters, out numAffected);
return (numAffected == 1);
}
// update Name, Description and ImageUrl of the record identified by the specified ID
public bool Update(int category_id, string name, string category_intro)
{
int numAffected;
// create the parameters
SqlParameter[] parameters = {
new SqlParameter("@category_id", SqlDbType.Int, 4),
new SqlParameter("@name", SqlDbType.NVarChar, 50),
new SqlParameter("@category_intro", SqlDbType.NVarChar, 100),
};
// set the values
parameters[0].Value = category_id;
parameters[1].Value = name.Trim();
parameters[2].Value = category_intro.Trim();
RunProcedure("UpdateCategory", parameters, out numAffected);
return (numAffected == 1);
}
// add a new category
public int Add(string name, string category_intro)
{
int numAffected;
// create the parameters
SqlParameter[] parameters = {
new SqlParameter("@name", SqlDbType.VarChar, 50),
new SqlParameter("@category_intro", SqlDbType.VarChar, 100),
new SqlParameter("@category_id", SqlDbType.Int, 4),
};
// set the values
parameters[0].Value = name.Trim();
parameters[1].Value = category_intro.Trim();
parameters[2].Direction = ParameterDirection.Output;
// run the procedure
RunProcedure("InsertCategory", parameters, out numAffected);
return (int)parameters[2].Value;
}
}
}