是確定在網絡使用這樣的: (我的應用程序是asp.net的MVC)我應該使用一個靜態的DbUtil類爲web,它是危險的嗎?
public static class DbUtil
{
public static int Insert(object o, string cs)
{
using (var conn = new SqlConnection(cs))
using (var cmd = conn.CreateCommand())
{
...
conn.Open();
return Convert.ToInt32(cmd.ExecuteScalar());
}
}
}
用法:
public class Repository<T> : IRepository<T>
{
public virtual int Insert(T o)
{
return DbUtil.Insert(o, Cs);
}
}
,並在服務或控制器構造注射後
public MyController(
IRepository<Organization> organizationRepository)
{
this.organizationRepository = organizationRepository;
}
這完全取決於你如何調用這個方法。你能給我們舉例說明它的用法嗎?否則,不可能說任何有用的事情。 – Steven 2010-10-04 13:06:20
@Steven我編輯了我的問題 – Omu 2010-10-04 13:11:29