Inject the container? .AddComponentInstance(typeof(IWindsorContainer),container);
Tuna Toksöz Eternal sunshine of the open source mind. http://devlicio.us/blogs/tuna_toksoz http://tunatoksoz.com http://twitter.com/tehlike On Wed, May 13, 2009 at 7:00 AM, dnagir <[email protected]> wrote: > > Hi, > > In my model I want to use services injected from an application (Win, > Web, Service). > > A small example. Let's say I have User class with Username, Password > (private) and Set/CheckPassword method. > > The Set/CheckPassword methods should use IPasswordEncryption service. > So depending on the implementation it should store password as clear > text, hash etc. > > So I implement the method (on User class) similar to this: > public void SetPassword(string newPassword) { > var service = (IPasswordEncryption)ModelInjection.GetContainer() > .Resolve(typeof(IPasswordEncryption)); > this.Password = service.EncryptPassword(newPassword); > } > > But I'm not sure how to implement GetContainer method. Actually where > should I obtain IWindsorContainer instance from? > Static variable? ThreadStatic? > > I don't want my model to know where itself is going to be used. > Currently I implement ModelInjection.GetContainer() like this? But I > don't like how it smells and, on the other hand, I don't see a better > way. > > public static class ModelInjection { > [ThreadStatic] > private IWindsorContainer container; > > public static IWindsorContainer GetContainer() { > if (container == null) > throw new InvalidOperationException("The container is not > injected from consumer. Please do it."); > return container; > } > > public static SetContainer(IWindsorContainer newContainer) { > if (newContainer == null) > throw new ArgumentNullException("newContainer"); > if (container != null) > throw new NotSupportedException("The container has already been > injected. We do not want to support its replacement now."); > container = newContainer; > } > } > > Then in my application I can do ModelInjection.SetContainer (from > Application_BeginRequest in Web or anywhere in Win). > > Is this a good approach? Some suggestions please? > > Thanks, > Dmitriy. > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Castle Project Users" 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/castle-project-users?hl=en -~----------~----~----~----~------~----~------~--~---
