Getting Started with OB

Apr 4, 2007 at 10:19 PM
I'm trying to do the following, but I'm not sure where to start:

class A creates an instance of class B. When class B is created I want to have a field within class B assigned an singleton instance of class C. This field can either be set via the constructor or property. For example:

class ClassA
{
   public void MethodA()
   {
      ClassB classB = new ClassB();
      classB.DoSomething();
   }
}
 
class ClassB
{
   public void ClassB()
   {
   }
 
   private IClassC iclassC;
 
   public IClassC ClassC
   {
      set { iclassC = value; }
   }
}
 
class ClassC : IClassC
{
}

I think I can use the CreateNew attribute on class C's constructor or property setter. But I'm not sure what else is required to wire-up ObjectBuilder.

My goal is to use this within the Web Services Software Factory. Ideally, I want my business logic classes to automatically create instances of the repository classes. I want to use interfaces here so that the test harness can use mock objects and production can use the real repository classes.

Any pointers would be greatly appreciated.
Developer
Apr 5, 2007 at 3:15 AM
ObjectBuilder has the DependencyAttribute attribute that you can apply to your ClassC property. This will cause the container to wire-up the dependency for you. The IClassC instance that it uses will depend on how the container is setup and the parameters you specify to the attribute.

I haven't used the Web Services Software Factory, so I don't know what their dependency container looks like, but I imagine it has some way to register a singleton instance/type.

An example might look like:
class ClassB
{
   public void ClassB()
   {
   }
 
   private IClassC iclassC;
 
   [Dependency(SearchMode = SearchMode.Local, NotPresentBehavior = NotPresentBehavior.Throw)]
   public IClassC ClassC
   {
      set { iclassC = value; }
   }
}
 
Apr 5, 2007 at 2:59 PM
From brannon:
ObjectBuilder has the DependencyAttribute attribute that you can apply to your ClassC property. This will cause the container to wire-up the dependency for you. The IClassC instance that it uses will depend on how the container is setup and the parameters you specify to the attribute.

I'm not sure that the service software factory is using a container. Do I need to create one, if so how? Since I'm using web services, I would assume the container would need to be created when the web application starts up.

Thanks,
Roger
Apr 5, 2007 at 8:07 PM
Hi AdrenalineWerks,

I'm pretty new to ObjectBuilder myself, but I believe you do need a container if you're looking for it to manage singletons for you. Does the below example do anything like what you want? (and could more knowledgable OB people please correct me if that's broken :-)

    class ClassA
    {
        public void MethodA()
        {
            Builder builder = new Builder();
            Locator locator = new Locator();
 
            locator.Add(typeof(ILifetimeContainer), new LifetimeContainer());
            builder.Policies.Set<ISingletonPolicy>(new SingletonPolicy(true), typeof(ClassC), null);
 
            ClassB classB1 = builder.BuildUp<ClassB>(locator, null, null);
            ClassB classB2 = builder.BuildUp<ClassB>(locator, null, null);
 
            classB1.DoSomething();
            classB2.DoSomething();
        }
    }
 
    class ClassB
    {
        private IClassC iclassC;
 
        [Dependency(NotPresentBehavior = NotPresentBehavior.CreateNew, CreateType = typeof(ClassC))]
        public IClassC ClassC
        {
            set { iclassC = value; }
        }
 
        public void DoSomething()
        {
            Console.WriteLine(iclassC.ID);
        }
    }
 
    interface IClassC
    {
        Guid ID { get; }
    }
 
    class ClassC : IClassC
    {
        private Guid id = Guid.NewGuid();
        public Guid ID { get { return id; } }
    }
Developer
Apr 6, 2007 at 1:18 AM

AdrenalineWerks wrote:
I'm not sure that the service software factory is using a container. Do I need to create one, if so how? Since I'm using web services, I would assume the container would need to be created when the web application starts up.


I'll try to take a look at the software factory and let you know. I'm sure they are using some sort of container.
Developer
Apr 7, 2007 at 9:36 PM
Unfortunately I can't find a way to just look at the source or documentation for the WSSF without installing a bunch of software.

You might try posing this question in their forums. They should have some sort of Builder or container that has a method like BuildItem(), etc. You are supposed to use that method to initialize and/or create objects. If you are using that method, then it should support the Dependency attribute.