Architecture: entity framework Web Api

Hubert Solecki

I'm creating a web api program using entity framework. So as the basis, I have an sql server database which I'm connected to with entity framework trought my web api program. Using an add-on for entity framework, I'v generated classes according to my database tables. However i don't want to use these classes for my webservices because I don't need to display some of the attributes generated by the entity framework and little bit tricky with all the proxies problems. These attributes are especially generated because of the foreign keys. As below, for this generated class, I don't need to display "Societe" object and "Utilisateur" object:

 public partial class FonctionnalitePerUser
{
    public int FonctionUserLngId { get; set; }
    public int FonctionUserLngUserId { get; set; }
    public int FonctionUserLngSocieteId { get; set; }
    public virtual Societe Societe { get; set; }
    public virtual Utilisateur Utilisateur { get; set; }

}

I would need some advice to avoid displaying that entities in my webservices. I was thinking about 3 possibilities:

  • As it's a partial class, I might create an other partial class with the same name where I put the attributes that I need and override the constructor.
  • I might inherit a custom class from that one to override the constructor in order to get one structured as I need.
  • I might create Management classes with functions that create the perfect objects that I need for my webservices. I mean functions that convert "FonctionnalitePerUser" object to "FonctionnalitePerUserCustom" objects.

These are the 3 solutions that I've found. In order to get the best performance, I was wondering if anyone can give me some advise about that or either propose some other solutions. Thanks in advance

Davin Tryon

In general, it is often useful to expose a different type of object for a web service API than for persistence. This is for exactly the reason you state: because you don't need to expose all of that persistence stuff to the rest of the world (clients).

Usually, you would map the information that you want to expose from your persistence model (EF entities etc) to a view model object (or DTO).

So, I would say your option 3 is on the right track.

I might create Management classes with functions that create the perfect objects that I need for my webservices. I mean functions that convert "FonctionnalitePerUser" object to "FonctionnalitePerUserCustom" objects

There are several tools out there that help with the converting or mapping of the objects. One is AutoMapper which will map by convention. This can save a lot of mapping code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Implement the Entity Framework in the Web API

From Dev

Error while publishing a Web API with Entity Framework

From Dev

Pagination using Entity Framework through Web API

From Dev

Pagination using Entity Framework through Web API

From Dev

Entity Framework Many to many Web API Get

From Dev

Web API with separate entity framework data layer

From Dev

Using Web API by Generating Entity Framework scaffolding API Controller Error

From Dev

Entity Framework web or Entity Object

From Dev

ASP Web API Controller with Entity Framework very slow and strange results

From Dev

Resolving Entity Framework's AccountController with Simple Injector and Web API

From Dev

Web API integrated with Entity framework 5 with database first approach

From Dev

Entity Framework and Web API/REST - Joining to list properties

From Dev

Which Versions of Entity Framework are Compatible with Web API 2?

From Dev

Entity Framework disposing with async controllers in Web api/MVC

From Dev

Entity Framework and Web API/REST - Joining to list properties

From Dev

Web ApI Entity Framework (Code First) Value not appearing in database

From Dev

ASP Web API Controller with Entity Framework very slow and strange results

From Dev

Search data in asp.net web api and Entity Framework

From Dev

Trouble connecting to my database with entity framework c# web api

From Dev

ASP.NET Web API 2 CRUD Operation with Entity Framework

From Dev

MVC architecture without using Entity Framework

From Dev

MVC architecture without using Entity Framework

From Dev

Working with WPF and Entity Framework together - what architecture?

From Dev

Is Entity framework only for web applications?

From Dev

Use Entity Framework with Web Proxy

From Dev

Return an object along with a 409 Conflict error in a Web API 2 POST call backed by Entity Framework?

From Dev

Asp.net web api + entity framework: multiple requests cause data conflict

From Dev

How to configure automapper for Unity dependency injection and Entity Framework in MVC 5 / Web API 2.0 app?

From Dev

Datetime field won't update Entity Framework SQL Server 2012 and Web API

Related Related

  1. 1

    Implement the Entity Framework in the Web API

  2. 2

    Error while publishing a Web API with Entity Framework

  3. 3

    Pagination using Entity Framework through Web API

  4. 4

    Pagination using Entity Framework through Web API

  5. 5

    Entity Framework Many to many Web API Get

  6. 6

    Web API with separate entity framework data layer

  7. 7

    Using Web API by Generating Entity Framework scaffolding API Controller Error

  8. 8

    Entity Framework web or Entity Object

  9. 9

    ASP Web API Controller with Entity Framework very slow and strange results

  10. 10

    Resolving Entity Framework's AccountController with Simple Injector and Web API

  11. 11

    Web API integrated with Entity framework 5 with database first approach

  12. 12

    Entity Framework and Web API/REST - Joining to list properties

  13. 13

    Which Versions of Entity Framework are Compatible with Web API 2?

  14. 14

    Entity Framework disposing with async controllers in Web api/MVC

  15. 15

    Entity Framework and Web API/REST - Joining to list properties

  16. 16

    Web ApI Entity Framework (Code First) Value not appearing in database

  17. 17

    ASP Web API Controller with Entity Framework very slow and strange results

  18. 18

    Search data in asp.net web api and Entity Framework

  19. 19

    Trouble connecting to my database with entity framework c# web api

  20. 20

    ASP.NET Web API 2 CRUD Operation with Entity Framework

  21. 21

    MVC architecture without using Entity Framework

  22. 22

    MVC architecture without using Entity Framework

  23. 23

    Working with WPF and Entity Framework together - what architecture?

  24. 24

    Is Entity framework only for web applications?

  25. 25

    Use Entity Framework with Web Proxy

  26. 26

    Return an object along with a 409 Conflict error in a Web API 2 POST call backed by Entity Framework?

  27. 27

    Asp.net web api + entity framework: multiple requests cause data conflict

  28. 28

    How to configure automapper for Unity dependency injection and Entity Framework in MVC 5 / Web API 2.0 app?

  29. 29

    Datetime field won't update Entity Framework SQL Server 2012 and Web API

HotTag

Archive