Solved

Mapping Data contract class and Model class in MVC

Posted on 2014-03-07
4
1,001 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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