2016-03-22 27 views
2

ASP.NET web formlarını kullanarak IoC konteynerini uygulamak istiyorum. Bu adımları tamamladım ediyorum: KernelIoC Container WebForms'ta çalışmıyor

public override IKernel CreateKernel() 
{ 
    IKernel kernel = new StandardKernel(new Module.Module()); 
    return kernel; 
} 
  • oluştur Ninject ve Ninject.Web ddl

  • public class Global : NinjectHttpApplication

  • yükleyin oluşturma

    1. Module

      012 Inject kullanma
      public override void Load() 
      { 
          Bind<IMetricService>().To<MetricService>(); 
      } 
      
    2. Page

      public partial class _Default : Page 
      { 
          [Inject] 
          private IMetricService metricService; 
      
          protected void Page_Init(object sender,EventArgs e) 
          { 
           metricService = new MetricService(metricService); 
          } 
      
          protected void Page_Load(object sender, EventArgs e) 
          { 
           metricService.GetAllMetrics(); 
          } 
      } 
      

    üzerinde Ve bu IoC konteyner bu MetricService bağlamak gerekir MetricService yapıcısındaki IMetricService geçerken Anlıyorum gibi benim MetricService sınıf

    public class MetricService : IMetricService 
    { 
         [Inject] 
         private IMetricService _metricService; 
    
         public MetricService(IMetricService metricService) 
         { 
          this._metricService = metricService; 
         } 
    
         public void GetAllCriteria() 
         { 
          _metricService.GetAllCriteria(); 
          Console.WriteLine("metric service"); 
         } 
    } 
    

    olduğunu sınıf. Sanırım hatam genel ama nerede olduğunu anlayamıyorum.

  • +0

    DI fikri var yukarı newing olduğunuzu PageInit içinde, konkresyonları bağlı kalmamak olduğunu atılır Bir bağımlılık? – Magrangs

    +0

    In ** PageInit ** ne yapmam gerekiyor? –

    +0

    İlk önce metricService = yeni MetricService (metricService); Kapsayıcı size doğru örneği sağlayacaktır. – Magrangs

    cevap

    3

    Inject özniteliği ile ortak özellikleri kullanmanız gerekir, böylece görülebilirler. Ayrıca, MetricService sınıfının somut bir uygulamasına güvenmeyin. Hizmeti tüketen sınıf sadece soyutlanmış bir uygulamaya dayanmalıdır (arayüz, bu durumda IMetricService).

    public partial class _Default : Page 
    { 
        [Inject] 
        public IMetricService metricService { get; set; } 
    
        protected void Page_Load(object sender, EventArgs e) 
        { 
         metricService.GetAllMetrics(); 
        } 
    } 
    

    Ve metrik hizmetin kendisinin bir örneğine ihtiyacı yoktur. Bu sadece bir felaket tarifi. MetricService sınıfınızı değiştirin, böylece özyineleme yoluyla kendini arama ihtiyacı olmadan tüm kriterleri alır.

    public class MetricService : IMetricService 
    { 
        public void GetAllCriteria() 
        { 
         //this is where you call out to your database 
         //or do whatever else you need to do to return the criteria 
        } 
    } 
    

    Ayrıca, ben GetAllCriteria şey dönmek gerekiyordu olduğunu götürün? Bu genellikle "get" öneki ile başlayan yöntemler anlamına gelir. Bu nedenle, dönüş türünü void'dan döndürdüğünüz türe değiştirmeniz gerekir.

    0

    Bu şekilde yapabilir miyim? Ben

    private static void RegisterServices(IKernel kernel) 
         { 
          kernel.Bind<IMetrics>().To<Metrics>(); 
         }  
    

    sahip

     [Inject] 
         private IMetrics metrics; 
    
         protected void Page_Init(object sender, EventArgs e) 
         { 
          metrics = new Metrics(metrics); 
         } 
    

    Bu benim Metrik sınıf

    public class Metrics : IMetrics 
        { 
         private IMetrics metrics; 
    
         public Metrics(IMetrics metrics) 
         { 
          this.metrics = metrics; 
         } 
        } 
    

    Ve ben page_ınit metriği koyduğunuzda Ninject Konteyner gerçek Metrik sınıf koymak ya da ben kullanıcı özelliğine ihtiyaç Bunu yapmak için .Bu fikir, bu IMetri * interfa sınıfında Page ve Metrik sınıfındadır. Ce, bir geçerli durum'a sahip olmak için.Ben bu şekilde İstisna üzerinde yapmak


     [Inject] 
         public IMetrics metrics { get; set; } 
    
         protected void Page_Init(object sender, EventArgs e) 
         { 
          metrics = new Metrics(metrics); 
         } 
    

    Error activating IMetrics using binding from IMetrics to Metrics 
    A cyclical dependency was detected between the constructors of two services. 
    Activation path: 
    3) Injection of dependency IMetrics into parameter metrics of constructor of type Metrics 
    2) Injection of dependency IMetrics into property metrics of type _Default 
    1) Request for default_aspx 
    
    Suggestions: 
    1) Ensure that you have not declared a dependency for IMetrics on any implementations of the service. 
    2) Consider combining the services into a single one to remove the cycle. 
    3) Use property injection instead of constructor injection, and implement IInitializable 
        if you need initialization logic to be run after property values have been injected.