Problems with an NHibernate LINQ query in loosely coupled project

北战南征 提交于 2020-01-06 03:39:06

问题


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!!


回答1:


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:

  • How To Write A Repository
  • The Generic Repository



回答2:


I believe the call session.Query<IMyInterface>() is invalid because NHibernate is not aware of your intention that "some mapped class (implemented somewhere) is the right class to query".

To stay loosely coupled (new operator is a dependency) you should use factory pattern (DAO objects) responsible for creating (querying) concrete entities you need.



来源:https://stackoverflow.com/questions/7530464/problems-with-an-nhibernate-linq-query-in-loosely-coupled-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!