views:

39

answers:

0

Hi,
I have a console application that is using a wrappers class to override the Resolve method of the windsor container. I have registered my services in the app.config file of the calling class. I have a service that requires some parameters to be passed through config file. This functionality is working fine till now. My requirement is now that I have to pass these parameters at runtime creating an object of Type IDictionary.

My DIService.cs class

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Castle.Core;
using Castle.Windsor;
using Castle.Windsor.Configuration;
using Castle.Windsor.Configuration.Interpreters;
using Castle.MicroKernel.Registration;

namespace DIService {

public class DIService
{
    static IWindsorContainer m_Container = new WindsorContainer(new XmlInterpreter());

    public static object GetObject1(string key,IDictionary args)
    {
        return m_Container.Resolve(key, args);
    }

}

}

SillyEncoder.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BussinessEntities
{
public class SillyEncoder : IEncoder
{
private char[] _mixedUp = "YACBDFEGIHJLKMONPRSQTUWVXZ".ToCharArray();
public string Encode(string source) {
string upperSource = source.ToUpper();
char[] encoded = new char[source.Length];
for (int i = 0; i < encoded.Length; i++) {
encoded[i] = MapCharacter(upperSource[i]);
}
return new string(encoded); }
private char MapCharacter(char ch) {
if ((ch >= 'A') && (ch <= 'Z')) { return _mixedUp[ch - 'A']; } return ch; } }

}

SecretMessageSender.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BussinessEntities
{
public interface IEncoder
{
string Encode(string source);
}

public class SecretMessageSender  
{
    private readonly IEncoder _encoder;
    private readonly string _from;

    public SecretMessageSender(string from, IEncoder encoder)
    {
        _from = from;
        _encoder = encoder;
    }

    public void SendMessage(string to, string body)
    {
        Console.WriteLine("to: {0}\r\nfrom: {1}\r\n\r\n{2}", to, _from, _encoder.Encode(body));
    }
}

}

NullEncoder.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BussinessEntities
{
public class NullEncoder : IEncoder
{
public string Encode(string source)
{
return source;
} } }

Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DIService;
//using BussinessEntities;

namespace CastleContainer
{
class Program
{
static void Main(string[] args)
{

Need to pass those parameters from here: IDictionary parameters = new Hashtable { { "from", "rrrr@dfdf" } };
object obj123 = DIService.DIService.GetObject1("messageSender", parameters);
BussinessEntities.SecretMessageSender sender3 = ((BussinessEntities.SecretMessageSender)obj123);
sender3.SendMessage("Test By Rahul", "Message Sent Successfully"); Console.Read();
}
}
}

App.config file

<section name="castle"
  type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />

  <component id="messageSender"
           type="BussinessEntities.SecretMessageSender, BussinessEntities"

lifestyle="transient">

    <parameters>
      SecretMessageSender
      <from>[email protected]</from>
      <encoder>${encoder.null}</encoder>

Problem lies here I need to remove the parameter tag from app.config

  </component>

  <component id="encoder.silly"
            service="BussinessEntities.IEncoder, BussinessEntities"
            type="BussinessEntities.SillyEncoder, BussinessEntities"
             lifestyle="transient"/>

  <component id="encoder.null"
            service="BussinessEntities.IEncoder, BussinessEntities"
            type="BussinessEntities.NullEncoder, BussinessEntities"
             lifestyle="transient"/>

</components>

Please help.

related questions