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
代码>。请看:非常感谢您的耐心等待。非常感谢。非常感谢您在这里的耐心。非常感谢。