lundi 3 août 2020

How does generic template pattern?

I need some help with this code. I have a base class with a method with generic parameter. Because Catalog have diferent Config and Products between Driver1 and Driver2

public abstract class SyncDriver<T> where T : class, Catalog, new()
{ 
    public abstract void Configure(Dictionary<string, string> configValues);
    public abstract void SyncCatalog(T Catalog);
}

public abstract class Catalog
{
    public abstract ICatalogConfig Config { get; set; }

    public abstract List<IProduct> Products { get; set; }
}

public interface ICatalogConfig
{
}
 
public interface IProduct
{
}

public class Driver1 : SyncDriver<CatalogDriver1>
{
    public override void SyncCatalog(CatalogDriver1 Catalog){
    ...
    }
    
    public Driver1(){}
}

public class CatalogDriver1: Catalog
{
    [DataMember]
    public override CatalogConfigDriver1 Config { get; set; }

    [DataMember]
    public override List<ProductDriver1> Products { get; set; }

    public CatalogDriver1(){}
}

public class CatalogConfigDriver1: ICatalogConfig
{
    [DataMember]
    public string url { get; set; }

    [DataMember]
    public string password { get; set; }
}

public class ProductDriver1: IProduct
{
    [DataMember]
    public string id { get; set; }
        
    [DataMember]
    public string name { get; set; }
}

But when I want to instantiate Driver1 I have an error.

private static GetSyncDriver<Catalog> CreateBukiSyncDriver(Type t)
{
    if (t == typeof(DriverConfig1))
        return new Driver1(); // ERROR: Cannot implicitly convert type...
    else if (t == typeof(DriverConfig2))
        return new Driver12(); // ERROR: Cannot implicitly convert type...
    else return null;
}

Aucun commentaire:

Enregistrer un commentaire