Using the Unity Framework, enter the page in System.Windows.Forms.Form
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 introduce specific classes for the two attributes above.
I have a type for each one and I am using app.config for configuration information. But I don't want to create an interface for every page, just for input, so how can I input into every page?
Basically, what should I put in the type attribute on the next element type 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 Unity Framework for this, btw.
Thanks.
Edit: I get a container and then I try to insert using this:
Container.Configure<InjectedMembers>().ConfigureInjectionFor<mdiAuthenticationForm>();
a source to share
Basically you want to use property nesting to inject mdiAuthenticationForm dependencies. Can't you do something like the following?
Add type mappings to your 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 to the config file, you can also do the following:
mdiAuthenticationForm form = new mdiAuthenticationForm();
container.BuildUp<mdiAuthenticationForm>(form);
This should resolve dependencies on the existing instance and link them.
a source to share
I got it to work, but not really, but it works. The last problem was that I have to include Form.ShowDialog in my interface.
First, this is the main part of my code that created and called the form from Program.cs, and the second is my interface. I wanted this to be modal, so I am using ShowDialog. I also moved two attributes to the new controller, but I still have one property to set to make sure it works correctly.
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();
}
a source to share