Can I inject the param as optional?

Topics: General discussion, Policy Injection Application Block
Jun 30, 2011 at 2:46 AM


I use the MVP pattern to build my application. The present will get the data from the PresentDAO services. And The the PresentDAO service will get the data from the DateDAO services.


DefaultPresent->Get data DefaultDAOService-> Get data   UserDAOService with LoginMethod

                                                                                     ProductDAOSerive with ProductViewMethod

And all the DAOService will inherit from IDAOService.

So I want to inject the DateDAOService every request rather than new them every time.

Here is my sample code:

public class DefaultDAO


        public bool Login(string uid,string pwd)


                 IUserDAO dao=new UserDAO();




       public IQueryable<Product> GetProducts()


              IProductDAO dao=new ProductDAO();









Now I want to get all the dataDAO service with dependency injection. What should I do.

Now I have one  choice that I resolve the DataDaoService at the DefaultPresent:

Such as:

IUserDAO dao=container.Resolve<IUserDAO>("UserDAO");


but I think  the present should not care about where are the datas come from. So can I inject the param as optional.



Login(string uid,string pwd,IUserDAO dao);


defaultDAO.Login(uid,pwd);//the dao will be inject



<register type="IUserDAO" mapTo="UserDAO" name="UserDAO">

       <method name="Login">

             <param name="dao" dependencyName="UserDAO"/>




I hope you could give me some advices. Thank you for your help.




Jul 1, 2011 at 12:54 PM

Hi David,

Could you be more simplistic in terms of where exactly you would want to do dependency injection? I'm afraid it's kinda hard to follow which class is which :o).

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.

Jul 6, 2011 at 2:08 PM
Edited Jul 6, 2011 at 2:10 PM

Hi Gino,

Thank you for your patience help.

Such as:

I have a method:

public void SaveUser(string uid, string username, IUserDAO dao);


Now I want to assign the "DAO" by dependency inject:


<parm name="dao" dependencyName="ORACLE_UserDAO" /> or <parm name="dao" dependencyName="MSSQL_UserDAO" />


and I asing the uid, username by code:

PresentCallTheSaveUserMethod.SaveUsercodepass_uidcodepass_username, inject_dao );

I want to assign the uid and username by myself, and assign the Dao( Data Access Object ) by dependency injection.


Thank you for your help. 




Jul 7, 2011 at 9:05 AM


Unfortunately, you still need to include the "optional" parameters in your method injection otherwise unity will just find the method that contains the exact number of parameters you defined in your configuration.


Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.

Jul 8, 2011 at 2:46 AM


Thank you Noel, I'll try it.