gpt4 book ai didi

c# - 界面架构设计建议

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:37 25 4
gpt4 key购买 nike

我有一个产品经理应用程序,我正在尝试使用界面进行设计。产品可以是打印或在线两种类型中的一种。我希望能够对其进行通常的 CRUD 操作。我正在使用 Entity Framework ,但在构建界面时遇到了障碍。

例如,我为接口(interface) AddProduct(Product productToCreate) 添加了我的第一个方法。由于我的实体来自 Entity Framework ,我应该为产品使用什么类型?因此,我创建了一个中间对象调用 Product 来将 Entity Framework 对象传输到其中,以便它适合界面。是对的吗?请指教。

class EFPrint
{
//Entity Framework object
public int PrintId { get; set; }
public string Product { get; set; }
public string Code { get; set; }
}

class EFOnline
{
//Entity Framework object
public int OnlineId { get; set; }
public string Product { get; set; }
public string Code { get; set; }
}

class Product
{
public int Id { get; set; }
public string Product { get; set; }
public string Code { get; set; }
}

interface IProductManagerService
{
void AddProduct(Product product);
}

class PrintService : IProductManagerService
{

public void AddProduct(Product product)
{
throw new NotImplementedException();
}
}

class OnlineService : IProductManagerService
{

public void AddProduct(Product product)
{
throw new NotImplementedException();
}
}

最佳答案

Entity Framework 允许您使用基类。您应该创建一个基础产品类,让 OnlineProduct 和 PrintProduct 继承自 Product:

public class Product { ... }
public class Online : Product { ... }
public class Print : Product { ... }

在您的 DbContext 中,不是为每种类型都有一个 DbSet,您应该为产品有一个 DbSet,并且只需添加访问器以便仅检索特定类型的产品:

public class ProductsContext : DbContext
{
public DbSet<Product> Products { get; set; }

public IQueryable<Online> OnlineProducts
{
get
{
return Products.OfType<Online>();
}
}

public IQueryable<Print> PrintProducts
{
get
{
return Products.OfType<Print>();
}
}
}

关于c# - 界面架构设计建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7669173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com