C# 将 Dapper POCO 和 Dapper.Fluent EntityMap 作为参数传递
C# Passing Dapper POCO and Dapper.Fluent EntityMap as a arguments
我目前正在尝试通过 dapper 使用 MySQL 数据库进行设置。
我已经为 dapper 声明了 POCO:
public class DevicesOnTesterDbTable
{
public string UUID { get; set; }
public string DeviceType { get; set; }
public string DeviceAddedAt { get; set; }
public uint Address { get; set; }
}
我也有 Dapper.Fluent EntityMap table:
public class DevicesOnTesterDbTableMap : EntityMap<DevicesOnTesterDbTable>
{
public DevicesOnTesterDbTableMap()
{
Map(p => p.UUID).ToColumn("devices_on_tester_uuid");
Map(p => p.DeviceType).ToColumn("devices_on_tester_type");
Map(p => p.DeviceAddedAt).ToColumn("devices_on_tester_add_at");
Map(p => p.Address).ToColumn("devices_on_tester_address");
}
}
我的数据库大约有十个 table,所以我有十对 POCO 和 EntityMap 类。因此,在阅读的情况下,我必须为每个 table:
执行类似的操作
public static List<DevicesOnTesterDbTable> ReadDevices(string server)
{
FluentMapper.Initialize(config =>
{
config.AddMap(new DevicesOnTesterDbTableMap());
});
try
{
using (var mySqlConnection = OpenConnection(server))
{
mySqlConnection.Open();
return mySqlConnection.Query<DevicesOnTesterDbTable>("Select * from power_source_calibration").ToList();
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
现在有没有办法将这对传递给其他一些方法,这些方法将执行 read\write\update 等常见操作?或者也许有更好的方法?
有更好的方法;其实你做错了。
您不需要像 ReadDevices
那样在每个方法中调用 FluentMapper.Initialize
。理想情况下,您的所有映射(针对所有实体)应该只在您的应用程序启动时发生一次。
以下来自here:
Initialize your mapping at the start of your application.
FluentMapper.Initialize(config =>
{
config.AddMap(new InvoiceMap());
});
此外,请参考 this 问题,该问题显示了如何在应用程序启动时执行此操作。参考 Register()
方法,它是 static
并且调用者在某个地方的应用程序启动时调用它一次。
我目前正在尝试通过 dapper 使用 MySQL 数据库进行设置。
我已经为 dapper 声明了 POCO:
public class DevicesOnTesterDbTable
{
public string UUID { get; set; }
public string DeviceType { get; set; }
public string DeviceAddedAt { get; set; }
public uint Address { get; set; }
}
我也有 Dapper.Fluent EntityMap table:
public class DevicesOnTesterDbTableMap : EntityMap<DevicesOnTesterDbTable>
{
public DevicesOnTesterDbTableMap()
{
Map(p => p.UUID).ToColumn("devices_on_tester_uuid");
Map(p => p.DeviceType).ToColumn("devices_on_tester_type");
Map(p => p.DeviceAddedAt).ToColumn("devices_on_tester_add_at");
Map(p => p.Address).ToColumn("devices_on_tester_address");
}
}
我的数据库大约有十个 table,所以我有十对 POCO 和 EntityMap 类。因此,在阅读的情况下,我必须为每个 table:
执行类似的操作public static List<DevicesOnTesterDbTable> ReadDevices(string server)
{
FluentMapper.Initialize(config =>
{
config.AddMap(new DevicesOnTesterDbTableMap());
});
try
{
using (var mySqlConnection = OpenConnection(server))
{
mySqlConnection.Open();
return mySqlConnection.Query<DevicesOnTesterDbTable>("Select * from power_source_calibration").ToList();
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
现在有没有办法将这对传递给其他一些方法,这些方法将执行 read\write\update 等常见操作?或者也许有更好的方法?
有更好的方法;其实你做错了。
您不需要像 ReadDevices
那样在每个方法中调用 FluentMapper.Initialize
。理想情况下,您的所有映射(针对所有实体)应该只在您的应用程序启动时发生一次。
以下来自here:
Initialize your mapping at the start of your application.
FluentMapper.Initialize(config => { config.AddMap(new InvoiceMap()); });
此外,请参考 this 问题,该问题显示了如何在应用程序启动时执行此操作。参考 Register()
方法,它是 static
并且调用者在某个地方的应用程序启动时调用它一次。