0

This question can seem odd but we really do have such cases where we need to fix constructor signature.

For example I want it for dependencies resolving in DI. And if constructor is extended and requires more parameters some of which are quite context dependent I'm in trouble.
Sure DI container will throw exception if it cannot resolve the dependency but this is not the best way I suppose.

I just refactor our application and understand that what initially planned to be a good candidate for DI is broken now due to deliberate constructor extension without understanding the consequences. The only way to stop it is just the convention between developers.
Language support would be very helpful but both C# and Java don't have it.

So I think there should be some reason for this feature is not implemented...Why?

Here is how our factory looks like:

public class EntityBase : IEntity
{
   public EntityBase(IBusinessModel model, IAppContext context)
   {
   }
}

public class DescendatnEntity : EntityBase, IDescendatnEntity
{
   public DescendatnEntity(IBusinessModel model, IAppContext context, ...additional params...)
   {
   }
}

public interface IEntitiesFactory
{
     IEntity GetBaseEntity();
     IDescendantEntity GetDescendantEntity(...those additional params...); 
}

IBusinessModel and IAppContext singletons whereas additional params are transient.

In case where only caller knows the values of transient parameters there's no need for the factory to know about these params.

Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
  • If you bring the constructor in line, where would these classes ultimately get their dependencies? – Jon Feb 19 '13 at 19:34
  • 1
    What do you mean by *fixing constructor signature*? – Sergey Berezovskiy Feb 19 '13 at 19:34
  • 2
    Sounds like you just need to create a factory class that can be used to create your types. – Servy Feb 19 '13 at 19:36
  • 2
    @lazyberezovsky Perhaps something like `public void Foo() where T : new(string)` to say that the type has a constructor that takes a single string as an argument. – Servy Feb 19 '13 at 19:37
  • Why not have an interface that defines what you need and use that? Instead of using the constructor, make an `Initialize` method that takes in what you need. – zimdanen Feb 19 '13 at 19:37
  • @lazyberezovsky I mean fixing the set of input parameters. – Pavel Voronin Feb 19 '13 at 19:37
  • could you give an example code? – pascalhein Feb 19 '13 at 19:41
  • @Servy This what we have now (Factory) but we have to provide code for each constructed entity because it requires its own set of parameters. And if these parameters were injected via properties or methods factory would become very lite. – Pavel Voronin Feb 19 '13 at 19:41
  • @csharpler I added some code as example to make it more clear. It's how it looks now. – Pavel Voronin Feb 19 '13 at 19:51
  • See this standard response to "Why doesn't C# have this feature?" by Eric Lippert: http://stackoverflow.com/a/8673015/1127114 – Michael Liu Feb 19 '13 at 19:59
  • @MichaelLiu Well, treat my question as the curiosity about the costs. They are not eveident for me (not speaking of time and mony required for implementation). And yes, I think that freezing the way of object hierarchy construction has its benefits. – Pavel Voronin Feb 19 '13 at 20:14

1 Answers1

0

You could use constructor chaining to provide your default implementation.

public class DescendantEntity : EntityBase, IDescendatnEntity
{

   public DescendantEntity(IBusinessModel model, IAppContext context, ...additional params...)
   {
   }
   public DescendantEntity(IBusinessModel model, IAppContext context)
       this(model, context, ...additional params defaults...)
   {
   }
}
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
  • We already use it. The problem is not in default implementation. The problem is that factory has to know about these additional parameters for each resolved dependency. – Pavel Voronin Feb 19 '13 at 20:19
  • @voroninp If you don't want the factory to know about the dependencies then you might want to refactor the dependencies into another class. – Dustin Kingen Feb 19 '13 at 20:22
  • I want factory to know about the dependencies it can provide to the dependency under constraction. The mere problem is that developers just allow constructor to change by adding some parameters unrelated to the process of construction. Yes, I will have to refactor classes back to the same constructor. – Pavel Voronin Feb 19 '13 at 20:31