public static void Init()
        {
            Mapper.Reset();

            Mapper.AddMap<Product, ProductViewModel>(src =>
            {
                var productViewModel = new ProductViewModel();
                productViewModel.InjectFrom(src);
                productViewModel.DefaultSharedOption =
                    Mapper.Map<ProductVariant, ProductVariantViewModel>(src.DefaultOption);
                productViewModel.Options = new List<ProductVariantViewModel>();
                foreach (var pv in src.Options)
                {
                    productViewModel.Options.Add(Mapper.Map<ProductVariant, ProductVariantViewModel>(pv));
                }
                return productViewModel;
            });

            Mapper.AddMap<Test, TestViewModel>(src =>
            {
                var testViewModel = new TestViewModel(string.Format("{0} - {1}", src.Name, src.Id));
                testViewModel.InjectFrom(src);

                testViewModel.Name = string.Format("{0} - {1} - {2}", src.Name, src.Weight, src.Age);

                testViewModel.Product = Mapper.Map<Product, ProductViewModel>(src.Product);
                testViewModel.SpareTheProduct = Mapper.Map<Product, ProductViewModel>(src.SpareProduct);
                testViewModel.Type = (Types) src.Type;
                testViewModel.Weight = src.Weight*2;
                testViewModel.Products = new List<ProductViewModel>();
                foreach (var product in src.Products)
                {
                    testViewModel.Products.Add(Mapper.Map<Product, ProductViewModel>(product));
                }
                return testViewModel;
            });

            Mapper.AddMap<User, UserViewModel>(src =>
            {
                var userViewModel = new UserViewModel();
                userViewModel.InjectFrom(src);
                userViewModel.BelongTo = Mapper.Map<Role, RoleViewModel>(src.Role);
                return userViewModel;
            });

            Mapper.AddMap<Author, AuthorViewModel>(src =>
            {
                var articles = new List<ArticleViewModel>(src.Articles.Count());
                var authorViewModel = new AuthorViewModel();
                authorViewModel.InjectFrom(src);

                for (var i = 0; i < articles.Count; i++)
                {
                    articles[i] = Mapper.Map<Article, ArticleViewModel>(src.Articles.ElementAt(i));
                }
                authorViewModel.OwnedArticles = articles;
                return authorViewModel;
            });
        }
Пример #2
0
        public static TestViewModel Map(Test src)
        {
            if (src == null)
            {
                return default(TestViewModel);
            }
            var dst = new TestViewModel(string.Format("{0} - {1}", src.Name, src.Id));
            dst.Id = src.Id;
            dst.Created = src.Created;
            dst.Age = src.Age;
            dst.Name = string.Format("{0} - {1} - {2}", src.Name, src.Weight, src.Age);
            dst.Type = (Types)src.Type;
            dst.Weight = src.Weight;

            dst.Product = Map(src.Product);
            dst.SpareTheProduct = Map(src.SpareProduct);
            dst.Products = src.Products.Select(Map).ToList();
            return dst;
        }