Gineer
Gineer

Reputation: 2408

extending classes that must be used in an interface

I have created an interface as shown below. The DTO object is a complex value object with 3 parameters.

public interface IOperation
{
    DTO Operate(DTO ArchiveAndPurgeDTO);
}

I need people that impliment this interface to be able to inherit from the original Value object and extend it where required.

My assumption was that they could simply inherit the DTO object, add (for example) another property and use it in the same class that impliments this interface.

When I try to use the extended value object, Visual Studio complains that I am no longer implimenting the interface.

How can I impliment this functionality.

Thanks in advance for any ideas, and/or suggestions.

Gineer

Edit: DTO Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Company.ArchiveAndPurge
{
    public class DTO
    {
        public DTO(String FriendlyID)
        {
            friendlyId = FriendlyID;
        }

        private String friendlyId = String.Empty;

        public String FriendlyId 
        { 
            get { return friendlyId; }
            set { friendlyId = value; } 
        }

        private String internalId = String.Empty;

        public String InternalyId
        {
            get { return internalId; }
            set { internalId = value; }
        }

        private Boolean archivedSuccessfully = false;

        public Boolean ArchivedSuccessfully
        {
            get { return archivedSuccessfully; }
            set { archivedSuccessfully = value; }
        }
    }
}

Extended DTO:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Company.MSO.ArchiveAndPurge
{
    public class DTO: Company.ArchiveAndPurge.DTO
    {
        private Boolean requiresArchiving = true;

        public Boolean RequiresArchiving
        {
            get { return requiresArchiving; }
            set { requiresArchiving = value; }
        }
    }
}

Interface Implementation where VS Complains:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Company.ArchiveAndPurge.Contracts;
using Company.ArchiveAndPurge;

namespace Company.MSO.ArchiveAndPurge
{
    public class ResolveFriendlyId: IOperation
    {
        #region IOperation Members

        public DTO Operate(DTO ArchiveAndPurgeDTO)
        {
            ArchiveAndPurgeDTO.InternalyId = ArchiveAndPurgeDTO.FriendlyId;
            return ArchiveAndPurgeDTO;
        }

        #endregion
    }
}

Upvotes: 1

Views: 184

Answers (2)

magnattic
magnattic

Reputation: 12998

Use Generics:

public interface IOperation<T> where T : DTO
{
    T Operate(T ArchiveAndPurgeDTO);
}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500983

As I understand it, you probably had something like:

public class ExtendedOperation : IOperation
{
    public ExtendedDTO Operate(ExtendedDTO dto)
    {
        ...
    }
}

That doesn't work in two ways:

  • You can't change the return type when implementing an interface method
  • You can't change the parameter list when implementing an interface

In particular, you wouldn't be implementing IOperation in a way which would be compatible with code like this:

IOperation operation = new ExtendedOperation();
operation.Operate(new DTO());

I suspect you might want to make the interface generic:

public interface IOperation<T> where T : DTO
{
    T Operate(T dto);
}

Upvotes: 2

Related Questions