我有一个返回IDataRecord的数据访问层。 我有一个服务于DataContracts(dto)的WCF服务。这些DataContracts由包含IDataRecord的参数化构造函数启动,如下所示:
[DataContract]
public class DataContractItem
{
[DataMember]
public int ID;
[DataMember]
public string Title;
public DataContractItem(IDataRecord record)
{
this.ID = Convert.ToInt32(record["ID"]);
this.Title = record["title"].ToString();
}
}
不幸的是我不能改变DAL,所以我不得不使用IDataRecord作为输入。但是在生成中,这种方法非常有效。映射在大多数情况下非常简单,有时它们有点复杂,但没有火箭科学。
但是,现在我希望能够使用泛型来实例化不同的DataContracts以简化WCF服务方法。我希望能够做到这样的事情:
public T DoSomething<T>(IDataRecord record) {
...
return new T(record);
}
所以我试着遵循以下解决方案:
使用带有构造函数的泛型类型接口。 不起作用:当然我们无法在界面中定义构造函数
使用静态方法实例化DataContract并创建包含此静态方法的类型化接口。 不起作用:当然我们不能在界面中定义静态方法
使用包含new()约束的泛型类型接口 不起作用:new()约束不能包含参数(IDataRecord)
使用工厂对象基于DataContract类型执行映射。 确实有效,但是:不是很干净,因为我现在有一个带有所有映射的switch语句在一个文件中。
我找不到真正干净的解决方案。有人可以为我阐明这个吗? 该项目对于任何复杂的映射技术而言都太小,对于“基于交换机”的工厂实现来说太大了。
答案 0 :(得分:2)
您可以通过反射调用类型构造函数:
public T DoSomething<T>(IDataRecord record)
{
//do something...
var ci = typeof(T).GetConstructor(new[] { typeof(IDataRecord) });
return (T)ci.Invoke(new object[] { record });
}
编辑:上述方法非常脆弱并且依赖于约定,因此另一种方法是为您的数据合同创建一个允许初始化的接口:
public interface IDataContract
{
void Initialise(IDataRecord record);
}
public T DoSomething<T>(IDataRecord record) where T : IDataContract, new()
{
//do something
T contract = new T();
contract.Initialise(record);
return contract;
}
答案 1 :(得分:2)
我认为您可以使用AutoMapper。它是一个开源项目,它可以让你做你想要的。在你做某事时你会这样做:
public TDestination DoSomething<TDestination>(IDataRecord record) {
return Mapper.Map(reader, reader.GetType(), typeof(TDestination));
}
在此之前,您需要设置地图
Mapper.CreateMap<IDataRecord, MyDestinationType>()
答案 2 :(得分:1)
我是这样做的。
DataRow的扩展方法,它允许我将业务对象转换为DataRow,反之亦然。
/// <summary>
/// Extension methods for DataRow.
/// </summary>
public static class DataRowExtensions
{
/// <summary>
/// Converts DataRow into business object.
/// </summary>
/// <typeparam name="TEntity">A type that inherits EntityBase.</typeparam>
/// <param name="dataRow">DataRow object to convert to business object.</param>
/// <returns>business object created from DataRow.</returns>
public static TEntity ToEntity<TEntity>(this DataRow dataRow)
where TEntity : EntityBase, new()
{
TEntity entity = new TEntity();
ExtensionHelper.TransformDataRowToEntity<TEntity, DataRow>(ref dataRow, ref entity);
return entity;
}
/// <summary>
/// Converts business object into DataRow.
/// </summary>
/// <typeparam name="TEntity">A type that inherits EntityBase.</typeparam>
/// <param name="dataRow">DataRow object to convert business object into.</param>
/// <param name="entity">Business object which needs to be converted to DataRow.</param>
public static void FromEntity<TEntity>(this DataRow dataRow, TEntity entity)
where TEntity : EntityBase, new()
{
ExtensionHelper.TransformEntityToDataRow<TEntity, DataRow>(ref entity, ref dataRow);
}
}
执行转换的实际辅助方法..
/// <summary>
/// Helper methods for transforming data objects into business objects and vice versa.
/// </summary>
/// <remarks>
/// <para>Most important implementation that takes care of universal transformation between business objects and data object.</para>
/// <para>Saves programmers from writing the same old code for every object in the system.</para>
/// </remarks>
public static class ExtensionHelper
{
/// <summary>
/// Transforms business object into DataRow.
/// </summary>
/// <typeparam name="TEntity">A type that inherits EntityBase.</typeparam>
/// <typeparam name="TDataRow">A type that inherits DataRow.</typeparam>
/// <param name="entity">business object which is transformed into DataRow object.</param>
/// <param name="dataRow">DataRow object which is transformed from business object.</param>
public static void TransformEntityToDataRow<TEntity, TDataRow>(ref TEntity entity, ref TDataRow dataRow)
where TDataRow : DataRow
where TEntity : EntityBase
{
IQueryable<DataField> entityFields = entity.GetDataFields();
foreach (DataColumn dataColoumn in dataRow.Table.Columns)
{
if (!dataColoumn.ReadOnly)
{
var entityField =
entityFields.Single(e => e.DataFieldMapping.MappedField.Equals(dataColoumn.ColumnName, StringComparison.OrdinalIgnoreCase));
if (entityField.Property.GetValue(entity, null) == null)
{
if (dataColoumn.AllowDBNull)
{
dataRow[dataColoumn] = System.DBNull.Value;
}
else
{
throw new Exception(dataColoumn.ColumnName + " cannot have null value.");
}
}
else
{
if (entityField.Property.GetType().IsEnum)
{
dataRow[dataColoumn] = Convert.ToByte(entityField.Property.GetValue(entity, null));
}
else
{
dataRow[dataColoumn] = entityField.Property.GetValue(entity, null);
}
}
}
}
}
/// <summary>
/// Transforms DataRow into business object.
/// </summary>
/// <typeparam name="TEntity">A type that inherits EntityBase.</typeparam>
/// <typeparam name="TDataRow">A type that inherits DataRow.</typeparam>
/// <param name="dataRow">DataRow object which is transformed from business object.</param>
/// <param name="entity">business object which is transformed into DataRow object.</param>
public static void TransformDataRowToEntity<TEntity, TDataRow>(ref TDataRow dataRow, ref TEntity entity)
where TDataRow : DataRow
where TEntity : EntityBase
{
IQueryable<DataField> entityFields = entity.GetDataFields();
foreach (var entityField in entityFields)
{
if (dataRow[entityField.DataFieldMapping.MappedField] is System.DBNull)
{
entityField.Property.SetValue(entity, null, null);
}
else
{
if (entityField.Property.GetType().IsEnum)
{
Type enumType = entityField.Property.GetType();
EnumConverter enumConverter = new EnumConverter(enumType);
object enumValue = enumConverter.ConvertFrom(dataRow[entityField.DataFieldMapping.MappedField]);
entityField.Property.SetValue(entity, enumValue, null);
}
else
{
entityField.Property.SetValue(entity, dataRow[entityField.DataFieldMapping.MappedField], null);
}
}
}
}
}
以下是我的示例Business-object的外观。请注意自定义属性..
/// <summary>
/// Represents User.
/// </summary>
[TableMapping("Users", "User", "Users")]
public class User : EntityBase
{
#region Constructor(s)
/// <summary>
/// Initializes a new instance of the User class.
/// </summary>
public User()
{
}
#endregion
#region Properties
#region Default Properties - Direct Field Mapping using DataFieldMappingAttribute
/// <summary>
/// Gets or sets the ID value of the AppUser object.
/// </summary>
[DataFieldMapping("UserID")]
[DataObjectFieldAttribute(true, true, false)]
[NotNullOrEmpty(Message = "UserID From UserDetails Table Is Required.")]
public override int Id
{
get;
set;
}
/// <summary>
/// Gets or sets the Username value of the User object.
/// </summary>
[DataFieldMapping("UserName")]
[Searchable]
[NotNullOrEmpty(Message = "Username Is Required.")]
public string UserName
{
get;
set;
}
/// <summary>
/// Gets or sets the FirstName value of the AppUser object.
/// </summary>
[DataFieldMapping("FirstName")]
[Searchable]
public string FirstName
{
get;
set;
}
/// <summary>
/// Gets or sets the LastName value of the AppUser object.
/// </summary>
[DataFieldMapping("LastName")]
[Searchable]
public string LastName
{
get;
set;
}
/// <summary>
/// Gets or sets the WebSite value of the AppUser object.
/// </summary>
[DataFieldMapping("WebSite")]
[ValidURL(Message = "Website is not in Proper Format.")]
public string WebSite
{
get;
set;
}
/// <summary>
/// Gets or sets the ContactNumber value of the AppUser object.
/// </summary>
[DataFieldMapping("ContactNumber")]
[Searchable]
public string ContactNumber
{
get;
set;
}
/// <summary>
/// Gets or sets a value indicating whether AppUser Object is active or inactive.
/// </summary>
[DataFieldMapping("IsActive")]
public bool IsActive
{
get;
set;
}
/// <summary>
/// Gets or sets the BirthDate value of the AppUser object.
/// </summary>
[DataFieldMapping("BirthDate")]
public DateTime? BirthDate
{
get;
set;
}
#region Derived Properties
/// <summary>
/// Gets the full name of the AppUser
/// </summary>
public string FullName
{
get { return this.FirstName + " " + this.LastName; }
}
/// <summary>
/// Gets the Age value of the AppUser
/// </summary>
public int Age
{
get { return this.BirthDate.HasValue ? this.BirthDate.Value.AgeInYears() : 0; }
}
#endregion
#endregion
}
这是我的客户代码..
/// <summary>
/// Gets User object by user name.
/// </summary>
/// <param name="username">UserName of the user</param>
/// <returns>User Object</returns>
public static User GetUserByUsername(string username)
{
try
{
return Adapter.GetUserByUserName(username)[0].ToEntity<User>();
}
catch
{
return null;
}
}