使用不同但相似的对象而不进行分支
多态性、代码可重用性、OOP、C# 问题。我想创建一个类似的方法:
private void ManyLinesOfCode(DataContext mycontext)
但我无法向它发送我的实际数据上下文,因为它们不是 DataContext 类型,而且我无法进行任何编译的转换。
var includeCustomersFlag = Request["includeCustomersFlag"];
if (includeCustomersFlag == "1")
{
var context = new TypeByRepGroupDataContext();
var lines = context.TypeByRepGroups;
.... many lines of code
}
else if (includeCustomersFlag == "2")
{
var context = new TypeByRepGroupNoChainsDataContext();
var lines = context.TypeByRepGroupsNoChains;
.... many lines of code
}
else
{
var context = new TypeByRepGroupChainsOnlyDataContext();
var lines = context.TypeByRepGroupsChainsOnliess;
.... many lines of code
}
我有一个解决方法,在我的数据上下文中使用单个参数化存储过程,而不是基于不同视图的多个数据上下文,但更喜欢对上面的臃肿代码使用 ac# 解决方案。
我正在尝试使用阿尔宾的解决方案。我可以编译这个:
public interface IMyInterface
{
string arcxpostyy {get; set;}
string arcxpostmm {get; set;}
string CustArea {get; set;}
string type {get; set;}
System.Nullable<decimal> amt {get; set;}
}
partial class TypeByRepGroup : IMyInterface { }
partial class TypeByRepGroupNoChain : IMyInterface { }
partial class TypeByRepGroupChainsOnly : IMyInterface { }
但是......它并没有给我带来任何东西。我非常怀疑我是否正确地制作了这个界面,但我至少想做出努力,并且不确定如何在我的代码中使用它(我的问题中的控制器片段)。互联网上没有链接到该解决方案的实际实现我能找到的。有人能完整地阐明解决方案吗?
几周后我开始担任高级软件工程师,并且希望能够做这种事情。
Polymorphism, Code reusibility, OOP, C# question. I would like to create a method like:
private void ManyLinesOfCode(DataContext mycontext)
but I wouldn't be able to send it my actual datacontexts because they are not of type DataContext and I haven't been able to do any casting that compiles.
var includeCustomersFlag = Request["includeCustomersFlag"];
if (includeCustomersFlag == "1")
{
var context = new TypeByRepGroupDataContext();
var lines = context.TypeByRepGroups;
.... many lines of code
}
else if (includeCustomersFlag == "2")
{
var context = new TypeByRepGroupNoChainsDataContext();
var lines = context.TypeByRepGroupsNoChains;
.... many lines of code
}
else
{
var context = new TypeByRepGroupChainsOnlyDataContext();
var lines = context.TypeByRepGroupsChainsOnliess;
.... many lines of code
}
I have a workaround by using a single parameterized stored procedure in my datacontext instead of multiple datacontexts based on different views, but would prefer to have a c# solution to the bloated code above.
I am trying to work with Albin's solution. I can compile this:
public interface IMyInterface
{
string arcxpostyy {get; set;}
string arcxpostmm {get; set;}
string CustArea {get; set;}
string type {get; set;}
System.Nullable<decimal> amt {get; set;}
}
partial class TypeByRepGroup : IMyInterface { }
partial class TypeByRepGroupNoChain : IMyInterface { }
partial class TypeByRepGroupChainsOnly : IMyInterface { }
But... it doesn't get me anything as is. I highly doubt I am making this interface right but I wanted to at least make an effort, and not sure how to use it in my code (the controller snippet in my question.) No links on the internet to an actual implementation of this solution that I could find. Can anybody fully spell out the solution?
I start a job as Senior Software Engineer in a couple weeks and would like to be able to do this kind of thing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的大问题不是 DataContext 的类型。就是你在不同的上下文中使用不同的实体。
确保
TypeByRepGroups
、TypeByRepGroupsNoChains
和TypeByRepGroupsChainsOnliess
实现通用接口。创建一个分部类,在其中定义这些类实现该接口。It is not the type of the DataContexts that are the big problems here. It is that you use different entities on the different contexts.
Make sure
TypeByRepGroups
,TypeByRepGroupsNoChains
andTypeByRepGroupsChainsOnliess
implements a common interface. Create a partial class where you define that those classes implements the interface.