Serialize to XML - private properties

前端 未结 2 486
攒了一身酷
攒了一身酷 2021-01-06 08:34

I\'m looking for a way to serialize a POCO that contains some read-only properties. In some Google and StackOverflow searches, I\'ve seen the following suggestions:

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-06 09:19

    While it would be sweet if serialize could access private properties unfortunately as of today there is no easy way.

    But there is another option in the way of an architecture solution. Do NOT destroy your business domain requirements, instead seperate your layers similar to a a nTeir design and implement DTO's...

    If you seperate your business, datafacade/dataadaptor (factory pattern fits well here) and DataAccess layers into 3 projects you can control through referencing that business never know about your DTO's. Hense if you decided to delete or implement the serialization or swap it to saving to SQL server you would not affect anything in your business layer.

    There is always one downfall, there is a bunch more code to write: * you have to write a object convertor both ways for each entity you wish to go to Dataaccess * you potentially destroy some of the OO hiding sinse a .Save method in business will need to be translated to the correct type in Dataface before moving on down to dataaccess

    You can make this a whole lot more easy with something like nHybinate or similar. Cheers Choco

提交回复
热议问题