Solved

Mapping Data contract class and Model class in MVC

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 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