How to use automapper

How to use automapper

Install it using Nuget



Classes


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace UsingAutoMapper.Models
{
    public class Scope
    {
        public int ScopeId { get; set; }
        public string ScopeName { get; set; }
        public string ScopeDisplay1 { get; set; }

        public List<TScopeType> ScopeTypes1 { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace UsingAutoMapper.Models
{
    public class ScopeViewModel
    {
        public int ScopeId { get; set; }
        public string ScopeName { get; set; }
        /// <summary>
        /// Id + Name
        /// </summary>
        public string ScopeDisplay { get; set; }
        public List<TScopeTypeViewModel> ScopeTypes { get; set; }
    }
}

Whole code

        private void UseAutoMapper()
        {
            ScopeViewModel model = new ScopeViewModel()
            {
                ScopeId = 1,
                ScopeName = "ScopeOne",
            };
          //  model.ScopeDisplay = model.ScopeId + " - " + model.ScopeName;

            Scope scope = new Scope();
            // model = scope;
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<ScopeViewModel, Scope>();
            });
            var sourceWithMapper = Mapper.Map<Scope>(model);

        }

Share on Google Plus

About myzingonline

Myzingonline is all about zing to share experience, knowledge, likes, dislikes and ideas of a person to the world.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment