我目前正在使用 MVC3、Spring.NET 和 FluentNHibernate 开始开发 ASP.NET 项目。
我的背景主要是java,但是有一些Spring框架的接触,所以Spring.NET应该很容易,peasy,对吧?
应用程序架构相当普通,控制器使用服务,服务使用 DAO,DAO 使用 NHibernate 实体和映射。
目前,我对当前的情况感到头疼:
我的一种服务方法使用了一个 Dao,它是使用 spring.net 注入的。当我使用 [Transaction] 属性注释服务方法时,DAO 依赖项将停止注入我的服务,从而导致 NullReferenceExceptions。
任何关于为什么会发生这种情况的想法将不胜感激。
一些代码来说明问题。这不是实际的代码,但非常接近。首先是抽象测试类,它扩展了 spring 类 AbstractTransactionalSpringContextTests :
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Namespace.Dao;
using Namespace.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Spring.Testing.Microsoft;
namespace Namespace.Tests.Services
{
[TestClass]
public class AbstractServiceTest : AbstractTransactionalSpringContextTests
{
public AbstractServiceTest()
{
}
protected override string[] ConfigLocations
{
get { return new string[]
{
"assembly://Assembly/Namespace.Config/dao.xml",
"assembly://Assembly/Namespace.Config/Services.xml",
"assembly://Assembly/Namespace.Config/web.xml"
};
}
}
}
}
扩展此类以创建实际的测试类:
using System.Collections.Generic;
using Namespace.Entities;
using Namespace.Services.Assets;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Namespace.Tests.Services
{
[TestClass]
public class AssetsServiceTest : AbstractServiceTest
{
public AssetsServiceTest()
{
}
private AssetsService assetsService;
public AssetsService AssetsService
{
get { return assetsService; }
set { assetsService = value; }
}
[TestMethod]
public void TestGetFacilities()
{
Assert.IsNotNull(assetsService);
/* THIS ASSERTION FAILS when assetsService.GetFacilities has the [Transaction] attribute */
Assert.IsNotNull(assetsService.FacilityDao);
IList<Facility> facilities = assetsService.GetFacilities();
Assert.IsNotNull(facilities);
}
}
}
这是服务类:
using System.Collections.Generic;
using Namespace.Dao;
using Namespace.Entities;
using Namespace.Models;
using Spring.Transaction;
using Spring.Transaction.Interceptor;
using Facility = Namespace.Entities.Facility;
namespace Namespace.Services.Assets
{
public class AssetsService
{
public AssetsService()
{
System.Console.Out.WriteLine("AssetsService created");
}
private FacilityDao _facilityDao;
public FacilityDao FacilityDao
{
get
{
return _facilityDao;
}
set
{
_facilityDao = value;
}
}
[Transaction(TransactionPropagation.Required, ReadOnly = true)]
public IList<Facility> GetFacilities()
{
return _facilityDao.FetchAll();
}
}
}
最后是浓缩和合并的 web.xml/applicationContext :
<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="http://www.springframework.net">
<db:provider id="DbProvider"
provider="System.Data.SqlClient"
connectionString="Data Source=localhost;...;"/>
<object id="transactionManager"
type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate32">
<property name="DbProvider" ref="DbProvider"/>
<property name="SessionFactory" ref="SessionFactory"/>
</object>
<tx:attribute-driven transaction-manager="transactionManager"/>
<object type="Namespace.Dao.FacilityDao, Namespace" id="FacilityDao">
<property name="SessionFactory" ref="SessionFactory"/>
</object>
<object type="Namespace.Services.Assets.AssetsService, Namespace" id="AssetsService">
<property name="FacilityDao" ref="FacilityDao" />
</object>
</objects>
编辑:
感谢 Max 和 Marijn 的提示,我现在已将 AssetsService 更改为使用接口而不是实际实现。但是,问题仍然存在。以下是修改后的细节。
namespace Namespace.Services.Assets
{
public class AssetsService
{
public AssetsService()
{
System.Console.Out.WriteLine("AssetsService created");
}
public IFacilityDao FacilityDao { get; set; }
[Transaction(TransactionPropagation.Required, ReadOnly = true)]
public IList<Facility> GetFacilities()
{
return FacilityDao.FetchAll();
}
}
}
IFacilityDao:
namespace Namespace.Dao
{
public interface IFacilityDao : IDao<Facility>
{}
public class FacilityDao : DaoBase<Facility>, IFacilityDao
{
}
}
伊道:
namespace Namespace.Dao
{
public interface IDao<T>
{
T Fetch(int id);
T FindByName(string name);
IList<T> FetchAll();
void SaveOrUpdate(T entity);
void Delete(T entity);
}
}
道基地:
namespace Namespace.Dao
{
public abstract class DaoBase<T> : IDao<T>
{
public ISessionFactory SessionFactory { get; set; }
public T Fetch(int id)
{
var result = SessionFactory
.GetCurrentSession()
.CreateCriteria(typeof(T))
.Add(Restrictions.Eq("ID", id))
.UniqueResult<T>();
return result;
}
//....
}
}