Ver Fonte

NEW Couche Factory ok

julien.legrand há 9 anos atrás
pai
commit
2dd93b15d6

+ 1 - 1
CD67.ModeleMVC.Factory/App.config

@@ -5,7 +5,7 @@
     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
     
   
-  </configSections>
+  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
   <entityFramework>
     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
     <providers>

+ 11 - 1
CD67.ModeleMVC.Factory/CD67.ModeleMVC.Factory.csproj

@@ -30,6 +30,14 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
+      <HintPath>..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll</HintPath>
+      <Private>True</Private>
+    </Reference>
+    <Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
+      <HintPath>..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
+      <Private>True</Private>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="System.ComponentModel.DataAnnotations" />
     <Reference Include="System.Core" />
@@ -40,11 +48,13 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="Class1.cs" />
+    <Compile Include="PARAMFactory.cs" />
+    <Compile Include="Internal\BaseFactory.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="App.config" />
+    <None Include="packages.config" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\CD67.ModeleMVC.Entity\CD67.ModeleMVC.Entity.csproj">

+ 0 - 12
CD67.ModeleMVC.Factory/Class1.cs

@@ -1,12 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace CD67.ModeleMVC.Factory
-{
-    public class Class1
-    {
-    }
-}

+ 63 - 0
CD67.ModeleMVC.Factory/Internal/BaseFactory.cs

@@ -0,0 +1,63 @@
+using CD67.ModeleMVC.Entity;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace CD67.ModeleMVC.Factory.Internal
+{
+    /// <summary>
+    /// Classe de base pour toutes les classes spécialisées de la couche de service
+    /// </summary>
+    public class BaseFactory : IDisposable
+    {
+        /// <summary>
+        /// Context Entity Framework utilisé dans la classe
+        /// </summary>
+        protected Entities dbContext;
+
+        /// <summary>
+        /// Constructeur sans argument pour les classes sans contexts Entity
+        /// </summary>
+        public BaseFactory() { }
+
+        /// <summary>
+        /// Constructeur avec initialisation du context Entity Framework
+        /// </summary>
+        /// <param name="JarvisContext">Jarvis Context Entity Framework</param>
+        public BaseFactory(Entities dbContext)
+        {
+            this.dbContext = dbContext;
+        }
+
+        #region Dispose
+        private bool disposed = false;
+
+        /// <summary>
+        /// Fonction pour détruire proprement la classe après utilisation
+        /// </summary>
+        /// <param name="disposing"></param>
+        protected virtual void Dispose(bool disposing)
+        {
+            if (!this.disposed)
+            {
+                if (disposing)
+                {
+                    dbContext.Dispose();
+                }
+            }
+            this.disposed = true;
+        }
+
+        /// <summary>
+        /// Fonction pour détruire proprement la classe après utilisation
+        /// </summary>
+        public void Dispose()
+        {
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+        #endregion
+    }
+}

+ 72 - 0
CD67.ModeleMVC.Factory/PARAMFactory.cs

@@ -0,0 +1,72 @@
+using CD67.ModeleMVC.Entity;
+using System;
+using System.Collections.Generic;
+using System.Data.Entity;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace CD67.ModeleMVC.Factory
+{
+    public class PARAMFactory : Internal.BaseFactory
+    {
+        /// <summary>
+        /// Constructeur public lié au constructeur de base
+        /// </summary>
+        /// <param name="dbContext">Context Entity Framework utilisé dans la classe</param>
+        public PARAMFactory(Entities dbContext) : base(dbContext) { }
+
+        /// <summary>
+        /// Retourne un objet par son Id
+        /// </summary>
+        /// <param name="Id">Id recherché</param>
+        /// <returns>Objet recherché</returns>
+        public PARAM getById(string Id)
+        {
+            return dbContext.PARAM.Find(Id);
+        }
+
+        /// <summary>
+        /// Retourne tous les objets
+        /// </summary>
+        /// <returns>Liste d'objets</returns>
+        public List<PARAM> getAll()
+        {
+            return dbContext.PARAM.ToList();
+        }
+
+        /// <summary>
+        /// Ajout de l'objet dans le context
+        /// </summary>
+        /// <param name="param">Objet à ajouter passé en référence pour obtenir le résultat des triggers ou numéroauto</param>
+        public void add(ref PARAM param)
+        {
+            dbContext.PARAM.Add(param);
+            dbContext.SaveChanges();
+        }
+
+        /// <summary>
+        /// Mise à jour de l'objet dans le context
+        /// </summary>
+        /// <param name="param">Objet à mettre à jour passé en référence pour obtenir le résultat des triggers ou numéroauto</param>
+        public void update(ref PARAM param)
+        {
+            // On attache l'objet en paramètre au contexte, on le note bien comme modifié pour qu'il soit mis à jour
+            dbContext.PARAM.Attach(param);
+            dbContext.Entry(param).State = EntityState.Modified;
+            dbContext.SaveChanges();
+        }
+
+        /// <summary>
+        /// Suppression de l'objet dans le context
+        /// </summary>
+        /// <param name="param">Objet à supprimer</param>
+        public void delete(ref PARAM param)
+        {
+            // Attention à ajouter ici d'éventuel contrôle ou suppression en cascade
+
+            dbContext.PARAM.Remove(param);
+            dbContext.SaveChanges();
+        }
+    }
+}

+ 4 - 0
CD67.ModeleMVC.Factory/packages.config

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="EntityFramework" version="6.0.0" targetFramework="net451" />
+</packages>

+ 8 - 0
CD67.ModeleMVC.MVC/CD67.ModeleMVC.MVC.csproj

@@ -39,6 +39,14 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
+      <HintPath>..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll</HintPath>
+      <Private>True</Private>
+    </Reference>
+    <Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
+      <HintPath>..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
+      <Private>True</Private>
+    </Reference>
     <Reference Include="Microsoft.CSharp" />
     <Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
       <HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>

+ 3 - 4
CD67.ModeleMVC.MVC/Web.config

@@ -5,9 +5,8 @@
   -->
 <configuration>
   <configSections>
-    
     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
-  
+    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
   </configSections>
   <appSettings>
     <add key="webpages:Version" value="2.0.0.0" />
@@ -46,6 +45,7 @@
     <providers>
       <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
     </providers>
+    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
   </entityFramework>
   <oracle.manageddataaccess.client>
     <version number="*">
@@ -57,8 +57,7 @@
   </oracle.manageddataaccess.client>
   <connectionStrings>
     <!-- 1ère connexion nécessaire pour créer le model Entity -->
-    <add name="OracleConnection" providerName="Oracle.ManagedDataAccess.Client"
-      connectionString="User Id=FER;Password=fer;Data Source=T-ORADB-01.cg67.fr:1523/ETU811"/>
+    <add name="OracleConnection" providerName="Oracle.ManagedDataAccess.Client" connectionString="User Id=FER;Password=fer;Data Source=T-ORADB-01.cg67.fr:1523/ETU811" />
   </connectionStrings>
   <runtime>
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

+ 1 - 0
CD67.ModeleMVC.MVC/packages.config

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <packages>
+  <package id="EntityFramework" version="6.0.0" targetFramework="net451" />
   <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net451" />
   <package id="Microsoft.AspNet.Mvc.FixedDisplayModes" version="5.0.0" targetFramework="net451" />
   <package id="Microsoft.AspNet.Mvc.fr" version="5.2.3" targetFramework="net451" />

+ 1 - 1
CD67.ModeleMVC.Tests/App.config

@@ -9,7 +9,7 @@
     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
     
   
-  </configSections>
+  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
   <appSettings></appSettings>
   <entityFramework>
     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />

+ 8 - 0
CD67.ModeleMVC.Tests/CD67.ModeleMVC.Tests.csproj

@@ -33,6 +33,14 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
+      <HintPath>..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll</HintPath>
+      <Private>True</Private>
+    </Reference>
+    <Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
+      <HintPath>..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
+      <Private>True</Private>
+    </Reference>
     <Reference Include="Microsoft.CSharp" />
     <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
     <Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">

+ 1 - 0
CD67.ModeleMVC.Tests/packages.config

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <packages>
+  <package id="EntityFramework" version="6.0.0" targetFramework="net451" />
   <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net451" />
   <package id="Microsoft.AspNet.Mvc.FixedDisplayModes" version="5.0.0" targetFramework="net451" />
   <package id="Microsoft.AspNet.Mvc.fr" version="5.2.3" targetFramework="net451" />

+ 13 - 4
ReadMe.txt

@@ -9,11 +9,12 @@ v0.1 13/07/2016 (Julien Legrand) : 1
 1. Renommer les projets en suivant ce schéma : "CD67.[nom appli].[Entity/Factory/MVC/Tests/Batchs]"
 2. Renommer également les assemblies et espaces de nom de chaque projet (dans les propriétés des projets, onglet "Application")
 
-x. Supprimer les fichiers Exemples :
-- CD67.ModeleMVC.Entity\Extend\PARAM.cs
+x. Créer vos propres fichiers d'extension avec DataAnnotation ici : "CD67.ModeleMVC.Entity\Extend" et supprimer le fichier d'exemple "CD67.ModeleMVC.Entity\Extend\PARAM.cs"
+x. Créer vos propres factory ici : "CD67.ModeleMVC.Factory" et supprimer le fichier d'exemple "CD67.ModeleMVC.Factory\PARAMFactory.cs"
 
 10. supprimer ce fichier pour ne pas que l'on sache que vous avez utilisé un modèle
 
+
 # Description générale
 La solution est consituée de 4 projets :
 - CD67.ModeleMVC.Entity : Projet qui contient les objets métiers, c'est à dire dans le cas de projets Entity : le modèle entity framework
@@ -21,6 +22,7 @@ La solution est consitu
 - CD67.ModeleMVC.MVC : Projet qui comprent le site Web MVC
 - CD67.ModeleMVC.Tests : Tests unitaires
 
+
 # CD67.ModeleMVC.Entity
 Le modèle se nomme par défaut "EntityModel"
 
@@ -35,13 +37,20 @@ Le dossier "Internal" contient :
 - "Entities.cs" : classe partielle permettant d'utiliser les nouvelles Exceptions FormattedDbEntityValidationException en surchargeant "SaveChanges()"
 En cas d'ajout d'une nouvelle connexion avec Entity Framework, il faut ajouter une nouvelle classe d'extension du même type.
 
+
 # CD67.ModeleMVC.Factory
-Les classes sont nommées ainsi : "[Nom objet]Factory"
+Les classes Factory sont nommées ainsi : "[Nom objet]Factory"
+On crée autant de classe Factory que d'objet à gérer
+Elles doivent contenir un constructeur public avec le context Entities en paramètre qui se base sur le constructeur de la classe parente.
+Elles contiennent généralement les fonctions suivantes : getById, getAll, add, update, delete.
+C'est à adapter à chaque cas.
+
+Le dossier "Internal" contient la classe de base "baseFactory" dont chaque "Factory" hérite.
 
-Chaque "Factory" hérite de la classe de base "baseFactory"
 
 # CD67.ModeleMVC.MVC
 La partie Model du projet n'est pas nécessaire que pour définir des classes ne servant qu'à l'affichage, les classes mêtiers étant dans le projet Entity.
 
+
 # CD67.ModeleMVC.Tests
 Projet le plus important de la solution :)