Breeze.js mixing DTOs and entities

泄露秘密 提交于 2019-11-30 10:12:18

You are in good company. Questions like this are stacking up. I hope to provide better guidance "soon".

In the short run (assuming you're a .NET developer), you may find some clues in the DocCode sample. Search for "ProductDto". DocCode doesn't show how you'd save changes to it so I'll have to hold that off until another time.

Your scenario may actually be easy to address.

Step #1: Use a custom DbContext

Start by writing a sub-class of your business model's DbContext. Add to this sub-class an override to your OnModelCreating and teach it to ignore the User properties that should not be part of the model.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Entity<User>().Ignore(u => u.whatever);
   ...
   base.OnModelCreating(modelBuilder);
}

Now refer to THIS derived DbContext when communicating with clients.

Notice that this involves a very small amount of code and is easy to maintain. It doesn't interfere with your use of the base DbContext which retains full access to all properties of User.

Step #2: Configure JSON.NET to exclude these properties from serialization

Follow James Newton King's guidance. Look in particular at IContractResolver if you don't want to decorate/pollute your User class with the [JsonIgnore] attribute. James is the author of JSON.NET.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!