Overblog Suivre ce blog
Editer l'article Administration Créer mon blog

Présentation

  • : Romagny13 - Du .NET,du pur .NET
  • Romagny13 - Du .NET,du pur .NET
  • : Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
  • Contact

Recherche

Articles Récents

30 mars 2014 7 30 /03 /mars /2014 18:40

II ADO.NET Entity Framework

 

1-Utilisation directe des POCO générées avec EDM
Exemple on a une ObservableCollection<Article> .Article étant généré avec le designer EDM.
Peu de changements par rapport à LINQ To SQL .Les changements se font de manière transparente. Seuls l’ajout et la modification demandent un peu de code.
private void Articles_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                Article newArticle = e.NewItems[0] as Article;
                newArticle.ArticleID = Guid.NewGuid();
 
                if (!context.Articles.Local.Contains(newArticle))
                    context.Articles.Add(newArticle);
            }
            if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                Article oldArticle = e.OldItems[0] as Article;
 
                if (context.Articles.Local.Contains(oldArticle))
                    context.Articles.Remove(oldArticle);
            }
                     
        }
 
        private void CurrentArticle_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
 
        }

 

2- N-Tiers

alldata2.png
 
ViewModel
   private void Articles_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                ArticleModel newArticle = e.NewItems[0] as ArticleModel;
                newArticle.ArticleID = Guid.NewGuid();
                articleService.Add(newArticle);
            }
            if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                ArticleModel oldArticle = e.OldItems[0] as ArticleModel;
                articleService.Remove(oldArticle);
            }
        }
        private void CurrentArticle_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            articleService.Update(CurrentArticle);
        }

 

Service
  public class ArticleService :IArticleService
    {
        private DbTradingEntities context = new DbTradingEntities();
 
        private List<ArticleModel> MapToModel(List<Article> articles)
        {
            List<ArticleModel> result = new List<ArticleModel>();
            foreach (Article article in articles)
            {
                result.Add(article.ToModel());
            }
            return result;
        }
 
        public List<ArticleModel> GetAll()
        {
            return MapToModel(context.Articles.ToList());
        }
 
        public void Add(ArticleModel model)
        {
            context.Articles.Add(model.ToEntity());
                     
        }
 
        public void Update(ArticleModel model)
        {
            if (context.Articles.Where(a => a.ArticleID == model.ArticleID).Count() > 0)
            {
                Article oldArticle = context.Articles.Single(a => a.ArticleID == model.ArticleID);
                context.Entry(oldArticle).CurrentValues.SetValues(model.ToEntity());
            }
                    
            if (context.Articles.Local.Where(a => a.ArticleID == model.ArticleID).Count() > 0)
            {
                Article oldArticle = context.Articles.Local.Single(a => a.ArticleID == model.ArticleID);
                context.Entry(oldArticle).CurrentValues.SetValues(model.ToEntity());
            }                      
                    
        }
 
        public void Remove(ArticleModel model)
        {
 
            if (context.Articles.Where(a => a.ArticleID == model.ArticleID).Count() > 0)
            {
                Article oldArticle = context.Articles.Single(a => a.ArticleID == model.ArticleID);
                context.Articles.Remove(oldArticle);
            }
            // Local (un élément qui vient d'être ajouté)
            if (context.Articles.Local.Where(a => a.ArticleID == model.ArticleID).Count() > 0)
            {
                Article oldArticle = context.Articles.Local.Single(a => a.ArticleID == model.ArticleID);
                context.Articles.Local.Remove(oldArticle);
            }
                     
        }
 
        public int SaveAll()
        {
           return context.SaveChanges();
        }
 
    }

 

Mapper identique à celui pour LINQ To SQL
public static ArticleModel ToModel(this Article entity)
             {
                    ArticleModel model = new ArticleModel()
                    {
                           ArticleID = entity.ArticleID,
                           ArticleName = entity.ArticleName,
                           Description = entity.Description,
                           UnitPrice = entity.UnitPrice,
                                       Collection = new CollectionModel() { CollectionID = entity.Collection.CollectionID,  CollectionName = entity.Collection.CollectionName }          
            };
                    return model;
       }
 
        // MODEL > DB
             public static Article ToEntity(this ArticleModel model)
             {
                    Article entity = new Article()
                    {
                           ArticleID = model.ArticleID,
                           ArticleName = model.ArticleName,
                           Description = model.Description,
                           UnitPrice = model.UnitPrice,
                           CollectionID = model.Collection.CollectionID,
                          
                    };
                    return entity;
             }
       }
 

 

Enfin,Il est possible de vérifier l’EntityState des lignes à insérer,modifier,supprimer en passant .Je donne des informations pour cela dans un précédent post

Partager cet article

Repost 0
Published by Romagny13 - dans Entity Framework
commenter cet article

commentaires