Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

عدم پشتیبانی کانفیگ کننده اتوماتیک از کلید اصلی چند گانه #353

Open
sadrazkh opened this issue Oct 30, 2021 · 0 comments

Comments

@sadrazkh
Copy link

زمانی که جدولی مانند جداول واسطه از چند کلید اصلی استفاده میکنند از
پیش فرض نمیشه استفاده کرد چرا که بخش سوم صرفا یک کلید را قبول میکند BaseDto
و اجازه نمیدهد ما از کانفیگ خودکار اتومپر استفاده کنیم

public class StudentHomeWorks : IEntity
{
    public int StudentId { get; set; }
    public long HomeWorkId { get; set; }
    public StudentHomeWorkStatus Status { get; set; }
    public Student Student { get; set; }
    public HomeWork HomeWork { get; set; }
}

public class StudentHomeWorksConfiguration : IEntityTypeConfiguration<StudentHomeWorks>
{
    public void Configure(EntityTypeBuilder<StudentHomeWorks> builder)
    {
        builder.HasKey(s => new { s.StudentId, s.HomeWorkId });
        builder.HasIndex(c => c.StudentId);
        builder.HasIndex(c => c.HomeWorkId);
        #region Relations
        builder.HasOne(s => s.Student)
            .WithMany(s => s.HomeWorks)
            .HasForeignKey(s => s.StudentId);
        builder.HasOne(s => s.HomeWork)
            .WithMany(e => e.Students)
            .HasForeignKey(s => s.HomeWorkId);
        #endregion
    }
}

``

public abstract class BaseDto<TDto, TEntity, TKey> : IHaveCustomMapping
    where TDto : class, new()
    where TEntity : class, IEntity<TKey>, new()
{
    [Display(Name = "ردیف")]
    public TKey Id { get; set; }

    public TEntity ToEntity(IMapper mapper)
    {
        return mapper.Map<TEntity>(CastToDerivedClass(mapper, this));
    }

    public TEntity ToEntity(IMapper mapper, TEntity entity)
    {
        return mapper.Map(CastToDerivedClass(mapper, this), entity);
    }

    public static TDto FromEntity(IMapper mapper, TEntity model)
    {
        return mapper.Map<TDto>(model);
    }

    protected TDto CastToDerivedClass(IMapper mapper, BaseDto<TDto, TEntity, TKey> baseInstance)
    {
        return mapper.Map<TDto>(baseInstance);
    }

    public void CreateMappings(Profile profile)
    {
        var mappingExpression = profile.CreateMap<TDto, TEntity>();

        var dtoType = typeof(TDto);
        var entityType = typeof(TEntity);
        //Ignore any property of source (like Post.Author) that dose not contains in destination 
        foreach (var property in entityType.GetProperties())
        {
            if (dtoType.GetProperty(property.Name) == null)
                mappingExpression.ForMember(property.Name, opt => opt.Ignore());
        }

        CustomMappings(mappingExpression.ReverseMap());
    }

    public virtual void CustomMappings(IMappingExpression<TEntity, TDto> mapping)
    {
    }
}

و مساله اینجاست که زمانی که دی تی او رو میسازیم چگونه کانفیگ کینم

public class ViewAllQuestionOfStudentDto : BaseDto<ViewAllQuestionOfStudentDto, StudentHomeWorks, ???>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant