views:

286

answers:

1

Let me clear with my Question, I have say 100 classes all these 100 classes implements 1 interface say IDependency, say suppose my classname is Customer it implements IDependency

public class Customer : IDependency
public class Order:IDependency

it goes on till 100 classes

And I am registering this class in web.config like

<type type="IDependency" mapTo="Customer"></type>
<type type="IDependency" mapTo="Order"></type>

this will be wrong because i am getting error IDependency already declared

Now what i need is there should be only 1 line in web.config and i should be able to resolve all my classes and I don't want to RegisterType in my code like

Container.RegisterType<IDependency,Customer>();
Container.RegisterType<IDependency,Order>();

I don't want to write these lines in my code file the requirement is like that all should be done through config file.

Do you guys think is this valid if yes can you at least give me idea how to approach it.

Overall what i think is there should be 1 class say BaseClass which implement my IDependency Interface first and all my 100 classes will inherit that class and my config file should be like

<type type="IDependency" mapTo="BaseClass"></type>

Please let me know if you need more clarification on this because I am really struck with this

+2  A: 

You can't register the same interface for two classes at the same time. In your code sample:

Container.RegisterType<IDependency,Customer>();
Container.RegisterType<IDependency,Order>();

what the Unity container will do is to register Order as the class to return when you invoke Resolve<IDependency>, and forget about the previous Customer class registration.

Anyway I don't quite understand which is your goal. If you could register multiple classes for the same interface, what would you expect to obtain when invoking Resolve<IDependency>, or what would you expect the classes having an IDependency parameter in the constructor to receive?

EDIT based on clarification from Dotnet Coder:

If all you want is to decide at runtime which class you want to register based on configuration, you can have one value in the configuration file (web.config, Settings, or the like) telling which class you want to associate. For example:

string className = Settings.Default["IDependencyClassName"]; //just an example, can use web.config or other config system as well
Container.RegisterType(typeof(IDependency), Type.GetType(className));
Konamiman
But in Config File if i have 100 classes i have to write 100 mapping lines right like <type type="IDependency" mapTo="Customer"></type> and <type type="IDependency" mapTo="Order" name="Order"></type> i am looking for a solution where my COnfig file will have only 1 Line
Dotnet Coder
But you are using only 1 mapping at the same time, right? Then, have just 1 line in the config file, and change the class name as appropriate.
Konamiman
Dotnet Coder
is anybody looking into this ???
Dotnet Coder