1

I'm using view classes for my templating. These view classes often require the Routing class to get URI's from other pages. You could actually say that the Routing should be available to every view class.

What I'm wondering how would you deal with the dependency injection. Injecting it through the constructor would mean that you will have to include it with every view class in many controllers. This would seem wrong to me.

You would also include it trough class extending but I don't think all views deserve a base class for just the routing library and it would able break the SOLID rules you do it because of a dependency that is not directly related to the child class.

My last option would be injecting it trough a public variable that injects it at a single place just before the template rendering.

How did you guys solve this problem? I'm looking for a clean non repetitive way.

  • 1
    Why should the view determine which URIs it links to? I would expect URIs to be data that the view uses. But then again I'm not doing web-programming. – Patrick Apr 1 '14 at 10:46
  • @Patrick the view does not determine the URI's they router would need to do that only the view does need to instruct the router what URI's it needs. – RJD22 Apr 1 '14 at 11:44
2

It sounds like you're describing a Cross-Cutting Concern.

Without knowing what exactly you mean by "clean, non-repetitive way" in the context of your problem domain, I can't really suggest any concrete solutions. Frameworks that enable AOP-like stuff such as DynamicProxy and PostSharp may address your issue, but again, I can't really say for sure without knowing exactly what you're looking for.

What I can say though is that if you have applied DI correctly and consistently, the dependency you speak of will be interchangable and the class which uses the dependency will be easy to unit test. In most scenarios, this makes the code sufficiently maintainable. So I wouldn't worry too much about the fact that a certain dependency recurs in multiple classes because that's OO programming for ya :)

  • But what if the dependency needs to be used in almost every View class? It really seems wrong to add it as a dependency to every constructor. – RJD22 Apr 1 '14 at 11:42
  • 1
    Unfortunately, I think in any case you won't be able to have your cake and eat it too due to the nature of the problem you describe; you will need to make a trade-off at some point. Off the top of my head, a second option would be to define a "BaseView" and declare a property into which you can inject a dependency, but that would have its own problem in that the dependency would be seen as optional. A third option would be to obtain the dependency through a static member of another class via a common factory method (so you can unit test it). – George Howarth Apr 1 '14 at 12:49
  • After some discussion I came to the conclusion that you just have to add the dependency to every view. I might not like it but sadly it's a fact I have to accept. That is why I gave you the +1 and correct answer. – RJD22 Apr 3 '14 at 8:25
0

I would create an interface IRoutingAware with method SetRouting that allows outside code to inject the routing instance. Then each view that implements this interface would have method called from the outside before the view is rendered. This would be even better if you had single place where the views are rendered, you just need to add two lines of code. In C#, the setting function could be implemented explicitly, which would make it obvious that the method shouldn't be called manually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.