Return type of interface

I have server-side classes with similar method signatures that I want to capture in an interface:

namespace MyLib

public class ClientList
    public ICollection<Client> Fetch() {
    {
        //do stuff
       return this.CreateCollection();
    }

    private ICollection<Client> CreateCollection()
    {
        List<Client> clientList = new List<Client>();
        // populate list
        return clientList;
    }

public class ProductList
    public ICollection<Product> Fetch() {
        //do stuff
       return this.CreateCollection();
    }

    private ICollection<Product> CreateCollection()
    {
        List<Product> productList = new List<Product>();
        // populate list
        return productList ;
    }

      

I need a method signature interface for Fetch that returns ICollection, enter undefined (as it will be different for each list). This ensures that every list object * will have a fetch method, and new ones will not have "getList" or other such named calls. With a bit of research, I believe that generics might be the way to go, but I'm not sure how.

I tried

public interface IDataRequest
    ICollection<T> Fetch<T>();

      

but when i implemented it as

public ICollection<Client> Fetch<Client>()

      

I got the error 'return this.CreateCollection ();':

Cannot implicitly convert type 'System.Collections.Generic.ICollection <MyLib.Client>' to 'System.Collections.Generic.ICollection <Client>'. Explicit conversion exists (are you missing a role?)

I thought it might be because I didn't specify a namespace. but if i change to

public ICollection<MyLib.Client> Fetch<MyLib.Client>()

      

then I got the error:

The type parameter declaration must be an identifier, not a type

in Fetch <MyLib.Client> ()

and finally if I change it to:

public ICollection<MyLib.Client> Fetch<Client>()

      

then I got the error:

'MyLib.ClientList' does not implement the MyLib.IDataRequest.Fetch () interface element. 'MyLib.ClientList.Fetch ()' cannot implement 'MyLib.IDataRequest.Fetch ()' because it does not have a corresponding return type 'System.Collections.Generic.ICollection'.

I don't have much knowledge of generics and you have reached the limit of what I can try with cargo cult attempts. Is this what I want to make possible? If so, you can show me both the interface signature of the interface and an example of a class method definition.

As pointed out in the comment, here is the client class:

namespace MyLib
{
    using System.Data;
    using System.Runtime.Serialization;

    [DataContract]
    public class Client
    {
        public Client(DataRow clientRecord)
        {
            this.clientId = clientRecord.Field<string>("ID");
            this.cphh = clientRecord.Field<string>("ID");
            this.name = clientRecord.Field<string>("Name");
            this.address = clientRecord.Field<string>("Address");

            if (CommonUtilities.GIS.ValidateOSMapRef(clientRecord.Field<string>("Locationd")))
            {
                this.location = string.Format(
                    "{0}, {1}",
                    CommonUtilities.GIS.ConvertMapRefToEasting(clientRecord.Field<string>("Locationd")),
                    CommonUtilities.GIS.ConvertMapRefToNorthing(clientRecord.Field<string>("Locationd")));
            }
        }

        [DataMember]
        public string clientId { get; private set; }

        [DataMember]
        public string name { get; private set; }

        [DataMember]
        public string address { get; private set; }

        [DataMember]
        public string location { get; private set; }

        [DataMember]
        public string cphh { get; private set; }
    }
}

      

+3


source to share


1 answer


You have misidentified your interface. Try it like this:

public interface IDataRequest<T>
{
    ICollection<T> Fetch();
}

      

Then your classes look like this:



public class ClientList : IDataRequest<Client>
{
    public ICollection<Client> Fetch()
    {
        //do stuff
       return this.CreateCollection();
    }

    private ICollection<Client> CreateCollection()
    {
        List<Client> clientList = new List<Client>();
        // populate list
        return clientList;
    }
}

public class ProductList : IDataRequest<Product>
{
    public ICollection<Product> Fetch()
    {
        //do stuff
       return this.CreateCollection();
    }

    private ICollection<Product> CreateCollection()
    {
        List<Product> productList = new List<Product>();
        // populate list
        return productList ;
    }
}

      

It compiles well.

+3


source







All Articles