csharp AutoMapper EF项目的生产力

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp AutoMapper EF项目的生产力相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using AutoMapper.QueryableExtensions;

namespace ConsoleApplication1
{
    #region DTO

    public class DocflowDto
    {
        public bool IsOutbound { get; set; }

        public string RecipientGlobalId { get; set; }
        public string InitiatorGlobalId { get; set; }
    }

    public class DocflowDto2
    {
        public bool IsOutbound { get; set; }

    }

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

        public string Surname { get; set; }

        public string Patronymic { get; set; }
    }

    public class MemberDto
    {
        public string AbonentId { get; set; }
        public string Name { get; set; }
        public string Inn { get; set; }
        public string Kpp { get; set; }
        public BasePersonViewModel Director { get; set; }
        public string OperatorName { get; set; }
        public string OperatorInn { get; set; }
        public string OperatorPrefix { get; set; }
    }

    public class PackageDto
    {
        public MemberDto Receiver { get; set; }

        public MemberDto Sender { get; set; }

        public DocflowDto Docflow { get; set; }
        public string TransactionCode { get; set; }
        public Guid? DraftGuid { get; set; }
    }

    public class PackageDto2
    {

        public string TransactionCode { get; set; }
        public Guid? DraftGuid { get; set; }

        public DocflowDto Docflow { get; set; }
    }

    #endregion



    #region Entity

    internal class Package
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

        public Docflow Docflow { get; set; }
        public DocflowMember PackageSender { get; set; }
        public DocflowMember PackageReceiver { get; set; }
    }

    internal class DocflowMember
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

        public string GlobalId { get; set; }
        public Abonent Abonent { get; set; }
        public Operator Operator { get; set; }
    }

    internal class Person
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

        public string Name { get; set; }

        public string Surname { get; set; }

        public string Patronymic { get; set; }
    }

    internal class Abonent
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

        public string Inn { get; set; }
        public string Kpp { get; set; }
        public string ShortName { get; set; }
        public Person Director { get; set; }
    }

    internal class Operator
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

        public string Name { get; set; }
        public string Inn { get; set; }
        public string Prefix { get; set; }
    }

    internal class Docflow
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

        public bool IsOutbound { get; set; }

        public string RecipientGlobalId { get; set; }
        public string InitiatorGlobalId { get; set; }
    }

    #endregion






    class Program
    {
        public static void Main()
        {
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<Docflow, DocflowDto>();
                cfg.CreateMap<Person, BasePersonViewModel>();
                cfg.CreateMap<DocflowMember, MemberDto>()
                    .ForMember(dest => dest.AbonentId, opt => opt.MapFrom(src => src.GlobalId))
                    .ForMember(dest => dest.Inn, opt => opt.MapFrom(src => src.Abonent.Inn))
                    .ForMember(dest => dest.Kpp, opt => opt.MapFrom(src => src.Abonent.Kpp))
                    .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Abonent.ShortName))
                    .ForMember(dest => dest.Director, opt => opt.MapFrom(src => src.Abonent.Director))
                    .ForMember(dest => dest.OperatorName, opt => opt.MapFrom(src => src.Operator.Name))
                    .ForMember(dest => dest.OperatorInn, opt => opt.MapFrom(src => src.Operator.Inn))
                    .ForMember(dest => dest.OperatorPrefix, opt => opt.MapFrom(src => src.Operator.Prefix))
                    ;
                cfg.CreateMap<Package, PackageDto>()
                    .ForMember(dest => dest.Sender, opt => opt.MapFrom(src => src.PackageSender))
                    .ForMember(dest => dest.Receiver, opt => opt.MapFrom(src => src.PackageReceiver))
                    .ForMember(dest => dest.Docflow, opt => opt.MapFrom(src => src.Docflow))
                    ;

                cfg.CreateMap<Package, PackageDto2>()
                    .ForMember(dest => dest.Docflow, opt => opt.MapFrom(src => src.Docflow));

            });
            //Mapper.AssertConfigurationIsValid();
            var context = new ClientContext();
            //var result = context.Packages.ProjectTo<PackageDto2>().FirstOrDefault(); //.Dump();
            var result2 = context.Packages.AsNoTracking().ProjectTo<PackageDto>().FirstOrDefault(); //.Dump();
        }
    }













    class Initializer : DropCreateDatabaseAlways<ClientContext>
    {
        protected override void Seed(ClientContext context)
        {
            context.Packages.Add(new Package()
            {
                Docflow = new Docflow() {InitiatorGlobalId = "111", IsOutbound = true, RecipientGlobalId = "111"},
                PackageReceiver = new DocflowMember()
                {
                    Abonent = new Abonent()
                    {
                        Director = new Person() {Name = "asd"},
                        Inn = "asd",
                        Kpp = "asd",
                        ShortName = "ads"
                    },
                    GlobalId = "asd",
                    Operator = new Operator() {Inn = "asd", Name = "ads"}
                },
                PackageSender = new DocflowMember()
                {
                    Abonent = new Abonent()
                    {
                        Director = new Person() {Name = "222"},
                        Inn = "222",
                        Kpp = "222",
                        ShortName = "222"
                    },
                    GlobalId = "222",
                    Operator = new Operator() {Inn = "222", Name = "222"}
                },
            });
        }
    }

    class ClientContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            Database.Log = s => Debug.WriteLine(s);
            Database.SetInitializer(new Initializer());
        }

        public DbSet<Package> Packages { get; set; }
    }
}

以上是关于csharp AutoMapper EF项目的生产力的主要内容,如果未能解决你的问题,请参考以下文章

AutoMapper.EF6

AutoMapper + EF Core:查询嵌套属性等于值的位置

EF架构~对AutoMapper实体映射的扩展

csharp automapper配置

csharp automapper formember

csharp 在LINQ查询中使用AutoMapper