Newer
Older
enum / Types.cs
@Derek Comartin Derek Comartin on 29 Jan 2024 1 KB Init
namespace TypesExample;

public abstract class Product(int id)
{
    public int Id { get; } = id;
}
public class Template(int id) : Product(id) { }
public class Ebook(int id) : Product(id) { }
public class OfflineCourse(int id) : Product(id) { }

public sealed class ProductHandler(ResourcesHelper resourcesHelper)
{
    public void DoStuff(Product product)
    {
        if (product is Template || 
            product is Ebook||
            product is OfflineCourse)
        {
            var fileName = resourcesHelper.GetDefaultDownloadFileName(product);
            var downloadUrl = resourcesHelper.GetDownloadUrl(product);
        }
    }
}

public sealed class ResourcesHelper
{
    public string? GetDownloadUrl(Product product)
    {
        if (product is Template || 
            product is Ebook||
            product is OfflineCourse)
        {
            // some code to do this...
            return "TODO: get the download URL";
        }

        return null;
    }

    public string? GetDefaultDownloadFileName(Product product)
    {
        if (product is Template || 
            product is Ebook||
            product is OfflineCourse)
        {
            // some code to do this...
            return "TODO: get the file name";
        }

        return null;
    }
}


public sealed record DownloadableResource(
    int Id,
    int ProductId,
    string DownloadUrl,
    string DefaultDownloadFilename);

public sealed class ProductHandler2(ResourcesHelper2 resourcesHelper)
{
    public void DoStuff(Product product)
    {
        var downloadableResource = resourcesHelper.GetDownloadable(product.Id);
        // do stuff with downloadable resource
    }
}

public sealed class ResourcesHelper2
{
    public DownloadableResource? GetDownloadable(int productId)
    {
        // TODO: go fetch this...
        return null;
    }
}