views:

27

answers:

2

I hawe all day strugle my head and i canot find any solution to my case so i nead help. Hear is my problem: I hawe two classes that implement one interface

public interface ICacheObject
{
    string Get();
}

public class WebCacheObject : ICacheObject
{
    public string Get()
    {
        return "Web";
    }
}

public class SysteCacheObject : ICacheObject
{
    public string Get()
    {
        return "System";
    }
}

So in some other clase For Example in Class Test I nead to inject the WebCacheObject and in Test2 clase i nead to inject SystemCacheObject. I did this in Initialize:

            ObjectFactory.Initialize(c =>{ c.For<IMessage>().Use<Message>();
                                       c.For<ICacheObject>().ConditionallyUse(t =>{t.If(g => g.RequestedName == "HTTP")
                                                                                            .ThenIt.Is.ConstructedBy(
                                                                                                () =>
                                                                                                new WebCacheObject());

                                                                                        t.If(g =>g.RequestedName =="OtherCache")
                                                                                            .ThenIt.Is.ConstructedBy(
                                                                                                () =>
                                                                                                new SysteCacheObject
                                                                                                    ());
                                                                                    });

But I dont know how to Call Test-s clase-s so if I call so the condition is true (or how to change condition so this will work)

ObjectFactory.GetInstance<'ITest>()

the Test Clase will hawe WebCache in other case SystemCache???

Sorry for my bad English.

A: 

Thanks i did find the solution if eny one of you neade hear is it (i did change some thing in condition and is working ):

x.For<ICacheStorage>().ConditionallyUse(c =>
                                                                {
                                                                    c.If(t => t.ParentType == typeof(Test) ||
                                                                        t.ParentType == typeof(Test2))
                                                                        .ThenIt
                                                                        .Is.ConstructedBy(
                                                                            by => new HttpContextCacheAdapter());

                                                                    c.TheDefault.Is.ConstructedBy(
                                                                            by => new NullObjectCache());

                                                                });

Sorry for my bad English

Florim Maxhuni
A: 

I think you should avoid the conditional construction syntax with if - if there is a simpler alternative. In your case I think that this will do:

For<ICacheStorage>().Use<NullObjectCache>();
For<Test>().Use<Test>.Ctor<ICacheStorage>().Is<HttpContextCacheAdapter>();
For<Test2>().Use<Test2>.Ctor<ICacheStorage>().Is<HttpContextCacheAdapter>();
PHeiberg