Five W DI, IoC because it makes my brain explode

So Inversion Of Control is an undefined description, so Dependency Injection has become a new definition. This is a very, very powerful solution and, quite possibly, also confuses someone who has never encountered this.

So in my search, so as not to be a deer in the headlights, I read. I found some awesome books and online posts. But, like all wonderful things, more questions arose than answers.

A problem that absorbs variables from an unknown project that will be implemented after they are implemented in my project.

Decision:

public interface ISiteParameter
{
    Guid CustomerId { get; set; }
    string FirstName { get; set; }
    string LastName { get; set; }
    string Phone { get; set; }
}

My injector:

public interface IInjectSiteParameter
{
     void InjectSite(ISiteParameter dependant);
}

Then I created this:

public class SiteContent : IInjectSiteParameter
{
    private ISiteParameter _dependent;

    #region Interface Member:
    public void InjectSite(ISiteParameter dependant)
    {
        _dependant = dependant;
    }
    #endregion
}

Then, to implement it using the Shared Reference for Fed Variables, I created a class that will be implemented as follows:

public class SiteParameters : ISiteParameter
{    
   public Guid Customer Id { get; set; }
   public string FirstName { get; set; }
   public string LastName  { get; set; }
   public string Phone { get; set; }
}

SiteParameters Class ; , :

ISiteParameter i = new ISiteParameter();
MessageBox.Show(i.Guid + i.FirstName + i.LastName + i.Phone);

, , ... ?

? , , ​​ , , ?

?

+5
2

4 .NET Mark Seemann

DI. , . , .

, , , , . , , , ASP.NET.

, . , , , , .

+3

depdendencies, .

:

public interface IWebHost 
{ 
   string DoSomething(string input); 
} 

public class FooManager 
{
   private IWebHost _host; 
   public FooManager(IWebHost host)
   {  
      _host = host; 
   } 

   public void Process()
   { 
      // do something with _host 
   }
} 

, :

  • , .
  • Dependency Injection .

, , , Factory, .

:

public interface IConnection : IDisposable 
{
   string DoSomething(string input); 
   // implement IDisposable
}

public interface IConnectionFactory 
{
   IConnection CreateConnection(); 
} 

public class DerpConnection : IConnection 
{
    // implementation
} 

public class DerpConnectionFactory : IConnectionFactory 
{
  // We only return DerpConnections from this factory.  

  IConnection CreateConnection() { return new DerpConnection(); } 
}

public class BarManager 
{ 
   private IConnectionFactory _connectionFactory; 
   public BarManager(IConnectionFactory connectionFactory)
   {   
      _connectionFactory = connectionFactory;
   } 

   public void Manage()
   {
      using(var connection = _connectionFactory.CreateConnection()) 
      { 
        // do something here. 
      } 
   }
}

DI IConnectionFactory, Factory DerpConnection. Factory IConnection.

, , - , , ( SRP)

+1

All Articles