1

我遇到了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()); 

我沒有問題與非通用刀,但注射不與普通道的工作。

我該如何解決問題?

在此先感謝。

+0

**更新** 對不起!我發佈了一個錯誤的組件註冊代碼: //註冊非通用dao container.Register(Classes.FromAssemblyNamed(「COGE.Business」)。BasedOn(typeof(BaseDao))。WithServiceAllInterfaces()。LifestyleTransient )); ()); //註冊通用道 container.Register(Classes.FromAssemblyNamed(「COGE.Business」)。BasedOn(typeof(BaseDao <>))。WithServiceAllInterfaces()。LifestyleTransient()); – Giandomenico

回答

0

我的猜測與繼承有關......「通用道」從「非通用道」繼承...可能(沒有嘗試)創建流暢的註冊混淆... 第一次註冊可能還會註冊通用道數由於繼承...

也小心WithServiceAllInterfaces,通常DefaultInterfaces足夠

我寧願使用一個通用的接口,所有存儲庫。 IRepository其中T:實體:儘量正確地實現存儲庫模式,比你在一杆

Classes.FromAssemblyNamed( 「COGE.Business」)註冊所有的daos /庫 .BasedOn(typeof運算(IRepository <>) ) .WithService.DefaultInterfaces()。LifestyleTransient()

我沒有看到你的代碼中的通用道的點...你沒有利用通用的dao /存儲庫模式。

+0

@Giandomenico:現在我很好奇......繼承猜測是正確的,或者您是否更改了存儲庫模式之後的方法? – Crixo

+0

你的假設是對的。我選擇刪除非通用實現,並解決了這個問題。謝謝(抱歉,如果答案遲了) – Giandomenico

相關問題