- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将格式化程序添加到我的 Automapper 配置以设置所有 DateTime?
字段的样式。我已经尝试在全局范围内添加我的格式化程序:
Mapper.AddFormatter<DateStringFormatter>();
以及具体映射本身:
Mapper.CreateMap<Post, PostViewModel>()
.ForMember(dto => dto.Published, opt => opt.AddFormatter<DateStringFormatter>());
但似乎都不起作用 - 它总是以正常格式输出日期。作为引用,这是我正在使用的 ViewModel 以及其余配置:
public class DateStringFormatter : BaseFormatter<DateTime?>
{
protected override string FormatValueCore(DateTime? value)
{
return value.Value.ToString("d");
}
}
public abstract class BaseFormatter<T> : IValueFormatter
{
public string FormatValue(ResolutionContext context)
{
if (context.SourceValue == null)
return null;
if (!(context.SourceValue is T))
return context.SourceValue == null ? String.Empty : context.SourceValue.ToString();
return FormatValueCore((T)context.SourceValue);
}
protected abstract string FormatValueCore(T value);
}
后 View 模型:
public int PostID { get; set; }
public int BlogID { get; set; }
public string UniqueUrl { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public string BodyShort { get; set; }
public string ViewCount { get; set; }
public DateTime CreatedOn { get; set; }
private DateTime? published;
public DateTime? Published
{
get
{
return (published.HasValue) ? published.Value : CreatedOn;
}
set
{
published = value;
}
}
我做错了什么?
谢谢!
最佳答案
格式化程序仅在目标成员类型为“字符串”类型时应用。由于“Published”是“DateTime?”类型,因此永远不会应用格式化程序。您在这里有几个选择:
我们通常选择 1),除非显示的值确实仅适用于此 View ,否则我们选择选项 2)。
关于Automapper 格式化程序不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1652749/
我正在尝试在两个对象列表之间进行映射。源类型具有类型为 A 的复杂属性。 ;目标类型是类型 A 的扁平子集加上源类型中的附加标量属性。 public class A { public int
Automapper 是否可以将平面对象映射到复杂的对象图? Mapper.CreateMap() 将 PersonDto.BirthCertificateFatherName 映射到 Person
我有类似 this. 的问题但是这个答案对我不起作用。 我正在做一个小项目。我有两个域模型(Post,Source): public class Post { public Post()
假设我在“消息”类上有一个“评论”属性。我还有 2 个类属性,它们具有“Body”属性。如果该类设置了任一类属性,我希望 AutoMapper 将 Body 属性投影到模型的 comment 属性中,
我是 AutoMapper 的新手,有一个我正在尝试解决的问题。 如果我有这样的源类: public class Membership { public int MembershipId {
我有一个场景,我想忽略基类中定义的类的某些属性。 我有一个这样的初始映射 Mapper.CreateMap() .Include()
如何覆盖 AutoMapper 用于给定属性的类型转换器? 例如,如果我有: public class Foo { public string Name { get; set; } } pub
我正在尝试使用 AutoMapper 将三个实体模型映射到一个 View 模型中。最终输出应该是一个递归类别树,其中包含类别中的产品。类别树正在运行,但 View 模型的 Products 属性为 n
我有一个 Student目的: public class Student { public int Id { get; set; } public string FirstName {
我有一个复杂的对象,如: public class BusinessUnit { public TradingDesk TradingDesk { get; } pub
假设我有这个类: public class Account { public int AccountID { get; set; } public Enterprise Enterpr
从概念上我发现 Automapper 非常有趣。然而,我正试图在上面烧伤(或加热)我的手指。有人可以帮我开始吗?我还不明白我可以从哪里开始。我会喜欢从头开始写一些代码(而不是使用其他人的样本)然后去做
我一直在尝试创建一个自动映射器自定义值解析器,但我似乎错过了一些设置步骤,因为它似乎永远找不到 public abstract class ValueResolver : IValueResolve
我正在尝试让 AutoMapper 为我们在 View 模型上本地化所有 DateTime 属性。我们在系统中的任何地方都使用 UTC 并将所有内容以 UTC 存储在数据库中,但我们希望自动将其转换为
我知道是 AutoMapper而不是 AutoMerge(r),而是…… 我已经开始使用 AutoMapper 并且需要映射 A -> B,并从 C 添加一些属性,以便 B 成为一种 A + C 的平
鉴于这些类,我如何映射它们的字典? public class TestClass { public string Name { get; set; } } public class TestC
我想自定义 AutoMapper 转换类型的方式,而不丢失 AutoMapper 已实现的功能。 我可以创建一个自定义 ITypeConverter实例,但我不知道如何调用默认行为。 Mapper.C
从存储过程返回的数据有 3 列重复数据: Name | Address | PhoneNumber | UniqueCol1 | UniqueCol2 理想情况下,我希望我的模型通过仅存储一次值并收集
当我使用 List 属性映射对象时,默认情况下 Automapper 会将目标对象的 list 属性设置为源对象的实例。 有没有办法让自动映射器创建一个新列表并复制项目但不复制列表实例? 我希望通过以
如果我有这组源类怎么办: namespace Source { class CA { public CB B { get; set; } } class
我是一名优秀的程序员,十分优秀!