views:

414

answers:

2

I have a form:

public partial class mdiAuthenticationForm : Form
    {
        public Services.Authentication.IAuthentication Authenticator { get; set; 
        public Services.Authentication.IAuthorization Authorizor { get; set; }

and I want to inject concrete classes for the two attributes above.

I have a type for each of them, and am using the app.config for the configuration information. But, I don't want to create an interface for each page, just to inject, so, how can I inject into each page?

Basically, what to I put in the type attribute in the following type element, or, how can I do this?

  <type type="" mapTo="mdiAuthenticationForm,project">
  <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
      <property name="Authenticator" propertyType="Services.Authentication.IAuthentication,project">
        <dependency name="mssqlauth" />
      </property>
    <property name="Authorizor" propertyType="Services.Authentication.IAuthorization,project">
      <dependency name="mssqlautz" />
    </property>
  </typeConfig>
  </type>

I am using the Unity Framework for this, btw.

Thank you.

Edit: I get the container and then I am trying to inject by using this:

Container.Configure<InjectedMembers>().ConfigureInjectionFor<mdiAuthenticationForm>();
A: 

I got it to work, not pretty, but it works. The last problem was that I have to include Form.ShowDialog in my interface.

First is the main part of my code that created and called the form, from Program.cs, then the second is my interface. I wanted this to be modal, which is why I use ShowDialog. I also moved the two attributes into a new controller, but I still have one property to set, to make certain it is working properly.

    container.Configure<InjectedMembers>().ConfigureInjectionFor<IAuthenticationForm>();
    containers.Configure(container);
    IAuthenticationForm f = container.Resolve<IAuthenticationForm>();
    f.ShowDialog();


public interface IAuthenticationForm
{
    Optimal4.Services.Authentication.IAuthorization Authorizor { get; set; }
    void checkAuthentication();
    System.Windows.Forms.DialogResult ShowDialog();
}
James Black
A: 

Basically you want to use property injection to inject mdiAuthenticationForm's dependencies. Can't you do something like the following?

Add your type mappings in the config file:

<container>
  <types>
    <type type="IAuthentication,PutAssemblyNameHere" mapTo="Authentication,PutAssemblyNameHere"/>
    <type type="IAuthorization,PutAssemblyNameHere" mapTo="Authorization,PutAssemblyNameHere"/>
    <type type="mdiAuthenticationForm,PutAssemblyNameHere"/>
  </types>
</container>

Put Dependency attributes on Authentication and Authorization properties.

[Dependency]
public Services.Authentication.IAuthentication Authenticator { get; set;} 
[Dependency]
public Services.Authentication.IAuthorization Authorizor { get; set; }

Then finally in your code, do the following to get an instance of mdiAuthenticationForm:

mdiAuthenticationForm form = container.Resolve<mdiAuthenticationForm>();

If you don't want to add mdiAuthentication in the config file, you can also do the following:

mdiAuthenticationForm form = new mdiAuthenticationForm();
container.BuildUp<mdiAuthenticationForm>(form);

This should resolve the dependencies on an existing instance and wire them up.

Mehmet Aras
I am trying to not put anything in my code beyond what I must for the wiring. I want to be as unobtrusive as possible, unfortunately this is harder than I expected.
James Black
This looks like what my final solution was, sorry it was so long for me to acknowledge this.
James Black