加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Linq to SQL中的DAO工厂

发布时间:2020-12-15 21:32:50 所属栏目:百科 来源:网络整理
导读:我正在努力创造以下情况: 首先,有几个Linq to SQL Table对象几乎完全相同.我们称之为products_something,products_somethingElse等. 其次,有一个接口可以完成Products DAO的所有常用方法.首先,我试图为所有产品只创建一个DAO,因为我可以处理它在另一个层(DA
我正在努力创造以下情况:

首先,有几个Linq to SQL Table对象几乎完全相同.我们称之为products_something,products_somethingElse等.

其次,有一个接口可以完成Products DAO的所有常用方法.首先,我试图为所有产品只创建一个DAO,因为我可以处理它在另一个层(DAL)上的差异.但是由于Linq to SQL需要强类型引用,我最终会为每种类型的产品使用一个DAO.我真的不知道是否可以做我之前提到过的事情.

最后,我有一个ProductsDaoFactory,它根据客户端(用户视图)选择??实例化正确的ProductDao.由于我不知道在运行时选择哪种类型的产品,我创建了一个通用工厂来处理它.

这是代码:

public enum ProductEnum
    {
        SimpleProduct,ComplexProduct,RevisedProduct,BrokenProduct
    }

    public interface IProducts<T>
    {
        T GetProductById(int id);
        IQueryable<T> GetAllProducts(string product_name,DateTime product_age);
        //Several other common methods
    }



public static class ProductFactory
    {
      //This won't compile because i have to set what's the type of the DAO i want
        public static IProducts<T> GetProductDAO(ProductEnum product)
        {
            switch (product)
            {
                case ProductEnum.SimpleProduct:
                    return new SimpleProductDAO();
                case ProductEnum.ComplexProduct:
                    return new ComplexProductDAO();
                case ProductEnum.RevisedProduct:
                    return new RevisedProductDAO();
                case ProductEnum.BrokenProduct:
                    return new BrokenProductDAO();
                default:
                    break;
            }

            return null;
        }
    }

    public class SimpleProductDAO : IProducts<SimpleProductDAO>
    {

        public SimpleProductDAO GetProductById(int id)
        {
            //Implementation
        }

        public IQueryable<SimpleProductDAO> GetAllProducts(string product_name,DateTime product_age)
        {
            //Implementation
        }
    }

问题是:我无法定义工厂方法的返回类型,因为它是通用的.我不得不把它的类型传给工厂,这只会破坏工厂的想法.那么,我如何创建一个实例化接口的泛型类型的工厂方法呢?

解决方法

您的问题是您的通用不是通用的.所以你的方法根本不应该是通用的,因为我不能说:

GetProductDAO<int>(someEnum);

我相信从工厂中删除通用不仅可以解决问题,还可以为API用户提供更好的清晰度和一致性.话虽如此,仿制药确实可以提供更好的智能感知.

我最喜欢的解决方案是删除枚举,只是传递泛型类型,你将添加对方法的限制.

public static IProducts<T> GetProductDAO<T>() where T: ISomeMarkerInterface,new()

所以SimpleProductDAO实现了ISomeMarkerInterface,它只是一个空接口:

public interface ISomeMarkerInterface
{

}

工厂变小:

public static class ProductFactory
{
    public static IProducts<T> GetProductDAO<T>() where T : ISomeMarkerInterface,IProducts<T>,new()
    {
        return new T();
    }
}

另外

将工厂定义为:

public static IProducts<T> GetProductDAO<T>(ProductEnum product)
{
    ...

这不是我最喜欢的解决方案,因为它可以在没有限制的情况下被滥用

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读