Combining common methods and overloads

I have the following method:

public void Set<T>(IEnumerable<T> records)
{
     foreach (var record in records)
     {
        Set(record);
     }
 }

      

I would like to call one of the following methods Set

, depending on T

:

    public void Set(RecordType1 record)
    {
        // Some RecordType1 logic
    }

    public void Set(RecordType2 record)
    {
        // Some logic applicable to RecordType2 only
    }

      

I hope you will see that I am trying to resolve a method call Set

for output at runtime. This "doesn't work" (ie, won't compile as it expects RecordType1

).

Question

How can I keep a structure like this without testing for the type before posting the records to the method Set

?

+3


source to share


2 answers


Why don't you create an interface ( IRecordType

), allow RecordType1

and RecordType2

inherit from it, and then move the main logic of each method Set

into the interface.

public interface IRecordType
{
    void Set(...);
}
public void Set(IEnumerable<IRecordType> records)
{
    foreach (var record in records)
    {
        record.Set(...);
    }
}

      

This is a more convenient solution. And it also provides better polymorphism.

Edit: resource to look at: https://msdn.microsoft.com/en-us/library/3b5b8ezk%28v=vs.90%29.aspx

In addition, a small sidebar: with the help interface

you can not only share certain methods, but also properties and events. If RecordType1

u RecordType2

have several properties in common, you can add those properties to interface

, and then use IRecordType

wherever you previously had to distinguish between the two to use these properties, methods or events, Similarly, code inside any of these properties, methods or eventsallowed to use other properties, methods, events or fields specific to the object itself. This is the goal of Object Oriented Languages ​​( C # ) and polymorphism.



Edit: As a result of the discussion in the comments, I also want to add additional information about the decision between using OOP approach ( interface

, abstract class

, class

), and the approach dynamic

StriplingWarrior offered:

If you do not have access to the actual implementation details RecordType1

or RecordType2

, or you cannot change the design of your application (due to the fact that you disagree with Set(RecordType1)

and Set(RecordType2)

methods, you may find it more fruitful to take advantage of its use dynamic

. There are other options, about which we might not have thought - you can always give one of them The disadvantage is dynamic

that it requires .NET 4.0.

In addition, more reasons, if you have access to the details of implementation RecordType1

and RecordType2

, but can not be changed Set(RecordType1)

and Set(RecordType2)

the definition, you can always change them:

public void Set(RecordType1 record)
{
    record.Set(...);
}

public void Set(RecordType2 record)
{
    record.Set(...);
}

      

This preserves the structure of the ENTIRE application and also reduces code maintenance and allows polymorphism.

+13


source


If you are sure that every item in a given collection will match the method signature, you can specify values ​​as dynamic

and let runtime determine what to bind it to:

 foreach (var record in records)
 {
    Set((dynamic)record);
 }

      



However, this is very error prone and I would recommend that you take a close look at what you are really trying to accomplish. There is most likely the best sample to meet your needs.

+7


source







All Articles