我创建了一个数据库,其中所有操作都由存储过程处理。我需要为我的同事编写一个库,以便稍后用于他的网站。虽然目前该库应该与BackgroundService(通过OPC收集数据)一起使用,但那里有不同的实体。
有很多文章说记录用作 DTO,而 EF 可以轻松地与类一起使用,因为它们是可变的并且基于引用。但我还是坚持了现在应该选择的方式。如果我的实体是记录,那么像从表中获取所有记录这样的操作会更容易,但如果我添加验证并考虑在未来的课程中使用 EF 或 Dapper 似乎更合适。
知道同时拥有两个班级和他们的记录对应者是否那么糟糕也很有趣?
/* Department.cs */
namespace DB.Models;
public class Department
{
private readonly short _id;
public short ID {
get => _id;
init {
if (value < 0)
throw new ArgumentOutOfRangeException($"{nameof(ID)} cannot be negative", nameof(ID));
_id = value;
}
}
private readonly short _itid;
public short ITID {
get => _itid;
init {
if (value < 0)
throw new ArgumentOutOfRangeException($"{nameof(ITID)} cannot be negative", nameof(ITID));
_itid = value;
}
}
private readonly string _name;
public string Name {
get => _name;
init {
if (value is null)
throw new ArgumentNullException(nameof(Name), $"{nameof(Name)} cannot be null");
if (value.All(char.IsWhiteSpace))
throw new ArgumentException($"{nameof(Name)} cannot be whitespace", nameof(Name));
_name = value;
}
}
private readonly string? _description;
public string? Description {
get => _description;
init {
if (value is not null && value.All(char.IsWhiteSpace))
throw new ArgumentException($"{nameof(Department)} cannot be whitespace", nameof(Department));
_description = value;
}
}
public Department(short id, short itid, string name, string? description)
{
ID = id;
ITID = itid;
Name = name;
Description = description;
}
}
/* MZTP.cs */
namespace DB;
public class MZTP
{
// constructor, properties and other methods of the class
public async Task<List<Department>?> GetDepartments()
{
List<Department>? departments = new();
try {
using (FbConnection con = new(ConfigConnectionString)) {
con.Open();
using (FbCommand cmd = new() { CommandText = "GET_DEPARTMENTS", Connection = con, CommandType = CommandType.StoredProcedure }) {
FbDataReader reader = await cmd.ExecuteReaderAsync();
if (!reader.HasRows) return departments;
while (await reader.ReadAsync()) {
var id = reader.GetInt16(reader.GetOrdinal("ID"));
var itid = reader.GetInt16(reader.GetOrdinal("ITID"));
var name =reader.GetString(reader.GetOrdinal("NAME"));
var descrOrdinal = reader.GetOrdinal("DESCRIPTION");
var description = (await reader.IsDBNullAsync(descrOrdinal))
? null
: reader.GetString(descrOrdinal);
departments.Add(new Department(id, itid, name, description));
}
}
}
return departments;
} catch (Exception err) {
ErrorMessage = err.Message;
departments = null;
}
return departments;
}
public async Task<short?> UpdateDepartment(Department department)
{
try {
using (FbConnection con = new(ConfigConnectionString)) {
con.Open();
using (FbCommand cmd = new() { CommandText = "UPDATE_DEPARTMENT", Connection = con, CommandType = CommandType.StoredProcedure }) {
cmd.Parameters.AddRange(new FbParameter[] {
new FbParameter("@ID", department.ID),
new FbParameter("@ITID", department.ITID),
new FbParameter("@NAME", department.Name),
new FbParameter("@DESCRIPTION", department.Description)
});
if (await cmd.ExecuteScalarAsync() is short id) {
return id;
} else {
return null;
}
}
}
} catch (Exception err) {
ErrorMessage = err.Message;
return null;
}
}
}
“坏”是主观的。它是否能实现您想要的目标,而不会带来太多负面后果,其中正面和负面影响都取决于您的使用情况?伟大的!这样做!
我要说的是,编写自己的命令/读取器代码几乎没有任何好处;这只是“忙碌的工作”——像 Dapper 这样的工具可以用 1 行代码替换大部分代码。