开发者

use different but similar objects without branching

开发者 https://www.devze.com 2023-04-11 21:06 出处:网络
Polymorphism, Code reusibility, OOP, C# question.I would like to create a method like: private void ManyLinesOfCode(DataContext mycontext)

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;开发者_StackOverflow社区}
    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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号