Object-oriented design: Saving complex objects

后端 未结 2 1945
别那么骄傲
别那么骄傲 2021-01-07 11:39

I have a complex domain model built on top of a legacy system that I\'ve built most of the \"get\" methods for - typically just by passing around database primary key IDs. E

2条回答
  •  时光说笑
    2021-01-07 12:14

    You should investigate the popular object-relational mapping frameworks for .NET. A lot of people have handled this problem before, and some of the smartest ones have shared the fruits of their labor with us. They've got loads of features and the most popular ones are heavily tested.

    Every few days, it seems, someone new asks for .NET ORM recommendations, so there are plenty of Stack Overflow topics to help guide you. For example:

    • What are your favorite .NET Object Relational Mappers (ORM)?
    • What ORM frameworks for .NET Do You Like Best?
    • Which ORM for .net would you recommend?

    (If you search on 'ORM' and '.NET' you'll turn up many many more matches.)

    In typical usage, the UI developer only deals with high-level parent objects (called 'aggregate roots' in the language of Domain-Driven Design). They use the ORM as a Repository and it ensures all the operations in a given Save are bundled together in sensible transactions. (You still need to enforce validity per business rules in your domain objects.)

    Here's a simplified look at what UI code would look like:

    // Given an IRepository implemented by your ORM,
    ICustomer customer = repository.Get(customerId);
    
    // ... do stuff to customer and its child objects ...
    
    repository.Save(customer);
    

提交回复
热议问题