开发者

How do I access a view using Fluent NHibernate?

My web app uses half a dozen tables, each of which get populated when a user passes through the system. In order to do stats analysis I've written a database view to flatten these tables into a single view.

The view is working, however, I want to automate some tests around the view creation.

My idea to do this was to create a model/map and repository for the view - with list action only. My current implementation doesn't work.

This is my Repository:

namespace FunctionalTests.SpssView
{
    public class SpssRepository
    {
        private readonly ISessionManager _sessionManager;

        public SpssRepository(ISessionManager sessionManager)
        {
开发者_如何转开发            _sessionManager = sessionManager;
        }

        public IList<Spss> ListFromSpssView()
        {
            ICriteria criteria = _sessionManager.GetSession().CreateCriteria(typeof(Spss));
            return criteria.List<Spss>();
        }
    }
}

This is the model class:

namespace FunctionalTests.SpssView
{
    public class Spss
    {
        public virtual String StudentId { get; set; }
        public virtual String UPNSCN { get; set; }
        ...
    }
}

And the mapping:

namespace FunctionalTests.SpssView
{
    public sealed class SpssMap : ClassMap<Spss>
    {
        public SpssMap()
        {
            Id(x => x.StudentId).GeneratedBy.Assigned();
            Map(x => x.UPNSCN);
            ...
        }
    }
}

I'm not entirely confident in the ID mapping - as it is just read from the view?

This is my test:

    [Test]
    public void ShouldPopulateAndRetrieveFromSpssView()
    {   
        var mockSessionManager = new Mock<ISessionManager>();
        mockSessionManager.Setup(x => x.GetSession()).Returns(_session);

        var caseRepository = new CaseRepository(mockSessionManager.Object);
        var caseList = caseRepository.ListCases();
        Assert.That(caseList.Count, Is.EqualTo(2));

        var repository = new SpssRepository(mockSessionManager.Object);

        var spssList = repository.ListFromSpssView();

        Assert.That(spssList.Count, Is.EqualTo(2));
    }   

Note the case list code - I put that in there to make sure the db connection was being made. This part of the test passes.

Running select * from spss; returns two results. (I'm using sql server 2005 fwiw)

And because this isn't production code, I created a new folder in my FunctionalTests visual studio project (I mention this, because it seems to me to be one of the main differences between this and my working repositories.) Should this make a difference??

Is it possible to test views like this? Is there anyway I can see the sql that is being generated? What am I doing wrong??!?

Thanks :)


Try adding:

    public SpssMap()
    {
       Table("myViewBame"); // ADD THIS
        Id(x => x.StudentId).GeneratedBy.Assigned();
        Map(x => x.UPNSCN);
        ...
    }

In order to see the generated SQL add this:

.ShowSql()

For example:

Fluently.Configure().Database(
                        MsSqlConfiguration.MsSql2005
                            .ConnectionString(
                                ConfigurationManager.ConnectionStrings["my"].ConnectionString).ShowSql())
                        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyClass>())
                        .BuildSessionFactory();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜