Solved

Mapping Data contract class and Model class in MVC

Posted on 2014-03-07
4
940 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

776 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