使用Repository模式时用,我常考虑到底是用静态方法(static)还是实例方法,我是用C#开发系统的,搜罗了一下,没找的有说服力的,自己决定了,等待验证
我开发c/s winform胖客户端系统时采用static,代码都的客户都运行,很少使用多线程,有没有公共的变量在多个方法中使用,自认为没啥问题。
好像大部分的系统都使用的实例方法
/// <summary>
/// 产品管理
/// </summary>
public class ProductRepository
{
public static Product GetById(int id)
{
var sqlstr = "select * from dbo.Product where Product_ID=@id";
using (var conn = Database.DbService())
{
return conn.Query<Product>(sqlstr, new { id }).Single();
}
}
public static void Delete(int id)
{
}
public static bool Exists(string no)
{
return false;
}
} public class Product
{
#region Fields
private int _product_id;
private int _parent_id;
private string _product_no = "";
private string _product_name = "";
private string _product_type = "";
private string _remark = "";
private string _creater = "";
private DateTime _create_date;
private string _data_availability = "";
#endregion
#region Public Properties
public int Product_ID
{
get { return _product_id; }
set
{
_product_id = value;
}
}
public int Parent_ID
{
get { return _parent_id; }
set
{
_parent_id = value;
}
}
public string Product_No
{
get { return _product_no; }
set
{
_product_no = value;
}
}
public string Product_Name
{
get { return _product_name; }
set
{
_product_name = value;
}
}
public string Product_Type
{
get { return _product_type; }
set
{
_product_type = value;
}
}
public string Remark
{
get { return _remark; }
set
{
_remark = value;
}
}
public string Creater
{
get { return _creater; }
set
{
_creater = value;
}
}
public DateTime Create_Date
{
get { return _create_date; }
set
{
_create_date = value;
}
}
#endregion
}