1 public interface IRepository<T> where T : Entity
2 {
3 T Load(int Id);
4 IList<T> Load();
5 void Save(T entity);
6 void Delete(T entity);
7 }
8
9 public class SqlToLinqRepository<T> : IRepository<T> where T : Entity
10 {
11 public static DataBaseDataContext context;
12 public static DataBaseDataContext Context
13 {
14 get
15 {
16 if (context == null)
17 return context = new DataBaseDataContext();
18 return context;
19 }
20 }
21
22 #region IRepository<T> 成员
23
24 public T Load(int Id)
25 {
26 throw new NotImplementedException();
27 }
28
29 public IList<T> Load()
30 {
31 throw new NotImplementedException();
32 }
33
34 public void Save(T entity)
35 {
36 throw new NotImplementedException();
37 }
38
39 public void Delete(T entity)
40 {
41 throw new NotImplementedException();
42 }
43
44 #endregion
45 }
T是我自己定义的实体类,因为每个实体类都有增删改相同的方法,所以我想写一个通用的类,但是因为这里的T不是DataContext里的实体,所以到这里我不知道怎么写了,请高手给一个思路!
比如我不用linq to sql 了,只要实现IRepository<T>接口就可以换一种数据库,如ACCESS!
你的T 如果是DataContext实体是不行的,除非再T再抽象一下,不直接使用DataContext,好像比较困难,也比较麻烦。
其实如果是这样,不妨换一种思路,可参考NHibernate,或者采用贫血模型,实体类和持久类分开,持久类采用provider模式
免费的用DBLinq,付费的用 ALinq 吧。
Interfaces:
public interface IRepository
{
void Initialize(DataContext dataContext);
bool Save();
bool Save(out string msg);
}
public interface IUserTable
{
int Id { get; set; }
IRepository CreateRepository();
}
public interface IUserTableRepository : IRepository
{
void Add(IUserTable obj);
void Delete(IUserTable obj);
void Delete(int pk);
object GetItem(int pk);
}
Repository:
public abstract class Repository<T> : IRepository
where T : class
{
protected object syncObject = new object();
#region ---- Constructor ----
protected Repository() { }
protected Repository(DataContext dataContext)
{
Initialize(dataContext);
}
#endregion
#region ---- Fields & Properities ----
public AdventureWorksDBDataContext CreateDataContext()
{
return StaticRepository.CreateDataContext();
}
private AdventureWorksDBDataContext _DataContext = null;
/// <summary>
/// Rechieve the DataContext
/// </summary>
public virtual AdventureWorksDBDataContext DataContext
{
get
{
if (_DataContext == null)
{
lock (syncObject)
{
if (_DataContext == null)
{
this._DataContext = CreateDataContext();
}
}
}
return _DataContext;
}
protected set { this._DataContext = value; }
}
public virtual Table<T> DataTable
{
get
{
return null;
}
}
#endregion
#region ---- Methods ----
public virtual void Initialize(DataContext dataContext)
{
this._DataContext = dataContext as MyDataContext;
}
//public virtual IQueryable<T> GetAll1()
//{
// return DataTable;
//}
/// <summary>
/// Properties should be used instead of Get/Set methods
/// in most situations. Methods are preferable to properties
/// in the following situations: the operation is a conversion,
/// is expensive or has an observable side-effect; the
/// order of execution is important; calling the member
/// twice in succession creates different results; a member
/// is static but returns a mutable value; or the member
/// returns an array."
/// </summary>
public virtual IQueryable<T> Items
{
get { return DataTable; }
}
/// <summary>
/// Save the changes into database
/// </summary>
/// <returns></returns>
public virtual bool Save()
{
string msg;
return Save(out msg);
}
/// <summary>
/// Execute linq statements, and output the message if exception catched
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public virtual bool Save(out string msg)
{
bool operateStatus = true;
try
{
this.DataContext.SubmitChanges();
msg = string.Empty;
}
catch (Exception ex)
{
operateStatus = false;
msg = ex.Message;
}
return operateStatus;
}
#endregion
}
UserTableRepository:
public abstract class UserTableRepository<T> : Repository<T>, IUserTableRepository
where T : class, IUserTable
{
#region ---- Constructor ----
protected UserTableRepository() { }
protected UserTableRepository(DataContext dataContext):base(dataContext)
{
}
#endregion
public override Table<T> DataTable
{
get
{
return DataContext.GetTable<T>();
}
}
#region ---- Methods ----
/// <summary>
/// Determine where the item with specialized <c>pk</c> exists or not
/// </summary>
/// <param name="pk">pk value</param>
/// <returns></returns>
public virtual bool Exists(int pk)
{
return Items.Any(a => a.Id == pk);
}
/// <summary>
/// Get item specified by its <c>pk</c>
/// </summary>
/// <param name="pk"></param>
/// <returns></returns>
public virtual T GetItem(int pk)
{
var qry = Items;
qry = qry.Where(a=>a.Id.Equals(pk));
return qry.FirstOrDefault();
//return Items.Where(a => a.Id. == pk).FirstOrDefault();
}
object IUserTableRepository.GetItem(int pk)
{
return GetItem(pk);
}
/// <summary>
/// Insert object
/// </summary>
/// <param name="obj"></param>
public virtual void Add(T item)
{
DataTable.InsertOnSubmit(item);
}
public virtual void Add(IUserTable item)
{
Add(item as T);
}
/// <summary>
/// Delete Object
/// </summary>
/// <param name="obj"></param>
public virtual void Delete(T item)
{
DataTable.DeleteOnSubmit(item);
}
public virtual void Delete(IUserTable item)
{
Delete(item as T);
}
/// <summary>
/// Delete item specified by its <c>pk</c>
/// </summary>
/// <param name="pk"></param>
/// <returns></returns>
public virtual void Delete(int pk)
{
if (Exists(pk))
{
T obj = GetItem(pk);
Delete(obj);
}
}
#endregion
}