Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于更新接口实现的引用的模式_C#_.net_Design Patterns - Fatal编程技术网

C# 用于更新接口实现的引用的模式

C# 用于更新接口实现的引用的模式,c#,.net,design-patterns,C#,.net,Design Patterns,我有以下界面: public interface ISearchProperties { string CurrentUserLocation { get; set; } string SearchUsername { get; set; } } 通过以下实现: public class BroadcastPreviewDto : ISearchProperties { // other properties } public class ProfileSearch

我有以下界面:

 public interface ISearchProperties {
    string CurrentUserLocation { get; set; }
    string SearchUsername { get; set; }
 }
通过以下实现:

public class BroadcastPreviewDto : ISearchProperties {
    // other properties
}
public class ProfileSearchDto : ISearchProperties {
    // other properties
}
我有以下职能:

public void PrepSearchProperties(ProfileSearchDto query) {
    // do a bunch of stuff to query properties here (only on ISearchProperties properties)
}
public void PrepSearchProperties(BroadCastPreviewDto query) {
    // do a bunch of same stuff to query properties here (only on ISearchProperties properties)
}
问题是,这不是很枯燥-功能体是完全相同的东西。我试着这样做:

public void PrepSearchProperties(ISearchProperties query) {
    // do a bunch of stuff to query properties here
}
但是,除非我将原始的
查询
声明为
ISearchProperties
,这将剥离实现类属性,否则这就不太管用了


我可以遵循什么模式来干燥代码?

如果您有此函数定义:

public void PrepSearchProperties(ISearchProperties query) {
    // statements of the form:
    query.SearchProperty = 123;
}
然后可以将
ISearchProperties
的任何实现传递给它。例如:

public class BroadcastPreviewDto : ISearchProperties {
    // implement ISearchProperties here
    // more, implementation-specific properties, e.g.
    public string BroadcastType { get; set; }
}

var bp = new BroadcastPreviewDto() {
    // set implementation specific properties here
    BroadcastType = "example"
};

// this compiles and executes fine
PrepSearchProperties(bp);

// Same instance as before. No properties stripped.
Console.WriteLine(bp.BroadcastType);  

如果您有此函数定义:

public void PrepSearchProperties(ISearchProperties query) {
    // statements of the form:
    query.SearchProperty = 123;
}
然后可以将
ISearchProperties
的任何实现传递给它。例如:

public class BroadcastPreviewDto : ISearchProperties {
    // implement ISearchProperties here
    // more, implementation-specific properties, e.g.
    public string BroadcastType { get; set; }
}

var bp = new BroadcastPreviewDto() {
    // set implementation specific properties here
    BroadcastType = "example"
};

// this compiles and executes fine
PrepSearchProperties(bp);

// Same instance as before. No properties stripped.
Console.WriteLine(bp.BroadcastType);  

您是否有必要使用
ref
为什么不使用方法
public void preSearchProperties(ISearchProperties查询)
?您不必返回任何内容。您不需要
ref
,除非您实际为
query
赋值。你是吗?我怀疑您可能不太了解
ref
的功能,或者您不了解,但是您的示例中缺少相关的代码。所以您在这些方法中创建了一个新对象?然后使用一个方法
private void PrepSearchPropertiesImpl(ISearchProperties查询)
处理所有真正只需要一个
ISearchProperties
对象的东西,然后从其他方法调用它,比如:
public void PrepSearchProperties(ref ProfileSearchDto query){query=new ProfileSearchDto();PreSearchPropertiesImpl(query);}
?如果只修改属性,则不需要
ref
。此代码中不需要
ref
query.xyz=“foobar”。如果您正在执行
query=newprofilesearchdto(),则只需要
ref
。请参阅:是否有绝对必须使用
ref
的原因为什么不使用方法
public void preSearchProperties(ISearchProperties查询)
?您不必返回任何内容。您不需要
ref
,除非您实际为
query
赋值。你是吗?我怀疑您可能不太了解
ref
的功能,或者您不了解,但是您的示例中缺少相关的代码。所以您在这些方法中创建了一个新对象?然后使用一个方法
private void PrepSearchPropertiesImpl(ISearchProperties查询)
处理所有真正只需要一个
ISearchProperties
对象的东西,然后从其他方法调用它,比如:
public void PrepSearchProperties(ref ProfileSearchDto query){query=new ProfileSearchDto();PreSearchPropertiesImpl(query);}
?如果只修改属性,则不需要
ref
。此代码中不需要
ref
query.xyz=“foobar”。如果您正在执行
query=newprofilesearchdto(),则只需要
ref
。请看:非常感谢您的耐心等待。非常感谢。非常感谢您在这里的耐心。非常感谢。