That's expected behavior. You have two dependencies on IView:

one via .ctor
second via settable property

Transient means a new object will be created to satisfied each of those.

It seems you want to make the property read only.

Krzysztof


On 26 January 2011 16:26, Ron <[email protected]> wrote:

> Hello,
>
> I'm trying to use Castle.Windsor as part of prism.
> I've encounterd an issue related to my usage of windsor for resolving
> views and view models.
> I have the ViewModel that gets a reference to a view in the
> constructor.
> I see that the view's constructor gets called twice instead of once,
> if I don't use singleton registration.
>
> Is this a bug? Am I doing something wrong?
> Any suggestions please?
>
> Thanks
> Ron
>
> Here's a simplified version of the code:
>
> using System;
> using Castle.Core;
> using Castle.MicroKernel.Registration;
> using Castle.Windsor;
>
> namespace WindsorIssue
> {
>    public interface IViewModel
>    {
>        IView View { get; set; }
>    }
>
>    public class ViewModel : IViewModel
>    {
>        public IView View { get; set; }
>
>        public ViewModel(IView view)
>        {
>            View = view;
>            Console.WriteLine("In ViewModel constructor");
>        }
>    }
>
>    public interface IView
>    {
>    }
>
>    public class View : IView
>    {
>        public View()
>        {
>            Console.WriteLine("In View Constructor");
>        }
>    }
>    class Program
>    {
>        private WindsorContainer Container { get; set; }
>        static void Main(string[] args)
>        {
>            Program myProgram = new Program {Container = new
> WindsorContainer()};
>
>            myProgram.RegisterTypeIfMissing<IView, View>(false);
>            myProgram.RegisterTypeIfMissing<IViewModel,
> ViewModel>(false);
>
>            IViewModel viewModel =
> myProgram.GetInstance<IViewModel>();
>            IView view = viewModel.View;
>
>        }
>
>        public void RegisterTypeIfMissing<TServiceType,
> TClassType>(bool registerSingleton)
>            where TClassType : class, TServiceType
>        {
>            if (IsTypeRegistered(Container, typeof(TServiceType)))
>            {
>                return;
>            }
>            Container.Kernel.Register(
>                Component.For<TServiceType>()
>                    .ImplementedBy<TClassType>()
>                    .LifeStyle.Is(registerSingleton ?
> LifestyleType.Singleton : LifestyleType.Transient)
>                );
>        }
>
>        public static bool IsTypeRegistered(IWindsorContainer
> container, Type type)
>        {
>            return container.Kernel.HasComponent(type);
>        }
>
>        public TService GetInstance<TService>()
>        {
>            Type serviceType = typeof(TService);
>            if (serviceType.IsClass)
>            {
>                RegisterType<TService, TService>(Container, false);
>            }
>            return (TService)Container.Resolve(serviceType);
>        }
>
>        public static void RegisterType<TServiceType,
> TClassType>(IWindsorContainer container, bool singleton)
>        {
>            if (!container.Kernel.HasComponent(typeof(TServiceType)))
>            {
>                container.Kernel.Register(
>
>
> Component.For(typeof(TServiceType)).ImplementedBy(typeof(TClassType)).Named(
>
> typeof(TClassType).FullName).LifeStyle.Is(singleton
>                                                                       ?
> LifestyleType.Singleton
>                                                                       :
> LifestyleType.Transient));
>            }
>        }
>    }
> }
>
> --
> 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]<castle-project-users%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/castle-project-users?hl=en.
>
>

-- 
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.

Reply via email to