使用自动映射器更改集合

时间:2018-11-17 06:21:30

标签: c# automapper icollection

我在C#中有两个具有一对多关系的模型:

public class Userinterface extends JPanel implements ActionListener{

    private static final long serialVersionUID = 1L;
    private static String str= "0";
    private static String str2= "0";
    private static String str3= "0";
    private JTextField angle= new JTextField(5);
    private JLabel alabel= new JLabel("Angle");
    private JTextField velocity= new JTextField(5);
    private JLabel vlabel= new JLabel("Velocity");
    private JButton Actionbutton= new JButton("launch");
    private JTextField time= new JTextField(5);
    private JLabel tlabel= new JLabel ("Time of fuse (s)");
    static int a;
    static int b;
    static int c;
    static int x;
    static int y;

    Userinterface(){
        setLayout(new FlowLayout(FlowLayout.CENTER));
        add(time);
        add(tlabel);
        add (angle);
        add(alabel);
        add (velocity);
        add(vlabel);
        add(Actionbutton);
        Actionbutton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==Actionbutton) {
        str=angle.getText();
        str2=velocity.getText();
        str3=time.getText();
        a=Integer.parseInt(str);
        b=Integer.parseInt(str2);
        c=Integer.parseInt(str3);
        repaint();  // ISSUE: Canvas does not repaint here
    }   
}

public static int findx() {
    x=Fire_WorkMath.calculatex(b, c, a);
    return x;
}

public static int findy() {
    y=Fire_WorkMath.calculatey(b, c, a);
    return y;
}

public class Canvas extends JPanel {

    Userinterface one= new Userinterface();
    int ang;
    int time;
    int y;
    int x;

    private static final long serialVersionUID = 1L;

    public Canvas() {

    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        x=Userinterface.findx();
        y=Userinterface.findy();
        g.drawLine(0, 0, x, y);
    }
}

和DTO如下:

public class Make
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Model> Models { get; set; }
}

public class Model
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Make Make { get; set; }
    public int MakeId { get; set; }
}

我正在使用Automapper在更新操作中映射DTO,问题是添加/删除集合工作正常,但是添加新项目更新现有项目却没有工作。

public class MakeDto
{
    public int Id { get; set; }
    public int ModelId { get; set; }
    public ICollection<Models> Models { get; set; }
}

1 个答案:

答案 0 :(得分:1)

为回答我的问题,并为将来的读者使用了Automapper.Collection,如下所示:

        var mapper = new MapperConfiguration(
            cfg =>
            {
                cfg.AddCollectionMappers();
                cfg.CreateMap<MakeDto, Make>()
                    .EqualityComparison((src, dst) => src.Id == dst.Id);
                cfg.CreateMap<Model, Model>()
                    .ForMember(x => x.Make, opt => opt.Ignore())
                    .EqualityComparison((src, dst) => src.Id == dst.Id);
            })
            .CreateMapper();
        mapper.Map<MakeDto, Make>(makeDto, make);
        await _unitOfWork.CompleteAsync();

也有兴趣的话,请参见此link