C# 用于访问对象图中更深层次的父对象属性的模式或方法
在我们的场景中,对象图中“更深”的对象需要根据祖先的值更新值。例如,考虑一个映射练习,我们从一个Web视图中映射一个对象的值。我们有原始的持久化对象,因为它存在于数据库/存储中,我们有从视图中传入的变异对象。给定下面的可视化“模式”,如果我们要将视图中的传入对象映射到数据库中的持久化“twin”,并且希望根据策略默认免赔额的值设置覆盖免赔额的值,有没有一种方法可以在不传递对父对象的引用的情况下实现这一点C# 用于访问对象图中更深层次的父对象属性的模式或方法,c#,design-patterns,mapping,C#,Design Patterns,Mapping,在我们的场景中,对象图中“更深”的对象需要根据祖先的值更新值。例如,考虑一个映射练习,我们从一个Web视图中映射一个对象的值。我们有原始的持久化对象,因为它存在于数据库/存储中,我们有从视图中传入的变异对象。给定下面的可视化“模式”,如果我们要将视图中的传入对象映射到数据库中的持久化“twin”,并且希望根据策略默认免赔额的值设置覆盖免赔额的值,有没有一种方法可以在不传递对父对象的引用的情况下实现这一点 { Policy: { DefaultDeducitble: 1000
{
Policy: {
DefaultDeducitble: 1000
LocationCollection: [{
Location: {
BuildingCollection: [{
Building: {
CoverageCollection: [{
Coverage: { DefaultDeductible: nulll}
}]
}
}]
}
}]
}
}
我们的映射器映射对象层次结构的一个级别,因此我们将有如下内容:
class PolicyMapper { /// maps policy and calls a location collection mapper }
class LocationCollectionMapper { /// calls a location mapper for each location }
class LocationMapper {///maps the location and calls building collection mapper }
class BuildingCollectionMapper { /// calls a building mapper for each building }
class BuidlingMapper {///maps the buidling and calls coverage collection mapper }
class CoverageCollectionMapper { /// calls a coverage mapper for each coverage }
class CoverageMapper {///maps the coverage, this is where we want a value from the Policy level }
public class TransientPolicyCache {
///code to load the cache and retreive stuff
public Policy Get() { //returns the policy }
}
public class CoverageMapper {
public CovergaeMapper(TransientPolicyCache cache) { ///cache is provided by ioc }
public Coverage Map(Coverage target, Coverage source){
//need the policy
var policy = _cache.Get();
// do the things
}
}
所以问题是,我们如何访问CoverageMapper中的策略级别信息,而不在整个映射层次结构中传递策略对象?一个想法是有一个我们可以查询的临时缓存。我真的不知道这是否存在…或者这是一个好主意还是一个反模式…但是它看起来像:
class PolicyMapper { /// maps policy and calls a location collection mapper }
class LocationCollectionMapper { /// calls a location mapper for each location }
class LocationMapper {///maps the location and calls building collection mapper }
class BuildingCollectionMapper { /// calls a building mapper for each building }
class BuidlingMapper {///maps the buidling and calls coverage collection mapper }
class CoverageCollectionMapper { /// calls a coverage mapper for each coverage }
class CoverageMapper {///maps the coverage, this is where we want a value from the Policy level }
public class TransientPolicyCache {
///code to load the cache and retreive stuff
public Policy Get() { //returns the policy }
}
public class CoverageMapper {
public CovergaeMapper(TransientPolicyCache cache) { ///cache is provided by ioc }
public Coverage Map(Coverage target, Coverage source){
//need the policy
var policy = _cache.Get();
// do the things
}
}
这让我想起了很多WPF
- 可视树中的每个实例都可以指定一个值,包括缺少此属性的内容
- 如果一个实例没有值,它将沿着可视化树运行,直到在任何对象上找到一个
- 如果仍然没有找到任何内容,将使用默认值。当然,整个程序中只有一个实例
但我不确定他们是否真的能在这方面帮助您。也许我不完全理解,但设置保险免赔额的默认值以匹配保单级别免赔额的责任不应该位于链的更上游,并且当您调用
setDefault免赔额()
时,它沿着对象层次结构向下走,并更新覆盖率免赔额。@Rakesh这是我们目前采取的方法。映射发生,并且是1:1。映射后处理发生,并且保持映射的关注点,并使用单独的规则设置默认值。接口如何?