我遇到了 Castle.Windsor 依赖注入的问题。我想用相关的 Dao 在容器中注册我所有的服务层。我还想获得 Propery Injection 而不是 Constructor injection。当我运行以下代码时,我总是发现我的 Dao 对象为空。当然,我在容器注册方面做错了。我已经阅读并尝试了许多我在网上找到的解决方案,但没有结果。
服务示例:
public class DummyBLL : IDummyBLL
{
public IDelegaDao delegaDao { get; set; }
public IUtenteDao utenteDao { get; set; }
public IFunzioneDao funzioneDao { get; set; }
public void dummyMethod(String key)
{
//Business logic that make use of the dao objects
}
...
}
道例:
public class BaseDao<T> : BaseDao where T : Entita
{
public BaseDao()
{
Session = NHibernateHelper.CurrentSession;
}
public BaseDao(ISession session)
{
this.Session = session;
}
}
public class BaseDao
{
public ISession Session { get; protected set; }
public BaseDao()
{
SearchFields = new List<string>();
Session = NHibernateHelper.CurrentSession;
}
public BaseDao(ISession session)
{
if (session != null)
{
Session = session;
}
else
{
Session = NHibernateHelper.CurrentSession;
}
SearchFields = new List<string>();
}
}
public interface IFunzioneDao
{
IEnumerable<COGE.Business.ObjectModel.Funzione> CercaFunzioniPerUtente(Guid idUtente);
IEnumerable<COGE.Business.Data.Dto.FunzioneDto> GetAllFunzioni();
}
public class FunzioneDao : BaseDao<Funzione>, IFunzioneDao
{
public FunzioneDao() { }
public FunzioneDao(ISession session): base(session){}
public IEnumerable<FunzioneDto> GetAllFunzioni()
{
var funzioni = Session.QueryOver<Funzione>()
.OrderBy(x => x.Categoria).Asc
.ThenBy(x => x.Descrizione).Asc
.List();
return funzioni.Select(x => x.ToDto());
}
public class TgcppdcDao : BaseDao, ITgcppdcDao
{
private IDbConnection connessione = null;
private ISession session = null;
private static readonly ILog Log = LogManager.GetLogger(typeof(TgcppdcDao));
public TgcppdcDao()
{
}
public TgcppdcDao(ISession session)
: base(session)
{
}
我有一些需要继承通用基类的 dao 和其他需要非通用基类的 dao。
要在容器中注册,我正在执行以下操作:
// service registration
container.Register(Classes.FromAssemblyNamed("COGE.Business").InNamespace("COGE.Business.BLL").WithServiceFirstInterface().LifestyleTransient());
//to register the non generic dao
container.Register(Classes.FromAssemblyNamed("COGE.Business").BasedOn(typeof(BaseDao<>)).WithServiceAllInterfaces().LifestyleTransient());
//to register generic dao
container.Register(Classes.FromAssemblyNamed("COGE.Business").BasedOn(typeof(IBaseGenericDao<>)).WithServiceAllInterfaces().LifestyleTransient());
我对非通用 dao 没有问题,但注入不适用于通用 dao。
我该如何解决这个问题?
提前致谢。