Hello,

I have a difficulty with changing interface from ordinary one to generic one. Could you suggest me how I can achieve this goal, please?

This is the way how I initialize API, based on user's choice from configuration which implementation of interface should be chosen:

/// MyServiceApplication.MyServiceApplication.cs
GeneralSystemAPI.Initialize();

This is code behind Initialize():

/// MyServiceApplication.GeneralSystemAPI.cs
namespace MyServiceApplication
{
    internal static class GeneralSystemAPI
    {
        internal static void Initialize()
        {
            //...
            CompanyAPI = FirstSystemAPI.Tools.GetCompanyAPI();
        }

        internal static ICompanyAPI CompanyAPI {get; private set;}

        internal static void Destroy()
        {
            CompanyAPI = null;
        }
    }
}

And code behind GetCompanyAPI() for given implementation of the interface:

/// FirstSystemAPI.Tools.cs
namespace FirstSystemAPI
{
    public static class Tools
    {
        public static ICompanyAPI GetCompanyAPI()
        {
            return new CompanyAPI();
        }
    }
}

The whole interface is stated as follows:

/// Common.Interfaces.ICompanyAPI.cs
namespace Common.Interfaces
{
    public interface ICompanyAPI
    {
        BaseCompanyType GetCompany(string companyID);
    }
}

And implemented like this:

/// FirstSystemAPI.APIs.CompanyAPI.cs
namespace FirstSystemAPI.APIs
{
    class CompanyAPI : ICompanyAPI
    {
        public BaseCompanyType GetCompany(string companyID)
        {
            BaseCompanyType company = new BaseCompanyType();
            // ...
            return company;
        }
    }
}

However, new requirement was created and now I need to be able to return objects from different classes, like DerivedCompanyType instead of BaseCompanyType, where DerivedCompanyType inherits from BaseCompanyType.

So I have tried to change it as follows:

/// Common.Interfaces.ICompany.cs
namespace Common.Interfaces
{
    public interface ICompanyAPI<T> where T: BaseCompanyType
    {
        T GetCompany(string companyID);
    }
}

And now I have two difficulties. One is with implementation of that interface and the other with its initialization.

When it comes to implementation of the interface I change it as follows:

/// FirstSystemAPI.APIs.CompanyAPI
namespace FirstSystemAPI.APIs
{
    class CompanyAPI : ICompanyAPI<DerivedCompanyType>
    {
        public DerivedCompanyType GetCompany(string companyID)
        {
            DerivedCompanyType company = new DerivedCompanyType();
            // ...
            return company;
        }
    }
}

And initialization is changed as follows:

/// FirstSystemAPI.Tools
namespace FirstSystemAPI
{
    public static class Tools
    {
        public static ICompanyAPI<DerivedCompanyType> GetCompanyAPI()
        {
            return new CompanyAPI() as ICompanyAPI<DerivedCompanyType>;
        }
    }
}

When I rebuild the project it complains in three places about the same thing:

The type 'DerivedCompanyType' cannot be used as type parameter 'T' in the generic type or method 'Common.Interfaces.ICompanyAPI<T>'. There is no implicit reference conversion from 'DerivedCompanyType' to 'BaseCompanyType'.

Best regards!

PS I don't want to create hidden crossposting so please be informed that similar thread can be found here: http://www.dreamincode.net/forums/topic/332665-custom-generic-interface/

like 8 of your last block you are trying to convert an CompayAPI object into an ICompanyApI<DerivedCompayType> object. Is it complaining about that?

Thans for reply!
Yes, it is. Line 6 'GetCompanyAPI' gets warning and line 8 'DerivedCompanyType' gets error. Both warning and error have the same content about no implicit reference conversion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.