标签: automapper-2

如何在构造函数中使用AutoMapper映射到"this"

我有一个源类型,它具有属性和目标类型,具有完全相同的属性.

为AutoMapper配置一个简单的映射后,如:

Mapper.CreateMap<MySourceType, MyDestinationType>();
Run Code Online (Sandbox Code Playgroud)

我想有一个MyDestinationType的构造函数,它有一个MySourceType参数,然后自动初始化创建类型的属性,源代码如下:

public MyDestinationType(MySourceType source)
{
    // Now here I am do not know what to write.
}
Run Code Online (Sandbox Code Playgroud)

我找到的唯一解决方法是为其创建一个静态工厂方法

public static MyDestinationType Create(MySourceType source)
{
     return Mapper.Map<MyDestinationType>(source);
}
Run Code Online (Sandbox Code Playgroud)

有没有办法不让这种静态的丑陋?

c# automapper automapper-2 automapper-3

6
推荐指数
1
解决办法
1622
查看次数

使用密钥将列表映射到Automapper中的现有列表

Automapper可以轻松处理将一个对象类型列表映射到另一个不同对象类型的列表,但是是否可以使用ID作为键将其映射到现有列表?

c# automapper automapper-2

5
推荐指数
1
解决办法
2691
查看次数

AutoMapper不使用派生类的集合

在处理包含派生类集合的对象时,我遇到了AutoMapper 2.1.267.0的问题.我在一个更简单的场景中使用以下类隔离了我的问题:

public class ClassABase
{
    public string Name { get; set; }
}

public class ClassA : ClassABase
{
    public string Description { get; set; }
}

public class ClassBBase
{
    public string Title { get; set; }
}

public class ClassB : ClassBBase
{
    public string Text { get; set; }
}

public class ContainerA
{
    public IList<ClassA> ClassAList { get; set; }
    public ClassA ClassA { get; set; }
}

public class ContainerB
{
    public IList<ClassB> ClassBList …
Run Code Online (Sandbox Code Playgroud)

c# automapper automapper-2

5
推荐指数
1
解决办法
1081
查看次数

带有ValueFormatter的AutoMapper

我正在学习如何使用AutoMapper,我在使用ValueFormatter时遇到了问题.

这是Console中的简单示例,我无法将其与NameFormatter一起使用:

class Program
{
    static void Main(string[] args)
    {
        Mapper.Initialize(x => x.AddProfile<ExampleProfile>());

        var person = new Person {FirstName = "John", LastName = "Smith"};

        PersonView oV = Mapper.Map<Person, PersonView>(person);

        Console.WriteLine(oV.Name);

        Console.ReadLine();
    }
}

public class ExampleProfile : Profile
{
    protected override void Configure()
    {
        //works:
        //CreateMap<Person, PersonView>()
        //    .ForMember(personView => personView.Name, ex => ex.MapFrom(
        //        person => person.FirstName + " " + person.LastName));

        //doesn't work:
        CreateMap<Person, PersonView>()
            .ForMember(personView => personView.Name, 
             person => person.AddFormatter<NameFormatter>());
    }
}

public class NameFormatter : ValueFormatter<Person> …
Run Code Online (Sandbox Code Playgroud)

c# automapper automapper-2

5
推荐指数
1
解决办法
2289
查看次数

Automapper将一个属性映射到多个

我在源对象和目标对象之间遇到了AutoMapper的挑战.我会尝试解释这种情况.在我的src对象上,我有一个字符串,根据它的长度,它应该映射到我的目标对象的多个属性.

class source
{
   public int Id {get; set;}
   /* some other properties */
   public string Value {get; set;}
}

class destination
{
   public int Id {get; set;}
   /* some other properties with the same name as the source */
   public string Value1 {get; set;}
   public string Value2 {get; set;}
   public string Value3 {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

预期的最大长度为30个字符(可能小于仅映射到两个属性或一个属性的字符).因此,每10个将映射到每个目标属性.我试图使用AutoMapper中的ResolveUsing方法,但是没有办法让函数知道我应该带回哪个段.所以我想忽略这些属性的映射,并在Automapper完成其他属性的工作后手动执行此操作

c# visual-studio-2010 automapper automapper-2 automapper-3

5
推荐指数
2
解决办法
5627
查看次数

自动发现automapper配置

在MVC中创建控制器时,您无需为其进行任何其他注册.添加区域也是如此.只要您的global.asax具有AreaRegistration.RegisterAllAreas()调用,就不需要进行其他设置.

使用AutoMapper,我们必须使用某种CreateMap<TSource, TDestination>调用来注册映射.可以使用static显式地执行这些操作Mapper.CreateMap,或者通过从AutoMapper.Profile类派生,重写Configure方法,然后CreateMap从那里调用.

在我看来,应该能够扫描一个程序集,从Profile类似MVC扫描扩展的类扩展Controller.使用这种机制,不应该仅通过创建派生自的类来创建映射Profile吗?是否存在任何此类库工具,或者是否存在内置于automapper中的内容?

reflection asp.net-mvc auto-registration automapper automapper-2

4
推荐指数
1
解决办法
2180
查看次数

从源到目标的自动映射映射,包含对象列表

我必须用automapper创建一个Mapping.

Public class Source
{
    public string Id;
    public string Firstname;
    public string Lastname;
}
Run Code Online (Sandbox Code Playgroud)

目的地是

Public class Destination
{
    public string Id;
    public Person[] persons;
}
Run Code Online (Sandbox Code Playgroud)

人类是

Public class Person
{
    public string FirstName;
    public string LastName;
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试创建映射

AutoMapper.Mapper.CreateMap<Source, Destination>(); 
Run Code Online (Sandbox Code Playgroud)

但我不知道如何将Firstname,Lastname映射到对象Person的数组.

automapping automapper automapper-2 automapper-3

4
推荐指数
1
解决办法
1782
查看次数

AutoMapper - 为什么要覆盖整个对象?

我不明白为什么它会覆盖我的整个对象.原因是我User从db 获取对象,我想从DTO分配新值.它不是仅仅添加这些新值,而是创建具有新值但前面所有值都设置为的新对象null.

我怎样才能确保在这种情况下他会"升级"我的对象,而不是创建新对象?

脚本

/users/{id} - PUT

// User has id, username, fullname
// UserPut has fullname
public HttpResponseMessage Put(int id, UserPut userPut)
{
    var user = _db.Users.SingleOrDefault(x => x.Id == id); // filled with properties

    Mapper.CreateMap<UserPut, User>();
    user = Mapper.Map<User>(userPut); // now it has only "fullname", everything else set to null

    // I can't save it to db because everything is set to null except "fullname"

    return Request.CreateResponse(HttpStatusCode.OK, user);
}
Run Code Online (Sandbox Code Playgroud)

c# dto automapping automapper automapper-2

3
推荐指数
1
解决办法
2224
查看次数