业务层在不知道的情况下处理Linq对象? [英] Business layer handling Linq-objects without knowing it?

查看:68
本文介绍了业务层在不知道的情况下处理Linq对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重构我的应用程序,以减少对数据源的依赖(此刻,我正在使用Linq To Sql)。但是我喜欢Linq To Sql从数据库中生成类的好方法以及它跟踪更改的方法。

I am refactoring my application to be less dependent on the data source (at the moment I am using Linq To Sql). But I like the nice way that Linq To Sql generates my classes out of my database and the way it tracks changes.

如果不是Linq,我只用一个接口包装该类,然后让我的业务层依赖该接口。但是,我无法使用linq做到这一点。

If this was not Linq I would just wrap the class with an interface and then let my business layer depend on that interface. However, I cannot do this with linq.

所以可以说我有一个存储库 IFooRepository 其中 Foos 是数据库中的一个表,Linq为我生成了 Foo 类。如果我不关心依赖关系,我只返回linq对象 Foo 。但是我的业务逻辑层没有我的Linq层的线索,因此我无法将那些对象注入到BLL中。而且由于我的Linq对象无法实现任何接口,因此我在IFooRepository中不能具有任何返回 IFoo 实现的函数。

So lets say I have a repository IFooRepository where Foos is a table in the database and Linq has generated the class Foo for me. If I wouldn't bother about dependencies I would just return the linq object Foo. But my Business Logic layer has no clue of my Linq layer so I cannot inject those objects into my BLL. And as my Linq objects cannot implement any interface I cannot have any functions in the IFooRepository returning an IFoo implementation.

因此,我想要的只是让我的BLL处理Linq对象,却不知道它,以便我的Linq层将跟踪BLL的更改。这可能吗?我正在考虑只让一个类从Linq对象继承,然后将其用于传递。

So, what I want is just for my BLL to handle the Linq objects but without knowing it so that my Linq Layer will track the changes of the BLL. Is this possible? I am thinking of just having a class inherit from the Linq object and then use that for passing around. Would that be valid?

推荐答案

您的LINQ-to-SQL实体可以实现接口,并且可以向它们添加任何功能,你喜欢。生成的实体是 partial 类,因此只要它是LINQ-to-SQL程序集的一部分,您就可以在另一个文件中执行类似的操作:

Your LINQ-to-SQL entities can implement interfaces, and you can add any functionality to them that you like. The generated entities are partial classes, so you can do something akin to this in another file, as long as it's part of your LINQ-to-SQL assembly:

public partial class YourEntityType : IInterfaceName
{
    // your implementation
}

您只需要在LINQ-to-SQL程序集和业务逻辑程序集都引用的另一个程序集中定义合同

You just need to define the contracts in another assembly that is referenced both by the LINQ-to-SQL assembly and the business logic assembly.

这篇关于业务层在不知道的情况下处理Linq对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆