Search code examples
c#.net-coresoap-clientpartial-classesduck-typing

How would I process two classes that are exactly the same except by name?


I am writing a service in Core 3.1. In the service that I am writing, I create a connected service to a legacy SOAP service that interfaces with an even older system. The SOAP service provides basic CRUD operations and uses classes as data containers. The problem is that the generated code for the service reference creates separate partial classes for objects that have the exact same properties.

public partial class REQUEST1
{
    public string PropertyName {get; set;}
}
public partial class REQUEST2
{
    public string PropertyName {get; set;}
}

I find that I am writing the same code over and over with respect to preparing the request object.

private void SetProperties(MyClass parameters, REQUEST1 request)
{
    request.PropertyName = parameters.MyParamValue;
}
private void SetProperties(MyClass parameters, REQUEST2 request)
{
    request.PropertyName = parameters.MyParamValue;
}

I don't want to modify the generated code as the next time it gets generated someone will have to remember to do that. Interfaces and base classes aren't really an option as that is modifying generated code. So I'm looking for suggestions on how to write a method that can take one of these classes and set values without the cookie-cutter code writing.

UPDATE: So this got more complicated. In the create/update services, I have complex objects.

public partial class ADDRESS
{
    // Address Properties created by the service reference
}

public partial class PERSON
{
    ADDRESS[] ADDRESSES { get; set;}
    // Other properties created by the service reference
}

I can create the interface for address and person but that creates another problem.

public interface IAddress
{
    // Address properties 
}
public interface IPerson
{
    IAddress[] ADDRESSES {get;set;}
    // Other properties
}
public partial class ADDRESS : IAddress
{
}
public partial class PERSON : IPerson
{
}

This creates an error stating that PERSON doesn't implement the member IAddress[] ADDRESSES because it doesn't have the correct return type. This makes sense but I'm not sure how to get around it. For individual objects with primitive types this approach works but for more complex types it seems to require another solution.


Solution

  • I suggest adding an interface to the classes using partial classes.

    public interface IRequest
    {
       string PropertyName {get; set;}
    }
    
    public partial class REQUEST1
    {
        public string PropertyName {get; set;}
    }
    public partial class REQUEST2
    {
        public string PropertyName {get; set;}
    }
    
    public partial class REQUEST1 : IRequest
    {
    }
    
    public partial class REQUEST2 : IRequest
    {
    }
    

    You can just have a method like this

    private void SetProperties(MyClass parameters, IRequest request)
    {
        request.PropertyName = parameters.MyParamValue;
    }