﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Ayende Rahien commented on Dependency Injection in a Dynamic Environment</title><description>Tudor,
  
  
&gt; why would you create a Policy which is not valid? 
  
  
BuildPolicy builds a Policy instance from user's input, which is assume to be invalid by default.
  
  
&gt; I assume that the Repository should persist the whole UnitOfWork 
  
  
No, the repository just handles saving &amp; querying, the UnitOfWork handles the UoW's life cycle.
  
  
&gt; UnitOfWork.Current, Repository&lt;X&gt;, ValidatorRunner, are some sort of singletons. How did you unit-test the above code?
  
  
UoW &amp; Repository are static accessors to the container, they are mockable using the Auto Mocking Container.
  
ValidatorRunner is a instance property on the controller.
  
  
&gt; - I observed that when ValidatorRunner.IsValid(..) returns false, you call ValidatorRunner.GetErrorSummary(...). I would express this as: bool ValidateRunner.Validate(policy, Action&lt;String&gt; set_summary). 
  
  
ValidatorRunner comes from Castle.Component.Validation, and I peronally like this API approach. Yours is equally valid, so I guess it is a matter of taste.
</description><link>http://ayende.com/2732/dependency-injection-in-a-dynamic-environment#comment5</link><guid>http://ayende.com/2732/dependency-injection-in-a-dynamic-environment#comment5</guid><pubDate>Thu, 23 Aug 2007 19:38:23 GMT</pubDate></item><item><title>Ayende Rahien commented on Dependency Injection in a Dynamic Environment</title><description>I don't like mocking my entities in general, although it is the case that entities may also be domain services.
  
I had a long discussion about this with JP &amp; Udi at DevTeach, and I don't think that taking mocking to the entities is necessary in most cases.
</description><link>http://ayende.com/2732/dependency-injection-in-a-dynamic-environment#comment4</link><guid>http://ayende.com/2732/dependency-injection-in-a-dynamic-environment#comment4</guid><pubDate>Thu, 23 Aug 2007 17:21:30 GMT</pubDate></item><item><title>hammett commented on Dependency Injection in a Dynamic Environment</title><description>&gt; you still need some sort of dependencies management, if only to provide a way to handle this as applications grow.
  
  
Absolutely. I had ventured on that field myself, as I was disgusted with objects creating and configuring others. Ruby has Needle, Python has, well a lot of tools. 
</description><link>http://ayende.com/2732/dependency-injection-in-a-dynamic-environment#comment3</link><guid>http://ayende.com/2732/dependency-injection-in-a-dynamic-environment#comment3</guid><pubDate>Thu, 23 Aug 2007 15:16:23 GMT</pubDate></item><item><title>Tudor-Andrei Pamula commented on Dependency Injection in a Dynamic Environment</title><description>if the code is there for my "critique" ;)
  
- why would you create a Policy which is not valid? 
  
  
- I assume that the Repository should persist the whole UnitOfWork (all/or nothing). 
  
In that case I would say that the Repository should flush the UnitOfWork. (or probably I misunderstood something)
  
  
- I assume that UnitOfWork.Current, Repository&lt;X&gt;, ValidatorRunner, are some sort of singletons (static instantiations) How did you unit-test the above code?
  
From my experience, it is very hard to substitute such globals, usually I had to Singleton.ReplaceInstanceWithAnotherInstanceOnlyInTests(x), or to use a strange inheritance tree...
  
  
- I observed that when ValidatorRunner.IsValid(..) returns false, you call
  
ValidatorRunner.GetErrorSummary(...). I would express this as:
  
bool ValidateRunner.Validate(policy, Action&lt;String&gt; set_summary). 
  
With this, and given Scope has a method Scope.SetErrorSummary(string) I would call: 
  
if (! ValidatorRunner.Validate(policy, Scope.SetErrorSummary)) return false;
  
  
- I would write the code like this:
  
    public virtual bool AddNewPolicy() {
  
        using (RhinoTransaction transaction = UnitOfWork.Current.BeginTransaction()) {
  
            Policy policy = BuildPolicy();
  
  
            if (! ValidatorRunner.Validate(policy, Scope.SetErrorSummary)) {
  
                return false; 
  
            }
  
  
            if (! ValidateThatUserIsAuthorizedToSavePolicy(policy)) {
  
                return false;
  
            }
  
  
            bool valid_policy_added = AddValidPolicy(policy);
  
            if (valid_policy_added) {
  
                transaction.Commit();
  
                SetupRedirect(Policy);
  
            }
  
            return valid_policy_added;
  
        }
  
    }
  
  
regards,
  
tap.
</description><link>http://ayende.com/2732/dependency-injection-in-a-dynamic-environment#comment2</link><guid>http://ayende.com/2732/dependency-injection-in-a-dynamic-environment#comment2</guid><pubDate>Thu, 23 Aug 2007 13:11:46 GMT</pubDate></item><item><title>Colin Jack commented on Dependency Injection in a Dynamic Environment</title><description>Superb post and yeah I think you are dead right, you have to be clear which layer you are working on in terms of DI. I also agree that DI into the domain is a bad idea, as you say the domain classes should be able to stand on their own.
  
  
Now here is a question I have. Lets say I have an Application layer service that calls to a repository, then to an infrastructure service, then into a domain service/entity and then returns. In that case I can inject in the repository and infrastructure service to do interaction testing of the Application layer service, but would you also inject the domain service/entity? There are very varying opinions on this but I'm interested to hear which way you approach this.
</description><link>http://ayende.com/2732/dependency-injection-in-a-dynamic-environment#comment1</link><guid>http://ayende.com/2732/dependency-injection-in-a-dynamic-environment#comment1</guid><pubDate>Thu, 23 Aug 2007 09:17:56 GMT</pubDate></item></channel></rss>