首页 新闻 搜索 专区 学院

写一个通用的Linq to sql 增删改

0
悬赏园豆:50 [已关闭问题]
代码
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!

SapronLee的主页 SapronLee | 初学一级 | 园豆:140
提问于:2010-02-06 03:43
< >
分享
其他回答(3)
0

你的T 如果是DataContext实体是不行的,除非再T再抽象一下,不直接使用DataContext,好像比较困难,也比较麻烦。

qpgame01 | 园豆:210 (菜鸟二级) | 2010-02-06 10:18
0

其实如果是这样,不妨换一种思路,可参考NHibernate,或者采用贫血模型,实体类和持久类分开,持久类采用provider模式

查尔斯 | 园豆:3832 (老鸟四级) | 2010-02-06 11:48
其实我也是看了Hibernate代码,看到它都可以抽象到通用的增删改方法,而且返回的是自定义实体,所以我才想让linq to sql 也能这样!
支持(0) 反对(0) SapronLee | 园豆:140 (初学一级) | 2010-02-06 15:00
0

免费的用DBLinq,付费的用 ALinq 吧。

麦舒 | 园豆:452 (菜鸟二级) | 2010-02-07 16:26
0

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

}

 

晨牧 | 园豆:215 (菜鸟二级) | 2010-02-08 15:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册