updated ex_042_012_EF_CF_Dictionary about dictionaries and EF

updatesEFSamplesToNetCore3
Marc CHEVALDONNE 5 years ago
parent eefe5b5e31
commit 4b82f0530c

@ -0,0 +1,48 @@
// ========================================================================
//
// Copyright (C) 2019-2020 MARC CHEVALDONNE
// marc.chevaldonne.free.fr
//
// Module : DictionaryItem.cs
// Author : Marc Chevaldonné
// Creation date : 2016-11-01
// Last Modified : 2019-12-25
//
// ========================================================================
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ex_042_012_EF_CF_Dictionary
{
/// <summary>
/// classe de base permettant de transformer un dictionnaire Dictionary<TKey, TValue> en
/// ICollection<Item<TKey, TValue>> permettant la liaison avec Entity Framwork.
/// Dans la classe Score, TKey est de type Nounours et TValue est de type int.
/// La propriété Key permet donc de réaliser la relation one to one avec NounoursEx.
/// La propriété Value permet d'obtenir la valeur associée (ici le score entier).
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
public class Item<TKey, TValue>
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid UniqueId
{
get; set;
}
public TKey Key
{
get;
set;
}
public TValue Value
{
get; set;
}
}
}

@ -0,0 +1,79 @@
// ========================================================================
//
// Copyright (C) 2019-2020 MARC CHEVALDONNE
// marc.chevaldonne.free.fr
//
// Module : LitEntity.cs
// Author : Marc Chevaldonné
// Last Modified : 2019-12-25
//
// ========================================================================
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
namespace ex_042_012_EF_CF_Dictionary
{
[Table("Lits")]
public class LitEntity : ILit
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid UniqueId
{
get; set;
}
public string Propriétaire
{
get; set;
}
/// <summary>
/// remplace le dictionnaire : on utilise une collection de Scores afin d'établir une relation one to many entre
/// LitEx et Score
/// </summary>
internal virtual ICollection<Score> mScores
{
get;
set;
} = new List<Score>();
public int this[INounours nounours]
{
get
{
var score = mScores.SingleOrDefault(sc => sc.Key.Equals(nounours));
return score != null ? score.Value : 0;
}
set
{
var score = mScores.SingleOrDefault(sc => sc.Key.Equals(nounours));
if (score != null)
{
score.Value = value;
}
else
{
var n = nounours as Nounours;
if (n == null) throw new InvalidCastException("nounours should be of type Nounours");
Score newScore = new Score()
{
Key = n,
Value = value,
Lit = this
};
mScores.Add(newScore);
}
}
}
public ReadOnlyDictionary<INounours, int> Scores =>
new ReadOnlyDictionary<INounours, int>(mScores.ToDictionary(kvp => kvp.Key as INounours, kvp => kvp.Value));
}
}

@ -0,0 +1,40 @@
// ========================================================================
//
// Copyright (C) 2019-2020 MARC CHEVALDONNE
// marc.chevaldonne.free.fr
//
// Module : ILit.cs
// Author : Marc Chevaldonné
// Last Modified : 2019-12-25
//
// ========================================================================
using System;
using System.Collections.ObjectModel;
namespace ex_042_012_EF_CF_Dictionary
{
public interface ILit
{
Guid UniqueId
{
get; set;
}
string Propriétaire
{
get; set;
}
ReadOnlyDictionary<INounours, int> Scores
{
get;
}
int this[INounours nounours]
{
get;
set;
}
}
}

@ -0,0 +1,41 @@
// ========================================================================
//
// Copyright (C) 2019-2020 MARC CHEVALDONNE
// marc.chevaldonne.free.fr
//
// Module : INounours.cs
// Author : Marc Chevaldonné
// Last Modified : 2019-12-25
//
// ========================================================================
using System;
namespace ex_042_012_EF_CF_Dictionary
{
public interface INounours
{
Guid UniqueId
{
get; set;
}
string Nom
{
get;
set;
}
DateTime DateDeNaissance
{
get;
set;
}
int NbPoils
{
get;
set;
}
}
}

@ -0,0 +1,28 @@
// ========================================================================
//
// Copyright (C) 2019-2020 MARC CHEVALDONNE
// marc.chevaldonne.free.fr
//
// Module : INounoursEqualityComparer.cs
// Author : Marc Chevaldonné
// Last Modified : 2019-12-25
//
// ========================================================================
using System.Collections.Generic;
namespace ex_042_012_EF_CF_Dictionary.Model
{
class INounoursEqualityComparer : EqualityComparer<INounours>
{
public override bool Equals(INounours x, INounours y)
{
return x.Nom == y.Nom && x.DateDeNaissance == y.DateDeNaissance;
}
public override int GetHashCode(INounours obj)
{
return obj.GetHashCode();
}
}
}
Loading…
Cancel
Save