我遇到了Castle.Windsor依賴注入的問題。 我想在容器中註冊與相關的Dao相關的所有服務層。我也想獲得Propery Injection而不是Constructor injection。當我運行下面的代碼時,我總是發現我的Dao對象是null。當然,我在容器註冊方面做錯了什麼。我已閱讀並嘗試了許多我在網絡上找到的解決方案,但沒有結果。Castle windsor - 依賴關係的空對象從普通基類繼承的類的注入
服務實例:
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需要繼承,需要非一般的一個通用基礎類等。
要在容器註冊我做了以下內容:
// 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 container.Register(Classes.FromAssemblyNamed(「COGE.Business」)。BasedOn(typeof(BaseDao))。WithServiceAllInterfaces()。LifestyleTransient )); ()); //註冊通用道 container.Register(Classes.FromAssemblyNamed(「COGE.Business」)。BasedOn(typeof(BaseDao <>))。WithServiceAllInterfaces()。LifestyleTransient()); – Giandomenico