[英]Castle windsor - null object with Dependecy Injection of class that inherits from a generic base class
我在使用Castle.Windsor依賴注入時遇到問題。 我想在容器中向相關的Dao注冊所有服務層。 我還想獲得屬性注入而不是構造函數注入。 當我運行以下代碼時,我總是發現我的Dao對象為null。 當然,我在容器注冊方面做錯了。 我已經閱讀並嘗試了許多在網絡上找不到結果的解決方案。
服務示例:
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沒問題,但是注入不適用於通用dao。
我該如何解決這個問題?
提前致謝。
我的猜測與繼承有關...“ Generic Dao”是從“ Nongeneric Dao”繼承的……可能(沒有嘗試)造成流利的注冊混亂...由於繼承,第一次注冊可能還會注冊Generic dao。 。
也要注意WithServiceAllInterfaces,通常DefaultInterfaces就足夠了
我寧願對所有存儲庫使用通用接口。 IRepository,其中T:Entity:嘗試正確實現存儲庫模式,而不是一口氣注冊所有dao / repository
Classes.FromAssemblyNamed(“ COGE.Business”).BasedOn(typeof(IRepository <>)).WithService.DefaultInterfaces()。LifestyleTransient()
我看不到通用dao的代碼中的要點...您沒有利用通用dao /存儲庫模式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.