Solved

Mapping Data contract class and Model class in MVC

Posted on 2014-03-07
4
906 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
Comment Utility
There are libraries to map the objects like automapper. http://automapper.org/
0
 

Author Comment

by:ksd123
Comment Utility
We are not suppose to use automapper.
0
 
LVL 16

Expert Comment

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

Author Comment

by:ksd123
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now