C # Interfaces and Shared Lists

I have an interface defined as:

namespace RivWorks.Interfaces.DataContracts
{
    public interface IProduct
    {
        [XmlElement]
        [DataMember(Name = "ID", Order = 0)]
        Guid ProductID { get; set; }
        [XmlElement]
        [DataMember(Name = "altID", Order = 1)]
        long alternateProductID { get; set; }
        [XmlElement]
        [DataMember(Name = "CompanyId", Order = 2)]
        Guid CompanyId { get; set; }
        ...
        [XmlElement]
        [DataMember(Name = "buttonPositionCSS", Order = 14)]
        string buttonPositionCSS { get; set; }
    }
}

I have a specific implementation, for example:

namespace RivWorks.Model.Objects
{
    [DataContract(Name = "Product", Namespace = "http://rivworks.com/DataContracts/2009/01/15")]
    public class Product : IProduct
    {
        #region Declarations
        private Guid _productID;
        private long _altProductID;
        private Guid _companyId;
        ...
        private string _buttonPositionCSS;
        #endregion

        #region IProduct Members
        public Guid ProductID { get { return _productID; } set { _productID = value; } }
        public long alternateProductID { get { return _altProductID; } set { _altProductID = value; } }
        public Guid CompanyId { get { return _companyId; } set { _companyId = value; } }
        ...
        public string buttonPositionCSS { get { return _buttonPositionCSS; } set { _buttonPositionCSS = value; } }
        #endregion
    }
}

I have another interface that is defined as:

namespace RivWorks.Interfaces.Services
{
    public interface IProductManager
    {
        #region Products
        IProduct GetProductById(Guid productId);
        List<IProduct> GetProductByCompany(Guid companyId);
        int SaveProduct(IProduct product);
        int DeleteProduct(Guid productId);
        #endregion
    }
}

I have a class defined as:

namespace RivWorks.Controller
{
    public class ProductManager : IProductManager
    {
        #region Declare Models
        private static RivWorks.Model.Negotiation.RIV_Entities _dbRiv = RivWorks.Model.Stores.RivEntities(AppSettings.RivWorkEntities_connString);
        private static RivWorks.Model.NegotiationAutos.RivFeedsEntities _dbFeed = RivWorks.Model.Stores.FeedEntities(AppSettings.FeedAutosEntities_connString);
        #endregion

        #region Products
        public IProduct GetProductById(Guid productId)
        {
            // deleted for simplicity sake
            return product;
        }
        public List<IProduct> GetProductByCompany(Guid companyId)
        {
            var company = (from a in _dbRiv.Company where a.CompanyId == companyId select a).First();
            var companyDetails = from a in _dbRiv.AutoNegotiationDetails where a.CompanyId == companyId select a;
            // ################################################## //
            List<IProduct> productList = new List<RivWorks.Model.Objects.Product>();
            // ################################################## //
            // deleted for simplicity sake
            return productList;
        }
        public int SaveProduct(IProduct product)
        {
            return 0;  // stub
        }
        public int DeleteProduct(Guid productId)
        {
            return 0;  // stub
        }
        #endregion
    }
}

I get this error at compile time:

It is not possible to implicitly convert the type 'System.Collections.Generic.List <RivWorks.Model.Objects.Product>' to 'System.Collections.Generic.List <RivWorks.Interfaces.DataContracts.IProduct>'

The system is a very service (WCF, WebOrb, etc.), and I would like to expose the interfaces as my contracts. I have a model and controller in .NET, and I use Services as Views (proxies) for third-party consumers (real view).

What am I missing or is something wrong?

+3
5

:

List<IProduct> productList = new List<RivWorks.Model.Objects.Product>();

To:

List<IProduct> productList = new List<IProduct>();

( List , , ):

List<RivWorks.Model.Objects.Product> productList = 
    new List<RivWorks.Model.Objects.Product>();

// Do some work here.

return productList.Cast<IProduct>().ToList();
+5

:

List<IProduct> productList = new List<RivWorks.Model.Objects.Product>(); 

List<IProduct> productList = new List<IProduct>(); 

, .

, .

+4

Product s, IProduct s. (, - IProduct , Product?)

List<IProduct> productList = new List<RivWorks.Model.Objects.Product>();

List<IProduct> productList = new List<IProduct>();
+4

, Generic List , IProduct, , . :

List<IProduct> = new List<Iproduct>();

.

This is something that can be done (under certain circumstances) only from .net Faramework version 4, when the framework implements joint and contravariance for common types.

+1
source

Generics in C # just don't work that way.

See here

If B is a subclass of A, C <B> is not a subclass of C <A>.

+1
source

Source: https://habr.com/ru/post/1523324/


All Articles