While you have done your very best to try to find and edge case I
think even this can be done without injecting to the entity. I have
found very very few circumstances where an entity needed a
non-transient dependency to something. The ones that I have found tend
to be infrastructure type cross cutting concerns (logging as an
example).

Use a builder (nested class) and a private constructor... give the
builder the service and have the service use the array overload that
is on the private constructor. This is the point of the builder
pattern.

The only thing that you can say is wrong with that is that if there is
a problem your coworkers will change the visibility on the constructor
and then call it directly but frankly you have bigger problems at that
point and should probably be seeking a competent team elsewheret.

Greg

On Thu, Mar 26, 2009 at 4:30 AM, Peter Morris <[email protected]> wrote:
>
>> Fabio gives the answer but do you really need to do it?
>
> Not sure :-)
>
>
> I have a Song class which has a Name property.  When the user searches for a
> song I want to list songs with a similar name to what they entered rather
> than the exact string.  To achieve this I am considering doing something
> like
>
> 1: Remove all characters so I only have consonants (except for first letters
> of words)
> 2: Remove all adjacent repeating letters
> 3: Convert to lower case
> 4: For each remaining word calculate a 32bit hash code
>
> For Song I will then have an association
>    Song 1---- *WordHash
>
> So
>    "My bonny lies over the occean"
> would become
>
>    my = 4622
>    bny = 6732
>    ls = 3623
>    ovr = 3415
>    th = 4312
>    ocn = 1542
>
> (Fake hash codes)
>
> Anyway, the point is that I can use a service to convert a string into a
> collection of Int32 which I can use to create the associated hashes when
> creating the song and also when searching for songs which might have a
> similar name.  I want to use a service obviously because it is used in 2
> places (create song, find song).
>
> Now my initial thought was to create a constructor for Song like so
>
>    public Song(string name, int[] wordHashes)
>    {
>        ...
>    }
>
> and have a higher layer which creates the song also pass in the wordHashes,
> but I am a bit of a control freak when it comes to code and this feels like
> it is open to some other developer calling the constructor with the wrong
> word hashes (I work with developers who would do this deliberately if it was
> a simple solution to an isolated problem).  So another thought was
>
>    public Song(string name, IWordHashService wordHashService)
>    {
>        ...
>    }
>
> but Unity IoC wont let me specify constants to pass to a constructor so
> that's not an option (fake example)....
>    IoC.Resolve<Song>(new { Name = "My Bonnie lies over the occean" })
>
>
>
> So then the next option
>
>    public Song(string name)
>    {
>        ...
>    }
>
>    [InjectionMethod]
>    public void InjectDependencies(IWordHashService wordHashService)
>    {
>        ...
>    }
>
> Obviously no good because the constructor will try to set the name before I
> have the service.
>
>
> Final option I can think of
>
> public class Song
> {
>    ....
>    IWordHashService WordHashService;
>
>    public Song()
>    {
>    }
>
>    [InjectionMethod]
>    public void InjectDependencies(IWordHashService wordHashService)
>    {
>        WordHashService = wordHashService;
>    }
>
>    private string name;
>    public string Name
>    {
>        get { return name; }
>        set
>        {
>            name = value;
>            //Calculate word hashes
>        }
>    }
> }
>
> This would work and is the kind of thing I would normally do, but in my
> current ORM I can mark a property so that it can no longer be updated once
> persisted so I can use this approach and still make Name immutible.  In NH I
> am trying to make my classes completely persistence ignorant so I need an
> OOP way to make Name immutible, which is why I wanted to go for the Name in
> the constructor, although I suppose I could throw an exception if you try to
> set Name when it already has a value.
>
> I'm looking forward to seeing some opinions, and maybe some alternative
> ideas.
>
>
> Pete
> ====
> http://mrpmorris.blogspot.com
>
>
> >
>



-- 
It is the mark of an educated mind to be able to entertain a thought
without accepting it.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to