C# 使用反射C的通用CRUD函数#
我的情况是,我必须创建一个通用函数,用于将值插入MySQL数据库。到目前为止,我的方法如下。当另一个类中没有复合类时,此代码可以正常工作。如果实体中有导航属性,那么代码就会中断,我被困在这里 (ORM-我们不能使用它,因为它的客户要求) 让我们拿一个实体(这种设计不在我手里,这些类已经存在了) 我无法更改此属性(实体)C# 使用反射C的通用CRUD函数#,c#,mysql,system.reflection,C#,Mysql,System.reflection,我的情况是,我必须创建一个通用函数,用于将值插入MySQL数据库。到目前为止,我的方法如下。当另一个类中没有复合类时,此代码可以正常工作。如果实体中有导航属性,那么代码就会中断,我被困在这里 (ORM-我们不能使用它,因为它的客户要求) 让我们拿一个实体(这种设计不在我手里,这些类已经存在了) 我无法更改此属性(实体) [表(Name=“person”)] 公共阶层人士 { [Column(Name=“id”,IsPrimaryKey=true)]//Column是我的自定义属性 公共int I
[表(Name=“person”)]
公共阶层人士
{
[Column(Name=“id”,IsPrimaryKey=true)]//Column是我的自定义属性
公共int Id{get;set;}
[列(Name=“Name”,IsPrimaryKey=true)]
公共字符串名称{get;set;}
//这是我无法得到它的地方如何插入
//具有我上面编写的函数的子类
[列(IsChild=true)]
公共列表联系人{get;set;}//导航属性
//这个属性也有同样的问题
//如何为父级插入
[列(IsParent=true)]
公共用户{get;set;}
}
//我将把这个insert方法称为
公共c=新公共();
c、 创建(个人p);//p将作为参数从控制器传递
请允许我建议使用micro orm,而不是手工编写所有这些功能
我建议使用or(您可以将其作为单个文件添加到应用程序中)。看起来您正在尝试创建自己的ORM实现。这将是非常复杂的,绝对不值得为客户项目付出努力
您可以使用代码生成工具从数据库结构生成数据访问层。以CodeSmith为例。数据库结构一点也不好,它已经存在很长时间了,客户没有心情改变它,我认为任何代码生成工具都不起作用,这就是为什么我要采用这种手动方法。可能客户机不值得,但就知识而言,它绝对值得
public string create(object entity){
var sql = new OdbcCommand();
var d = new StringDictionary();
var dd = new StringDictionary();
try
{
var objectType = entity.GetType();
var properties = objectType.GetProperties();
var tableName = objectType.GetCustomAttributes(false).Select(x => (x as TableAttribute).Name).FirstOrDefault();
foreach (var prop in properties)
{
if (prop.DeclaringType != typeof (BasicRequest)) // BasicRequest is base class
{
if (
prop.GetCustomAttributes(false).Where(y=>y is ColumnAttribute).Select(x => (x as ColumnAttribute)).FirstOrDefault().IsPrimaryKey)
{
continue;
}
if (prop.PropertyType.IsGenericType) // here comes the problem
{
// the problem is if a class has List of its child
objects then how to insert them since here parent
is not yet inserted.
}
if (prop.GetCustomAttributes(false).Where(y => y is ColumnAttribute).Select(x => (x as ColumnAttribute)).FirstOrDefault().IsParent)
{
// same problem here also but this time this object is
parent object of the entity
/*parentObject = prop.GetValue(entity, null);
CreateNew(parentObject);
*/
continue;
}
d[prop.GetCustomAttributes(false).Where(y => y is ColumnAttribute).Select(x => (x as ColumnAttribute).Name).FirstOrDefault()] = DBUtil.FixSQLString(Convert.ToString(prop.GetValue(entity, null)));
}
else
{
continue;
}
}
Connection coxn = ConnectionPool.GetInstance().GetCoxn(Constants.MyName,ConnectionPool.WriteServer,"db_name");
sql.Connection = coxn.odbcConnection;
sql.CommandType = CommandType.Text;
sql.CommandText = DBUtil.CreateInsert(tableName, d);
int affected = sql.ExecuteNonQuery();
if (affected != 1)
{
ErrorMessage = "Insert Failed Unexpectedly";
return "0";
}
return "1";
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
return "0";
}
}
[Table(Name="person")]
public class Person
{
[Column(Name="id",IsPrimaryKey=true)] // Column is my custom attribute
public int Id{get;set;}
[Column(Name="name",IsPrimaryKey=true)]
public string Name{get;set;}
// this is where i'm not able get it how do i insert
// child class with the function i wrote above
[Column(IsChild=true)]
public List<Contact> contact{get;set;} // Navigation property
//Same problem with this property also
// how do i do insert for parent
[Column(IsParent=true)]
public User user{get;set;}
}
// i'll call that insert method as
Common c = new Common();
c.Create(Person p); // p will be passed as argument from controller