使用不同但相似的对象而不进行分支

发布于 2024-12-08 17:21:19 字数 1446 浏览 0 评论 0原文

多态性、代码可重用性、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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把梦留给海 2024-12-15 17:21:19

这里的大问题不是 DataContext 的类型。就是你在不同的上下文中使用不同的实体。

确保 TypeByRepGroupsTypeByRepGroupsNoChainsTypeByRepGroupsChainsOnliess 实现通用接口。创建一个分部类,在其中定义这些类实现该接口。

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 and TypeByRepGroupsChainsOnliess implements a common interface. Create a partial class where you define that those classes implements the interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文