将Quriable对象泄漏到上层

时间:2015-10-16 18:28:12

标签: entity-framework user-interface service repository data-access-layer

我有一个灵活的应用程序,用户可以:

    1. 按任意字段过滤
    1. 按任意多个字段排序。
    1. 因为它将在ASP.Net Site +一些Xamarin C#Apps中运行,我也会在其中进行分页。
    1. 对于网络性能,它会在将要显示的必填字段上发送投影。

因此,如果我在每个“服务”方法中包含参数“ UQueryConstraints ”,则可以发送过滤器表达式+ oderBy表达式+页码+字段的投影,供存储库使用,它将把它应用到DBContext,这是否会被视为域服务的数据泄漏?

如本图所示:        http://1drv.ms/1Ngi3Kn Layered project e.g:

通知:

  • “UQueryConstraints”,它不会泄漏任何“IQueryable”。
  • “AmbientDbContextLocator”,来自:

    <http://mehdi.me/ambient-dbcontext-in-ef6/>
    <https://github.com/mehdime/DbContextScope>
    

    公共课UIView {

    public static void Display()
    {
        object constraintsB = new UQueryConstraints<Car>().Filter(x => x.carNo <= 6).SortBy(x => x.eName).Page(1, 5);
        //.Projection( field1, field2, field3)
        Debug.WriteLine("---------------test CarModel -------------------");
        CarModel carModel1 = new CarModel();
        carModel1.printCars(constraintsB);
    
    }
    

    }

    公共类CarModel {

    private CarService _carService = new CarService();
    void printCars(UQueryConstraints<Car> constraints)
    {
        foreach ( c in _carService.getCarsList("", constraints)) {
            Debug.WriteLine("Reading from converted back: aName =" + c.aName + ", eName = " + c.eName);
        }
    
    }
    

    }

    公共类CarService {

    public IList<Car> getCarsList(string Text, UQueryConstraints<Car>    constraints)
    {
        object dbContextScopeFactory = new DbContextScopeFactory();
        object ambientDbContextLocator = new AmbientDbContextLocator();
    
        using (dbContextScope == dbContextScopeFactory.Create()) {
            //after creating the Scope:
            //1. create the repository
            //2. call repository functions
            object carRep = new CarRepository(ambientDbContextLocator);
            return carRep.getCarsList("", constraints);
        }
    
    }
    

    }

    public class CarRepository:URepositoryFramwork.URepository {     public CarRepository(IAmbientDbContextLocator contextLocator)     {         base.New(contextLocator);

    }
    
    public IList<Car> getCarsList(string Text, UQueryConstraints<Car> constraints)
    {
        object query = this.DataSet.Where(constraints.FilterExpression);
        //.Select(constraints._projection2)
    
        IList<Car> items;
    
        if (constraints == null) {
            items = query.ToList();
        } else {
            items = constraints.ApplyTo(query).ToList();
    
        }
    
        return items;
    }
    

    }

问候。

1 个答案:

答案 0 :(得分:0)

以下几点。

  • 根本不需要UQueryConstraints,您根本不需要在用户界面中进行任何过滤。

  • 我认为模型是需要从服务返回的东西所以我不会在UI层中创建CarModel然后将值推送到它,它对我没有意义。

  • 我在请求某些数据的服务上有一个方法,然后以某种形式或形式将其返回给用户界面。

  • 我将服务注入UIView

  • 我不明白为什么围绕上下文会有如此多的 noise ,为什么要在 getCarsList 中创建它? getCarList 应该是一个名为 RequestCars 的类,应删除存储库和服务,以支持command pattern中描述的内容。

    < / LI>

我根本不喜欢这里的整个抽象,对我来说似乎过于工程化,谁说IQueryable应该被抽象出来?它就像抽象语言/框架功能一样,而你应该抽象域功能,只有 必要时。

抽象第三方框架在某种程度上可以很好,但这不是其中一种情况。