我的代码工作正常,但我不确定我采用的方法是否是最佳实践。所以,我决定在这个论坛上提问。
所以这是我的情况:
我的模型看起来像这样:
public class Member
{
[Range(1, int.MaxValue)]
public int? MemberID { get; set; }
[Required]
public List<MemberExperience> MemberExperiences { get; set; }
[Required]
public string MemberAddress { get; set; }
[Required]
public MemberInformation MemberInformation { get; set; }
}
public class MemberExperience
{
[Required]
[Range(1, int.MaxValue)]
public int FromYear { get; set; }
[Required]
[Range(1, int.MaxValue)]
public int ToYear { get; set; }
[Required]
public string CompanyAddress { get; set; }
[Required]
public string ProgrammingLanguage { get; set; }
}
public class MemberInformation
{
[Required]
public string FullName { get; set; }
[Required]
public DateTime BirthDate { get; set; }
[Required]
public string TelephoneNumber { get; set; }
}
从客户端来说,我传递给服务器的是这样的:
{
"MemberExperiences": [
{
"FromYear": 2005,
"ToYear": 2008,
"CompanyAddress": "string",
"ProgrammingLanguage": "Javascript"
},
{
"FromYear": 2009,
"ToYear": 2012,
"CompanyAddress": "string",
"ProgrammingLanguage": "C++"
},
{
"FromYear": 2013,
"ToYear": 2017,
"CompanyAddress": "string",
"ProgrammingLanguage": "C#"
}
],
"MemberAddress": "string",
"MemberInformation": {
"FullName": "string",
"BirthDate": "1992-01-01",
"TelephoneNumber": "string"
}
}
然后我的控制器看起来像这样(我想从客户端传递的数据将转换为Member
模型,其中包含所有数据):
[HttpPost]
public HttpActionResult AddMember([FromBody] Member member)
{
var response = AddMemberToDatabase(member);
return Ok(response);
}
但是,因为我不知道Dapper
(我使用Dapper
与数据库进行通信)如何转换映射变量(在本例中为MemberExperience
和{{ 1}})成单个对象并被识别。所以,我所做的就是这样:
获取所有成员数据,但在将其插入数据库之前,我将创建一个存储来自MemberInformation
和MemberExperience
的数据列表的类。
将所有数据加入1个单长字符串中,并用分隔符分隔,只有数据库才允许访问它并知道它。
SingleMember 类:
MemberInformation
AddMemberToDatabase 功能:
public class SingleMember
{
public int? MemberID { get; set; }
public string FromYears { get; set; }
public string ToYears { get; set; }
public string CompanyAddresses { get; set; }
public string ProgrammingLanguages { get; set; }
public string MemberAddress { get; set; }
public string FullName { get; set; }
public DateTime BirthDate { get; set; }
public string TelephoneNumber { get; set; }
}
然后,从private int AddMemberToDatabase(Member members)
{
var separator = "$Format";
var singleMember = new SingleMember
{
FromYears = string.Join(separator, members.MemberExperiences.Select(x => x.FromYear)),
ToYears = string.Join(separator, members.MemberExperiences.Select(x => x.ToYear)),
CompanyAddresses = string.Join(separator, members.MemberExperiences.Select(x => x.CompanyAddress)),
ProgrammingLanguages = string.Join(separator, members.MemberExperiences.Select(x => x.ProgrammingLanguage)),
MemberAddress = members.MemberAddress;
FullName = members.MemberInformation.FullName;
BirthDate = members.MemberInformation.BirthDate;
TelephoneNumber = members.MemberInformation.TelephoneNumber;
};
using (TransactionScope trans = new TransactionScope())
using (IDbConnection conn = new SqlConnection("MyConnection"))
{
conn.Open();
int rowsAffected = conn.Execute("MyStoredProcedure", singleMember);
trans.Complete();
return rowsAffected;
}
}
开始,它将按照定义的格式MyStoredProcedure
拆分连接的数据,并将其按1插入到表1中,直到被拆分的项目不再被拆分
$Format
有没有更好的方法呢?
你的回答非常感谢。
由于
答案 0 :(得分:0)
我认为为CRUD操作创建存储过程效果不佳。如果您有很多表,请在您的应用程序中创建c#支持以生成SQL命令(插入,更新,删除),并将它们作为带有参数的SqlCommand(事务处理器)发送到数据库中。