自动生成automapper的configure匹配规则(二)可视化工具

这篇博客介绍如何创建一个简单的WPF应用,作为Automapper的可视化配置工具。通过两个Listbox分别展示数据源和目标类型,第三个Listbox显示匹配结果。作者自定义了CustomData类来存储匹配信息,并提供了代码生成逻辑,最终能够生成Automapper的configure代码。尽管存在一些不足,如未处理类型统一和线程保护,作者期待得到改进意见并计划未来完善。
摘要由CSDN通过智能技术生成

上一篇中,我们写了一个生成复杂属性列表的方法:http://blog.csdn.net/d100000/article/details/49756229


【注:用的是.net  4.0版本,wpf】


在这一篇中,我们将建立一个可视化的匹配工具。

首先把布局说一下


布局比较简陋,一共有三个listbox,一个是是数据来源类型的list,一个是数据匹配目标的list,还有一个是匹配结果的list

为了操作方便,我构建了一个子自定义的数据类型,用于保存匹配完成的数据。

自定义的CustomData类:

using System.Linq;

namespace KDY.Common.ModelMapper.libs
{
    /// <summary>
    /// 自定义结构体类型
    /// classname[] 第一个保存的是来源类名,第二个保存的是目标类名
    /// dataString[] 第一个是来源属性名,第二个是目标属性名
    /// </summary>
    public class  CustomData  
    {
        private string[] _className;
        /// <summary>
        /// 转换属性所属类名
        /// </summary>
        public string[] ClassName
        {
            get { return _className; }
            set { _className = value; }
        }

        private string[] _dataStrings;
        /// <summary>
        /// 转换属性名
        /// </summary>
        public string[] DataStrings
        {
            get { return _dataStrings; }
            set { _dataStrings = value; }
        }

        /// <summary>
        /// 显示转换规则
        /// </summary>
        public string ShowString
        {
            get { return _dataStrings.Count() != 2 ? null : _dataStrings[0] + "->" + _dataStrings[1]; }
        }
    }
}

保存了转换属性的类名,属性名,以及属性匹配的可视化结果。

可视化结果就是一二string;即如果如图:【下图测试数据来源和目标相同】



布局文件,由于新手,也不想改了,所以没有按照规范设置分区,直接都是拖控件的。

<Window x:Class="KDY.Common.ModelMapper.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="502.8" Width="741"
        ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
    <Grid>
        <Label Content=" 匹配名称:" HorizontalAlignment="Left" Margin="491,9,0,0" VerticalAlignment="Top" Width="170" Foreground="#FF7A7A7A"/>
        <Label Content=" 数据来源" HorizontalAlignment="Left" Margin="10,38,0,0" VerticalAlignment="Top" Width="170" Foreground="#FF7A7A7A"/>
        <Label Content="赋值目标" HorizontalAlignment="Left" Margin="199,38,0,0" VerticalAlignment="Top" Width="170" Foreground="#FF7A7A7A"/>
        <Label Content="匹配规则" HorizontalAlignment="Left" Margin="495,38,0,0" VerticalAlignment="Top" Width="170" Foreground="#FF7A7A7A"/>
        <ListBox x:Name="LbFromTypeList" HorizontalAlignment="Left" Height="400" Margin="10,64,0,0" VerticalAlignment="Top" Width="170"/>
        <ComboBox x:Name="CbSelectFrom" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="170" Height="26" SelectionChanged="CbFromChange"/>
        <ListBox x:Name="LbToTypeListTo" HorizontalAlignment="Left" Height="400" Margin="199,64,0,0" VerticalAlignment="Top" Width="170"  />
        <Button Content=">>>>" HorizontalAlignment="Left" Margin="398,163,0,0" VerticalAlignment="Top" Width="75" Click="BtnAdd_Click"/>
        <ListBox x:Name="LbSouceList" HorizontalAlignment="Left" Height="400" Margin="495,64,0,0" VerticalAlignment="Top" Width="229" />
        <Label x:Name="LabFromCount" Content="" HorizontalAlignment="Left" Margin="80,38,0,0" VerticalAlignment="Top"/>
        <Label x:Name="LabToCount" Content="" HorizontalAlignment="Left" Margin="262,38,0,0" VerticalAlignment="Top"/>
        <Label x:Name="LabSouceCount" Content="" HorizontalAlignment="Left" Margin="561,38,0,0" VerticalAlignment="Top"/>
        <Button Content="Delete" HorizontalAlignment="Left" Margin="398,212,0,0" VerticalAlignment="Top" Width="75" Click="BtnDelete_Click"/>
        <ComboBox x:Name="CbSelectTo" HorizontalAlignment="Left" Margin="199,10,0,0" VerticalAlignment="Top" Width="170" Height="26" SelectionChanged="CbToChange"/>
        <Button Content="生成匹配文件" HorizontalAlignment="Left" Margin="398,10,0,0" VerticalAlignment="Top" Width="89" Height="26" Click="BtnCreate_Click"/>
        <TextBox x:Name="TextBoxName" HorizontalAlignment="Left" Height="23" Margin="561,10,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="163"/>
        <
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
常用的代码自动生成工具有很多,比如 MyBatis Generator、Lombok、AutoValue、AutoMapper、Swagger Codegen 等等。这里以 MyBatis Generator 为例,介绍其安装和使用方法。 1. 安装 MyBatis Generator MyBatis Generator 是一个基于 Maven 的插件,可以在命令行或者 Eclipse、IntelliJ IDEA 等 IDE 中使用。下面介绍如何在 Maven 项目中使用 MyBatis Generator。 在项目的 pom.xml 文件中添加以下依赖: ``` <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.4.0</version> </dependency> </dependencies> ``` 2. 配置 MyBatis Generator 在 Maven 项目中,需要在 pom.xml 文件中配置 MyBatis Generator 插件。例如: ``` <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <configuration> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> </dependencies> </plugin> </plugins> </build> ``` 其中,generatorConfig.xml 是 MyBatis Generator 的配置文件,可以根据实际情况进行修改。 3. 使用 MyBatis Generator 在 Maven 项目中,可以使用以下命令生成代码: ``` mvn mybatis-generator:generate ``` 执行该命令后,MyBatis Generator 会根据配置文件自动生成代码。 以上是 MyBatis Generator 的基本安装和使用方法。其他代码自动生成工具的安装和使用方式也类似,只需要根据其官方文档进行配置即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值