简体   繁体   English

如何首先使用带有EF代码的wcf数据服务公开dto对象?

[英]How can i expose dto objects using wcf data service with ef code first?

I am trying to make a wcf data service where i dont want to get acces to the database models but instead i want to use Data transfer objects. 我正在尝试提供WCF数据服务,我不想访问数据库模型,但我想使用数据传输对象。 I have been reading a lot on the internet about how to accomplish this but i cant get a good answer for my problem. 我已经在互联网上阅读了很多有关如何完成此操作的信息,但是对于我的问题我却找不到很好的答案。 It is the first time for me doing something with wcf data services so i am a little inexperienced. 这是我第一次使用wcf数据服务来做某事,所以我有点没有经验。

Oke here are my models that are linked to my database using Entity Framework 好的,这是我的模型,这些模型使用实体框架链接到我的数据库

public class User
    {
        [Key]
        public int UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }

        public string CountryCode { get; set; }
        public string PhoneNumber { get; set; }
        public ICollection<User> Contacts { get; set; }

        public virtual Language Language { get; set; }

        public User()
        {
            Contacts = new List<User>();
        }
    }

public class Message
    {
        [Key]
        public int MessageId { get; set; }
        public DateTime SentDate { get; set; }

        public virtual User Sender { get; set; }
        public virtual User Receiver { get; set; }

        public string Content { get; set; }

        public string OriginalCultureInfoEnglishName { get; set; }
        public string ForeignCultureInfoEnglishName { get; set; }
    }

public class Language
    {
        [Key]
        public int LanguageId { get; set; }
        public string CultureInfoEnglishName { get; set; }
    }

Now i made a Service.svc which has my DatabaseContext so it can directly acces my database models. 现在,我制作了一个Service.svc,其中包含我的DatabaseContext,因此可以直接访问我的数据库模型。 What i want to achieve is that instead of directly getting the database models i would like to get the DTO models when i query against my service. 我要实现的是,当我查询我的服务时,我想获得DTO模型,而不是直接获得数据库模型。

A Example of how my dto's would look like 我的dto的外观示例

public class UserDTO
    {
        public int UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        public string Country { get; set; }
        public string PhoneNumber { get; set; }

        public ICollection<ContactDTO> Contacts { get; set; }

        public virtual LanguageDTO Language { get; set; }

        public UserModel()
        {
            Contacts = new List<ContactDTO>();
        }
    }

public class ContactDTO
    {
        public int UserId { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }

        public string Country { get; set; }
        public string PhoneNumber { get; set; }

        public virtual LanguageDTO Language { get; set; }
    }

public class LanguageDTO
    {
        public int LanguageId { get; set; }
        public string CultureInfoEnglishName { get; set; }
    }

public class MessageDTO
    {
        public int MessageId { get; set; }
        public DateTime SentDate { get; set; }

        public virtual ContactDTO Sender { get; set; }
        public virtual ContactDTO Receiver { get; set; }

        public string Content { get; set; }

        public string OriginalCultureInfoEnglishName { get; set; }
        public string ForeignCultureInfoEnglishName { get; set; }
    }

Now is it possible to do it like this by making a different context that i can use in my service.svc or is there any other way to achieve the this? 现在可以通过创建可以在service.svc中使用的不同上下文来做到这一点,或者是否有其他方法可以实现此目的?

for example i would like to get ContactDto by userid which is a user but with less properties because they are not relevant in the client application. 例如,我想通过作为用户但具有较少属性的userid获取ContactDto,因为它们与客户端应用程序无关。 I see this happening by a uri http://localhost:54895/Service.svc/ContactDto(1) 我看到这是通过uri http://localhost:54895/Service.svc/ContactDto(1)

Hopefully anyone can clear this up for me because it is really frustrating :) 希望任何人都可以为我解决这个问题,因为它确实令人沮丧:)

I'm not sure that what you're interested in is possible, exactly. 我不确定您感兴趣的内容是否完全可能。 You are looking to have multiple entity sets per type (aka MEST ), and I don't know how well that's supported. 您正在寻找每种类型有多个实体集(又名MEST ),但我不知道支持的程度如何。

Beyond that point, and into a discussion around DTOs in general... 超出这一点,并进入有关DTO的一般讨论...

If you use custom providers, you can implement your own IDataServiceMetadataProvider and IDataServiceQueryProvider . 如果使用自定义提供程序,则可以实现自己的IDataServiceMetadataProviderIDataServiceQueryProvider When your service starts, you can make calls into the IDataServiceMetadataProvider to control what entities and properties are exposed or hidden -- including exposing properties that do not actually exist on your entity. 服务启动时,您可以调用IDataServiceMetadataProvider来控制公开或隐藏哪些实体和属性-包括公开实体上实际不存在的属性。 The upshot is that you end up with a DTO without coding a DTO class. 结果是您最终得到了DTO,而没有编写DTO类。 The exposed metadata is the DTO. 公开的元数据是DTO。 This is a good resource for creating your own providers. 是用于创建自己的提供程序的良好资源。

In your case, this isn't a 100% solution, because you can't selectively choose when a property is exposed and when it's not. 对于您而言,这不是100%的解决方案,因为您无法选择性地选择何时公开属性,何时不公开属性。

Hope this helps... 希望这可以帮助...

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

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