Breeze.js mixing DTOs and entities

后端 未结 1 1778
无人及你
无人及你 2021-01-01 04:29

In Ward\'s article \"The Breeze Server: Have It Your Way\":

The typical business application has a minimum of 200 domain model types. 90+% of the ti

相关标签:
1条回答
  • 2021-01-01 04:58

    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.

    0 讨论(0)
提交回复
热议问题