Base solution for your next web application
Open Closed

AutoMap, AutoMapTo, AutoMapFrom Why, Where When and How #609


User avatar
0
sayram created

As become a beginner i can use Abp but i want to use full functional, write better code and understand things how they work. Documentations not for beginners and i have a lot of questions that confuses me.

So can anybody please explain Why, Where, When, How i have to use AutoMap, AutoMapTo, AutoMapFrom?

And i need real sample about Permissions and settings usage. Change, Create, Update from control panel. How you guys done these?

Thank you.


3 Answer(s)
  • User Avatar
    0
    sampath created

    Hi, A 1 :

    ASP.NET Boilerplate provides several attributes and extension methods to define mappings. To use it, add Abp.AutoMapper nuget package to your project. Then, use attribute AutoMap for two way mapping, AutoMapFrom and AutoMapTo for one way mapping. Use MapTo extension methods to map one object to another. Example mapping definition:

    Here is the Link : [http://www.aspnetboilerplate.com/Pages/Documents/Data-Transfer-Objects#DocAutoMapping])

    A 2 : Here is the Articles and Tutorials section (first section) : [http://www.aspnetboilerplate.com/Pages/Documents])

    Hope this will help to you. Good Luck ! :)

  • User Avatar
    0
    sayram created

    <cite>sampath: </cite> Hi, A 1 :

    ASP.NET Boilerplate provides several attributes and extension methods to define mappings. To use it, add Abp.AutoMapper nuget package to your project. Then, use attribute AutoMap for two way mapping, AutoMapFrom and AutoMapTo for one way mapping. Use MapTo extension methods to map one object to another. Example mapping definition:

    Hope this will help to you. Good Luck ! :)

    Let say we have a category entity like below.

    public class Category : Entity<int>
        {
            public virtual int? ParentId { get; set; }
            public virtual string Title { get; set; }
            public virtual string Description { get; set; }
            public virtual Category ParentCategory { get; set; }
            public virtual ICollection<Category> SubCategories { get; set; }
            public virtual ContentState ContentState { get; set; }
        }
    

    IInputDto

    public class CategorySummaryInput : IInputDto
        {
            public ContentState ContentState { get; set; }
        }
    

    IOutputDto

    public class CategorySummaryOutput : IOutputDto
        {
            public IList<CategorySummaryDto> Categories { get; set; }
        }
    

    CategorySummaryDto

    [AutoMap(typeof(Category))]
        public class CategorySummaryDto : EntityDto
        {
            public int? ParentId { get; set; }
            public string Title { get; set; }
            public string Description { get; set; }
            public IList<CategorySummaryDto> SubCategories { get; set; }
        }
    

    In Admin/Categories/Index view, I'll be needed sub categories of current category.

    How can i map

    public virtual ICollection<Category> SubCategories { get; set; }
    

    to

    public IList<CategorySummaryDto> SubCategories { get; set; }
    
  • User Avatar
    0
    hikalkan created
    Support Team

    This should work:

    ICollection<Category> categories = ...get from somewhere
    var categoryDtos = categories.MapTo<List<CategorySummaryDto>>(); //the mapping code
    

    ABP has no mapping system, just simple helper attributes and an extension method (MapTo). It simply use AutoMapper. And AutoMapper has a wide documentation (<a class="postlink" href="https://github.com/AutoMapper/AutoMapper/wiki/Getting-started">https://github.com/AutoMapper/AutoMappe ... ng-started</a>). So, you can read it's documentation to learn mapping details and use cases. (That's why we did not cover mapping in the documents much.)