我想知道是否有更好的方法來解決我忽略的這個問題。 (我正在尋找第二個意見)有沒有更好的方法將對象從數據庫查詢映射到對象?
我想創建一個通用和簡單的方法來綁定對象到數據庫閱讀器查詢使用「Oracle.DataAccess.Client」。
爲了做到這一點,我最初想創建一個從OracleCommand繼承的對象;但是,OracleCommand是一個密封的對象。
爲了解決這個問題,我決定創建一個擴展方法,試圖將對象映射到數據庫中的每一行的通用列。
編輯:在我的情況下,我知道數據庫將看起來像什麼;但是,在運行時,我不知道數據庫在哪裏。即數據庫可能已經提前傳輸,並且最終用戶將在運行時指定數據庫的憑證。
下面是執行:
public static T[] Bind<T>(this OracleCommand oe, Binding binding, CommandBehavior Behavior = CommandBehavior.Default)
{
List<T> ret = new List<T>();
using (var reader = oe.ExecuteReader(Behavior))
{
while (reader.Read())
{
T unknownObj = (T)Activator.CreateInstance(typeof(T));
for (int i = 0; i < binding.GetBindCount(); i++)
{
var propinfo = unknownObj.GetType().GetProperties().ToList();
var prop = propinfo.Find((p) => p.Name == binding.GetBindValue(i, true));
prop.SetValue(unknownObj, reader[binding.GetBindValue(i, false)]);
}
ret.Add(unknownObj);
}
}
return ret.ToArray();
}
}
public class Binding
{
List<BindingMap> _map = new List<BindingMap>();
public void AddBind(String VariableName, String ColumnName)
{
_map.Add(new BindingMap(VariableName, ColumnName));
}
public String GetBindValue(int index, bool IsVariable = true)
{
var a = _map.ToArray();
return (IsVariable) ? a[index].Variable : a[index].Column;
}
public int GetBindCount()
{
return _map.Count;
}
}
public class BindingMap
{
public String Column;
public String Variable;
public BindingMap(String v, String c)
{
Variable = v;
Column = c;
}
}
有沒有更好的方式來做到這一點,我已經忽略了,或者這是一個聲音?
,將在真正的代碼中使用的方法是這樣的:
static void Main()
{
Binding b = new Binding();
b.AddBind("CreatedBy", "Create_by");
using (var Conn = new OracleConnection())
{
Conn.ConnectionString = od.Options.GetConnectionString();
using (var Command = new OracleCommand())
{
Command.Connection = Conn;
Command.CommandText = "Select * From Accounts";
Conn.Open();
var a = Command.Bind<Account>(b);
foreach (Account e in a)
{
Console.WriteLine(e.CreatedBy);
}
}
}
Console.Read();
}
public class Account
{
public String CreatedBy
{
get;
set;
}
}
井的問題,使用ORM?你不需要重新發明輪子。 –
'是否有更好的方法將對象從數據庫查詢映射到對象? - 是的,它被稱爲[實體框架](http://msdn.microsoft.com/zh-cn/data/aa937723)。我在日常基礎上看到的類似問題的數量讓我覺得微軟不會公開它的[數據訪問技術](http://msdn.microsoft.com/zh-cn/data/aa937685.aspx)好。 –
同意,ORM是要走的路。你可以使用EF http://stackoverflow.com/questions/82644/can-you-use-microsoft-entity-framework-with-oracle –