Solved

Mapping Data contract class and Model class in MVC

Posted on 2014-03-07
4
1,019 Views
Last Modified: 2014-10-29
Hi Experts

I am using AngularJS on the client side and ASP.net MVC, Web API on the  server side.We are using Repository class to get data from wcf service and pass this data to API controller like below and this is working fine without any issue.

//Controller

[HttpGet]

 public class ItemsController : ApiController

    {
      ItemRepository rep=new ItemRepository();//Instantiating Item Repository class

        public IEnumerable<Item> GetAllItems() //Item is Data contract class
        
	{
            return rep.GetItems();
        }

       
    }

Open in new window


//Repository

Public class ItemRepository

{

public IEnumerable<Item> GetItems()  // Item is a Data contract class
    {
        return _repo.value.Items;
    }

//Some logic here for ItemService contract which will implement using ChannelFactory

}

Open in new window


But  I was told to create a Item Model class and Map Item Datacontract class members to Item Model class properties and return that  data (Model)  to API controller. How to achieve  this and need sample code for the mapper and API Controller .

Below is my Model class.

// Model 

Public class ItemModel

{
        public int ItemId { get; set; }
        public string ItemName { get; set; }
        public string ItemDescription { get; set; }
        public string ItemCategory { get; set; }
}

Open in new window

0
Comment
Question by:ksd123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
SriVaddadi earned 500 total points
ID: 39914219
There are libraries to map the objects like automapper. http://automapper.org/
0
 

Author Comment

by:ksd123
ID: 39915773
We are not suppose to use automapper.
0
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 39915779
Is it just automapper or any such tools?  You could use reflection to map objects.
0
 

Author Comment

by:ksd123
ID: 39916522
Right now I have a repository class that will fetch the data from the service and API controller will retrieve this data  and send it to the client and this is working fine without any issue.(Please check my original question with repository and controller methods)

Now because of new requirement I have to customize the data I get from the service and return back to API controller. To do this I need to have mapping method like below and do some customization and assign data contract members to Model properties and send it to API controller .I don't know how to implement this in C#

1) create a method that accepts 2 parameters (Model m_object, Datacontract d_object) and fill the model object (Collection)

m_object.Itemid=d_object.Itemid;
m_object.Itemname=d_object.Itemid
...........

Open in new window


2)In the API controller,I have to use Model object instead of Data contract object and little bit confusion on returning IEnumerable collection

I need to sample code for the above methods (no automapper or any  othere tools) as I have very simple class with just 5 properties.

Thanks in  Advance
0

Featured Post

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question