在松耦合的项目与NHibernate的LINQ查询问题 [英] Problems with an NHibernate LINQ query in loosely coupled project

查看:105
本文介绍了在松耦合的项目与NHibernate的LINQ查询问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望能帮到你!

我已经开始在MVC 3的项目,并设置在另一个组装业务领域模型,接口定义此组件和将使用它的所有项目之间的合同。我使用Ninject注入的依赖关系到项目中。我在一个特定的LINQ查询的那一刻撞了南墙。

 公开的IEnumerable< ITheInterface> DoMyQuery()
    {
        使用(ISession的会话= _sessionFactory.OpenSession()){
            VAR的查询=(
                从C在session.Query< IMyInterface的>()
                那里的东西== anotherthing
                通过新的{} c.TheGrouper c组到GRP
                选择新IMyInterface的(){
                    属性= grp.Key
                }
            );            返回query.ToList();
        }
    }

现在很明显,我不能实例化一个接口,但是这是我的问题!围绕它的唯一方法就是实例化具体类,但打破我被松耦合的规则。有没有其他人碰到了在此之前?

我想我的问题是,我怎么使用使用接口,而不能具体类?中选择新的对象在LINQ查询

请注意:仅仅是为了记录,即使我使用我的具体类只得到它的工作,我得到的NHibernate的错误无法解析属性:关键:......但那是另一个问题<。 / p>

任何帮助AP preciated !!


解决方案

只需使用接口和DI容器并不意味着你编写松耦合code。接口应在应用程序中使用 的接缝处,而不是实体:


  

一个缝在那里你可以在你的程序改变行为的地方
  没有在那个地方修改


马克李约瑟:


  

...我们想改变这个code中的工作方式在特定的范围内,但我们
  不想改变它在那个地方,因为它需要保持的方式
  它是在其他情况下使用时


实体(域对象)是您的应用程序的核心。当你改变他们,你改变他们的。然而,在你的数据访问code栋Seam是一个非常好的主意。它使用的模式实现。 LINQ,ICreteria,HQL是只是实现的细节是从背后的消费者领域驱动库接口隐藏。一旦你暴露你的项目将连接到他们的这些数据访问技术之一,将很难测试。请看看这两篇文章和<一个href=\"http://stackoverflow.com/questions/7513681/mocking-out-nhibernate-queryover-with-moq/7518039#7518039\">this和<一个href=\"http://stackoverflow.com/questions/7322434/iqueryable-repositories-take-2/7323598#7323598\">this答案:

hope you can help!

I've started a project in MVC 3 and set the business domain model in another assembly, the interfaces define the contract between this assembly and all projects that will use it. I'm using Ninject to inject the dependencies into the project. I've hit a brick wall at the moment with a specific LINQ query.

    public IEnumerable<ITheInterface> DoMyQuery()
    {
        using (ISession session = _sessionFactory.OpenSession()) {
            var query = (
                from c in session.Query<IMyInterface>()
                where something == anotherthing
                group c by new { c.TheGrouper } into grp
                select new IMyInterface() {
                    Property = grp.Key
                }
            );

            return query.ToList();
        }
    }

Now obviously I can't instantiate an interface, but this is my problem! The only way around it is to instantiate the concrete class, but that breaks my rules of being loosely coupled. Has anyone else ran into this before?

I suppose my question is, how do I use "select new Object" in a LINQ query by using the interface and NOT the concrete class?

Note: just for the record, even if I do use my concrete class to just get it to work, I get an NHibernate error of "Could not resolve property: Key of: "... but that's another issue.

Any help appreciated!!

解决方案

Just using interfaces and DI container does not mean that you writing loosely coupled code. Interfaces should be used at application Seams, not for Entities:

A seam is a place where you can alter behaviour in your program without editing in that place

From Mark Needham:

... we want to alter the way that code works in a specific context but we don’t want to change it in that place since it needs to remain the way it is when used in other contexts.

Entities (domain objects) are the core of your app. When you change them you change them in place. Building Seam around your data access code however is a very good idea. It is implemented using Repository pattern. Linq, ICreteria, HQL is just an implementation detail that is hidden from consumers behind domain driven repository interface. Once you expose one of these data access technologies your project will be coupled to them, and will be harder to test. Please take a look at these two articles and this and this answers:

这篇关于在松耦合的项目与NHibernate的LINQ查询问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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