views:

63

answers:

4

Hello Friends,

I have this Class which is accepting a Dictionary<string,object> which is been invoked from different places in the code. Dictionary concept is making things untyped and difficult to figure out what to pass the class to function otherwise it throws run-time exception. As this Dictionary<string,object> is a contract definition so i had to write an extension method to convert my type to a dictionary when invoking my class . But now how can i close this Class that it only accepts a specific Type

So for eg.

public class CreateReport : IRep
{

public void SetParam(Dictionary<string,object> parm)
{
 // Here the dictionary param are been set. 
}

 public object RunRep()
{

}
}

ClassInvoker.Invoke(CreateReport , Dictionary<string,object>{"MyParam" , "World"});

So this is how things are now.

I have changed it by creating a Property class as

public class CreateReportProp
{
  public string MyParam { get;set;}
}

and having a extension method as ConvertObjToDict

so Now we have to do something like

ClassInvoker.Invoke(CreateReport , new CreateReportProp { MyParam = "World"}.ConvertObjToDict());

But i would like to go further and close the Class so that CreateReportClass you can only Pass CreateReportParam otherwise compiler throws an exception.

Please give me some ideas as how can i acheive this.

A: 

Why not use a strongly typed dictionary:

public void SetParam<T>(Dictionary<string, T> parm)
{
}
Darin Dimitrov
Hi Darin,Sorry i didnt specify clearly its does uses strongly typed dictionary.But my question is more to do with how can i close the class to accept only specific Type which in turn would anyways be converted by my extension method to a dictionary. But right now as you can see i can pass any Type to the invoking to the class and i would only get runtime exception. Which i want as compile time rather.
netmatrix01
A: 

I would use a generic interface you need 2 interfaces a generic typed intherface that uses strongly typed dictionaries. then you can work with the interfaces and not worry about the classes because each class that implements the interfaces will look after how to work with the data.

public interface IExample<t> : IExample
{
 AddDictionary(T);
}

public interface IExample
{
 AddDictionary(object)
}

then write the object version to cast into the typed version and implement there.

Add any other generic methods you need and then the classes you are using will manage themselves and all should be hunky dory ... that is if i understood you properly.

John Nicholas
A: 

You can limit generics using the where key word. Here is a simple example using a linked list

public class LinkedList<K,T> where K : IComparable
{
   T Find(K key)
   {
      Node<K,T> current = m_Head;
      while(current.NextNode != null)
      {
         if(current.Key.CompareTo(key) == 0)

            break;
         else      

            current = current.NextNode;
      }
      return current.Item; 
   }
   //Rest of the implementation 
}
Nanook
A: 

I presume you are using the CreateReport class for many specific reports? If this is the case, the you have a chance to enact a compile-time check. If the CreateReport class dynamically invokes the report, i.e. by name, and each report has different parameter requirements, then there is no compile-time check possible.

In the first case, you would change the signature for the CreateReport class as follows:

public class CreateReport<T> : IRep, where T : CreateReportProp

In the SetParam method, you would have

public void SetParam(T param)

For each specific report you can create a different property class as long as it inherits from CreateReportProp. Then when you instance the report class you will specify that one.

CreateReport<MySpecificParam> myReport = new CreateReport<MySpecificParam>();
myReport.SetParam( new MySpecificParam() {...});

If you wish to do less changes within the CreateReport class you can have your CreateReportProp implement an IEnumerable or IDictionary interface so that iterating the parameters is more generic.

Jennifer Zouak