2

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

1 Answer 1

4

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));
Sign up to request clarification or add additional context in comments.

4 Comments

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
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.
i willclear the picture here i have a Project where DAL is based on Entity Framework and ADO both are in seperate DLL and i have a seperate dll for my DependencyModule which has IDependecy interface my requirement is just by commenting or uncommenting 1 line my DAL should go to ADO or EntityFramework my EF & Ado has nearly 100 classes each so suppose i am using my Entity Framework now all the classes implement 1 interface IDependency what i need is i can easily call Customer , Order or Orderdetail of Enitity Framework or ADO any one at a time
I was looking at this question today. You didn't get what you were after, but the answer got 3 up votes. You'll probably have to ask again with a different title.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.