简体   繁体   English

架构:实体框架Web Api

[英]Architecture: entity framework Web Api

I'm creating a web api program using entity framework. 我正在使用实体框架创建一个web api程序。 So as the basis, I have an sql server database which I'm connected to with entity framework trought my web api program. 所以作为基础,我有一个sql server数据库,我用实体框架连接到我的web api程序。 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. 但是我不想将这些类用于我的webservices,因为我不需要显示实体框架生成的一些属性,并且对于所有代理问题都有点棘手。 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: 如下所示,对于这个生成的类,我不需要显示“Societe”对象和“Utilisateur”对象:

 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. 我需要一些建议,以避免在我的Web服务中显示该实体。 I was thinking about 3 possibilities: 我在考虑3种可能性:

  • 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. 我可能会创建具有函数的管理类,这些函数可以创建我的Web服务所需的完美对象。 I mean functions that convert "FonctionnalitePerUser" object to "FonctionnalitePerUserCustom" objects. 我的意思是将“FonctionnalitePerUser”对象转换为“FonctionnalitePerUserCustom”对象的函数。

These are the 3 solutions that I've found. 这些是我发现的3种解决方案。 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 提前致谢

If your using Newtonsoft Json.NET which I think is the default in MVC5 then you can attribute your properties to tell newtonsoft what to serialize and what to ignore. 如果您使用Newtonsoft Json.NET,我认为它是MVC5中的默认设置,那么您可以将属性归因于告诉newtonsoft要序列化什么以及忽略什么。

public class Car
{
  // included in JSON
  public string Model { get; set; }
  public DateTime Year { get; set; }
  public List<string> Features { get; set; }

  // ignored
  [JsonIgnore]
  public DateTime LastModified { get; set; }
}

or if you have more properties you want to ignore than you want to serialize you can do this: 或者,如果您想要忽略的属性多于想要序列化的属性,则可以执行以下操作:

[DataContract]
public class Computer
{
  // included in JSON
  [DataMember]
  public string Name { get; set; }
  [DataMember]
  public decimal SalePrice { get; set; }

  // ignored
  public string Manufacture { get; set; }
  public int StockCount { get; set; }
  public decimal WholeSalePrice { get; set; }
  public DateTime NextShipmentDate { get; set; }
}

this information was taken from here . 这些信息来自这里

In general, it is often useful to expose a different type of object for a web service API than for persistence. 通常,为Web服务API公开不同类型的对象通常比持久化更有用。 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). 通常,您可以将要从持久性模型(EF实体等)公开的信息映射到视图模型对象(或DTO)。

So, I would say your option 3 is on the right track. 所以,我想说你的选择3是在正确的轨道上。

I might create Management classes with functions that create the perfect objects that I need for my webservices. 我可能会创建具有函数的管理类,这些函数可以创建我的Web服务所需的完美对象。 I mean functions that convert "FonctionnalitePerUser" object to "FonctionnalitePerUserCustom" objects 我的意思是将“FonctionnalitePerUser”对象转换为“FonctionnalitePerUserCustom”对象的函数

There are several tools out there that help with the converting or mapping of the objects. 有几种工具可以帮助转换映射对象。 One is AutoMapper which will map by convention. 一个是AutoMapper ,它将按照惯例进行映射。 This can save a lot of mapping code. 这可以节省大量的映射代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM