├── packages.config ├── AuthorModel.cs ├── AuthorEntity.cs ├── BookModel.cs ├── BookEntity.cs ├── EjemploAutoMapper.sln ├── Properties └── AssemblyInfo.cs ├── EjemploAutoMapper.csproj ├── Program.cs └── .gitignore /packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /AuthorModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace EjemploAutoMapper 4 | { 5 | public class AuthorModel 6 | { 7 | public string Name { get; set; } 8 | public int Age { get; set; } 9 | public int BooksCount { get; set; } 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /AuthorEntity.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace EjemploAutoMapper 5 | { 6 | public class AuthorEntity 7 | { 8 | public int Id { get; set; } 9 | public string Name { get; set; } 10 | public int Age { get; set; } 11 | public List Books { get; set; } 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /BookModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace EjemploAutoMapper 4 | { 5 | public class BookModel 6 | { 7 | public string FullTitle { get; set; } 8 | public int Pages { get; set; } 9 | public int Edition { get; set; } 10 | public string AuthorName { get; set; } 11 | public DateTime TimeSent { get; set; } 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /BookEntity.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace EjemploAutoMapper 4 | { 5 | public class BookEntity 6 | { 7 | public int Id { get; set; } 8 | public string Title { get; set; } 9 | public string Subtitle { get; set; } 10 | public int Pages { get; set; } 11 | public int Edition { get; set; } 12 | public AuthorEntity Author { get; set; } 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /EjemploAutoMapper.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2012 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EjemploAutoMapper", "EjemploAutoMapper.csproj", "{075FA301-C2BB-408B-B1ED-444C230335F2}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|x86 = Debug|x86 9 | Release|x86 = Release|x86 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {075FA301-C2BB-408B-B1ED-444C230335F2}.Debug|x86.ActiveCfg = Debug|x86 13 | {075FA301-C2BB-408B-B1ED-444C230335F2}.Debug|x86.Build.0 = Debug|x86 14 | {075FA301-C2BB-408B-B1ED-444C230335F2}.Release|x86.ActiveCfg = Release|x86 15 | {075FA301-C2BB-408B-B1ED-444C230335F2}.Release|x86.Build.0 = Release|x86 16 | EndGlobalSection 17 | EndGlobal 18 | -------------------------------------------------------------------------------- /Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | 4 | // Information about this assembly is defined by the following attributes. 5 | // Change them to the values specific to your project. 6 | 7 | [assembly: AssemblyTitle("EjemploAutoMapper")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("")] 12 | [assembly: AssemblyCopyright("fferegrino")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". 17 | // The form "{Major}.{Minor}.*" will automatically update the build and revision, 18 | // and "{Major}.{Minor}.{Build}.*" will update just the revision. 19 | 20 | [assembly: AssemblyVersion("1.0.*")] 21 | 22 | // The following attributes are used to specify the signing key for the assembly, 23 | // if desired. See the Mono documentation for more information about signing. 24 | 25 | //[assembly: AssemblyDelaySign(false)] 26 | //[assembly: AssemblyKeyFile("")] 27 | 28 | -------------------------------------------------------------------------------- /EjemploAutoMapper.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | x86 6 | {075FA301-C2BB-408B-B1ED-444C230335F2} 7 | Exe 8 | EjemploAutoMapper 9 | EjemploAutoMapper 10 | v4.5 11 | 12 | 13 | true 14 | full 15 | false 16 | bin\Debug 17 | DEBUG; 18 | prompt 19 | 4 20 | true 21 | x86 22 | 23 | 24 | full 25 | true 26 | bin\Release 27 | prompt 28 | 4 29 | true 30 | x86 31 | 32 | 33 | 34 | 35 | packages\AutoMapper.4.2.1\lib\net45\AutoMapper.dll 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AutoMapper; 3 | using System.Collections.Generic; 4 | 5 | namespace EjemploAutoMapper 6 | { 7 | class MainClass 8 | { 9 | public static void Main(string[] args) 10 | { 11 | var mapper = CreateMapper(); 12 | var entidadAutor = new AuthorEntity 13 | { 14 | Id = 2, 15 | Name = "Joanne Rowling", 16 | Age = 50, 17 | Books = new List() 18 | { 19 | new BookEntity { Id = 4, Title = "Harry Potter", Subtitle = "y la piedra filosofal" }, 20 | new BookEntity { Id = 3, Title = "Harry Potter", Subtitle = "y la cámara secreta" }, 21 | new BookEntity { Id = 9, Title = "Una vacante imprevista" } 22 | } 23 | }; 24 | 25 | #region Manual mapping 26 | 27 | #endregion 28 | 29 | var bookEntity = new BookEntity 30 | { 31 | Id = 30, 32 | Title = "C# 6 in a Nutshell", 33 | Edition = 6, 34 | Pages = 1114, 35 | Author = new AuthorEntity 36 | { 37 | Name = "Joseph Albahari", 38 | Age = 35 39 | } 40 | }; 41 | 42 | #region Manual mapping 43 | //var modeloAutor = new AuthorModel(); 44 | //modeloAutor.Name = entidadAutor.Name; 45 | //modeloAutor.Age = entidadAutor.Age; 46 | //modeloAutor.BooksCount = entidadAutor.Books != null ? entidadAutor.Books.Count : 0; 47 | 48 | //var modeloLibro = new BookModel(); 49 | //modeloLibro.FullTitle = bookEntity.Title + " " + bookEntity.Subtitle; 50 | //modeloLibro.AuthorName = bookEntity.Author != null ? bookEntity.Author.Name : null; 51 | //modeloLibro.Pages = bookEntity.Pages; 52 | #endregion 53 | 54 | var modeloAutor = mapper.Map(entidadAutor); 55 | Console.WriteLine("Información de autor"); 56 | Console.WriteLine("Autor: " + modeloAutor.Name); 57 | Console.WriteLine("Edad: " + modeloAutor.Age); 58 | Console.WriteLine("Libros: " + modeloAutor.BooksCount); 59 | 60 | var modeloLibro = mapper.Map(bookEntity); 61 | Console.WriteLine("Información de libro"); 62 | Console.WriteLine("Título: " + modeloLibro.FullTitle); 63 | Console.WriteLine("Autor: " + modeloLibro.AuthorName); 64 | Console.WriteLine("Páginas: " + modeloLibro.Pages); 65 | 66 | 67 | 68 | } 69 | 70 | static IMapper CreateMapper() 71 | { 72 | var automappingConfiguration = new AutoMapper.MapperConfiguration(config => 73 | { 74 | config.CreateMap(); 75 | 76 | config.CreateMap() 77 | .ForMember(model => model.AuthorName, 78 | opt => opt.ResolveUsing(entity => entity.Author.Name)) 79 | .ForMember(model => model.FullTitle, 80 | opt => opt.ResolveUsing(entity => entity.Title + " " + entity.Subtitle)); 81 | }); 82 | 83 | return automappingConfiguration.CreateMapper(); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | 24 | # Visual Studio 2015 cache/options directory 25 | .vs/ 26 | # Uncomment if you have tasks that create the project's static files in wwwroot 27 | #wwwroot/ 28 | 29 | # MSTest test Results 30 | [Tt]est[Rr]esult*/ 31 | [Bb]uild[Ll]og.* 32 | 33 | # NUNIT 34 | *.VisualState.xml 35 | TestResult.xml 36 | 37 | # Build Results of an ATL Project 38 | [Dd]ebugPS/ 39 | [Rr]eleasePS/ 40 | dlldata.c 41 | 42 | # DNX 43 | project.lock.json 44 | artifacts/ 45 | 46 | *_i.c 47 | *_p.c 48 | *_i.h 49 | *.ilk 50 | *.meta 51 | *.obj 52 | *.pch 53 | *.pdb 54 | *.pgc 55 | *.pgd 56 | *.rsp 57 | *.sbr 58 | *.tlb 59 | *.tli 60 | *.tlh 61 | *.tmp 62 | *.tmp_proj 63 | *.log 64 | *.vspscc 65 | *.vssscc 66 | .builds 67 | *.pidb 68 | *.svclog 69 | *.scc 70 | 71 | # Chutzpah Test files 72 | _Chutzpah* 73 | 74 | # Visual C++ cache files 75 | ipch/ 76 | *.aps 77 | *.ncb 78 | *.opendb 79 | *.opensdf 80 | *.sdf 81 | *.cachefile 82 | 83 | # Visual Studio profiler 84 | *.psess 85 | *.vsp 86 | *.vspx 87 | *.sap 88 | 89 | # TFS 2012 Local Workspace 90 | $tf/ 91 | 92 | # Guidance Automation Toolkit 93 | *.gpState 94 | 95 | # ReSharper is a .NET coding add-in 96 | _ReSharper*/ 97 | *.[Rr]e[Ss]harper 98 | *.DotSettings.user 99 | 100 | # JustCode is a .NET coding add-in 101 | .JustCode 102 | 103 | # TeamCity is a build add-in 104 | _TeamCity* 105 | 106 | # DotCover is a Code Coverage Tool 107 | *.dotCover 108 | 109 | # NCrunch 110 | _NCrunch_* 111 | .*crunch*.local.xml 112 | nCrunchTemp_* 113 | 114 | # MightyMoose 115 | *.mm.* 116 | AutoTest.Net/ 117 | 118 | # Web workbench (sass) 119 | .sass-cache/ 120 | 121 | # Installshield output folder 122 | [Ee]xpress/ 123 | 124 | # DocProject is a documentation generator add-in 125 | DocProject/buildhelp/ 126 | DocProject/Help/*.HxT 127 | DocProject/Help/*.HxC 128 | DocProject/Help/*.hhc 129 | DocProject/Help/*.hhk 130 | DocProject/Help/*.hhp 131 | DocProject/Help/Html2 132 | DocProject/Help/html 133 | 134 | # Click-Once directory 135 | publish/ 136 | 137 | # Publish Web Output 138 | *.[Pp]ublish.xml 139 | *.azurePubxml 140 | # TODO: Comment the next line if you want to checkin your web deploy settings 141 | # but database connection strings (with potential passwords) will be unencrypted 142 | *.pubxml 143 | *.publishproj 144 | 145 | # NuGet Packages 146 | *.nupkg 147 | # The packages folder can be ignored because of Package Restore 148 | **/packages/* 149 | # except build/, which is used as an MSBuild target. 150 | !**/packages/build/ 151 | # Uncomment if necessary however generally it will be regenerated when needed 152 | #!**/packages/repositories.config 153 | # NuGet v3's project.json files produces more ignoreable files 154 | *.nuget.props 155 | *.nuget.targets 156 | 157 | # Microsoft Azure Build Output 158 | csx/ 159 | *.build.csdef 160 | 161 | # Microsoft Azure Emulator 162 | ecf/ 163 | rcf/ 164 | 165 | # Microsoft Azure ApplicationInsights config file 166 | ApplicationInsights.config 167 | 168 | # Windows Store app package directory 169 | AppPackages/ 170 | BundleArtifacts/ 171 | 172 | # Visual Studio cache files 173 | # files ending in .cache can be ignored 174 | *.[Cc]ache 175 | # but keep track of directories ending in .cache 176 | !*.[Cc]ache/ 177 | 178 | # Others 179 | ClientBin/ 180 | ~$* 181 | *~ 182 | *.dbmdl 183 | *.dbproj.schemaview 184 | *.pfx 185 | *.publishsettings 186 | node_modules/ 187 | orleans.codegen.cs 188 | 189 | # RIA/Silverlight projects 190 | Generated_Code/ 191 | 192 | # Backup & report files from converting an old project file 193 | # to a newer Visual Studio version. Backup files are not needed, 194 | # because we have git ;-) 195 | _UpgradeReport_Files/ 196 | Backup*/ 197 | UpgradeLog*.XML 198 | UpgradeLog*.htm 199 | 200 | # SQL Server files 201 | *.mdf 202 | *.ldf 203 | 204 | # Business Intelligence projects 205 | *.rdl.data 206 | *.bim.layout 207 | *.bim_*.settings 208 | 209 | # Microsoft Fakes 210 | FakesAssemblies/ 211 | 212 | # GhostDoc plugin setting file 213 | *.GhostDoc.xml 214 | 215 | # Node.js Tools for Visual Studio 216 | .ntvs_analysis.dat 217 | 218 | # Visual Studio 6 build log 219 | *.plg 220 | 221 | # Visual Studio 6 workspace options file 222 | *.opt 223 | 224 | # Visual Studio LightSwitch build output 225 | **/*.HTMLClient/GeneratedArtifacts 226 | **/*.DesktopClient/GeneratedArtifacts 227 | **/*.DesktopClient/ModelManifest.xml 228 | **/*.Server/GeneratedArtifacts 229 | **/*.Server/ModelManifest.xml 230 | _Pvt_Extensions 231 | 232 | # Paket dependency manager 233 | .paket/paket.exe 234 | 235 | # FAKE - F# Make 236 | .fake/ 237 | --------------------------------------------------------------------------------