Comment utiliser Unity avec le fichier de config
Bonjour,
je suis entrain de travailler sur un projet test avec unity 2.1, j'ai donc créer un projet Bibliothèque de classe qui contient ma BDD mes interfaces et mes classes et un projet winform où j'utilise Unity pour travailler avec cette Bibliotheque.
voici le code de mes interfaces :
Code:
1 2 3 4 5 6 7 8 9 10
| namespace Objet
{
public interface IPersonneRepository
{
Personne ObtenirParId(int id);
List<Personne> ObtenirTous();
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| namespace Objet
{
public interface IPersonneService
{
Personne ObtenirParId(int id);
List<Personne> ObtenirParNom(string nom);
List<Personne> ObtenirTous();
List<string> ObtenirTousLesNoms();
}
} |
et j'ai donc 2 classes PersonneRepository et PersonneService qui implémentent ces interfaces.
En initialisant le container unity en code aucun problème pour faire appel au méthode de mes interfaces:
Code:
1 2 3
|
container = new UnityContainer().RegisterType<IPersonneRepository, PersonneRepository>().RegisterType<IPersonneService, PersonneService>();
comboBox1.DataSource = container.Resolve<IPersonneService>().ObtenirTousLesNoms(); |
Mais si je charge mon fichier de configuration j'ai une erreur:
Code:
1 2
| container = new UnityContainer().LoadConfiguration();
comboBox1.DataSource = container.Resolve<IPersonneService>().ObtenirTousLesNoms(); |
Erreur
Citation:
{"Resolution of the dependency failed, type = \"Objet.IPersonneService\", name = \"(none)\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The current type, Objet.IPersonneService, is an interface and cannot be constructed. Are you missing a type mapping?\r\n-----------------------------------------------\r\nAt the time of the exception, the container was:\r\n\r\n Resolving Objet.IPersonneService,(none)\r\n"}
Voici mon fichier de config:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<assembly name="Objet" />
<namespace name="Objet" />
<!--<alias alias="IPersonneRepository" type="Objet.IPersonneRepository, Objet" />
<alias alias="IPersonneService" type="Objet.IPersonneService, Objet" />
<alias alias="PersonneRepository" type="Objet.PersonneRepository, Objet" />
<alias alias="PersonneService" type="Objet.PersonneService, Objet" />-->
<container>
<register type="IPersonneRepository" name="Repository" mapTo="PersonneRepository" />
<register type="IPersonneService" name="Service" mapTo="PersonneService" >
<constructor>
<param name="rep" dependencyType="IPersonneRepository"/>
</constructor>
</register>
</container>
</unity>
</configuration> |
Je pense soit avoir oublié une étape ou soit avoir mal initialisé mon container, j'ai essayé de modifier le fichier de config ou là façon d'appeler mon container mais j'ai toujours une erreur.
Merci pour votre aide.