Par Thierry Thoua,
lundi, avril 6 2009.
Lien permanent
Astoria
Ce post est la pour présenter l'interface permettant de réaliser une "custom" persistence sous ADO.Net Data Services. La classe "contexte" doit hériter de IUpdatable.
1°) La première étape est de reprendre la classe "contexte" que l'on a utilisé dans le précédent post !
public class CustomDataContext : IUpdatable
{
private static List<Pays> pays = CreePays();
public IQueryable<Pays> Pays
{
get { return pays.AsQueryable(); }
}
private static List<Pays> CreePays()
{
List<Pays> pays = new List<Pays>();
pays.Add(new Pays() { ID = 1, Nom = "Belgique", Superficie = 30528 });
pays.Add(new Pays() { ID = 2, Nom = "France", Superficie = 670922 });
pays.Add(new Pays() { ID = 3, Nom = "Luxembourg", Superficie = 2586 });
pays.Add(new Pays() { ID = 4, Nom = "Suisse", Superficie = 41290 });
return pays;
}
}
2°) La seconde étape est donc d'implémenter chaque méthode. Comme vous pouvez le voir ci-dessous, certaines sont restées vide parce que l'exemple ne gère pas les collections enfants etc etc. Nous restons sur un modèle "simple".
public class CustomDataContext : IUpdatable
{
private static List<Pays> pays = CreePays();
public IQueryable<Pays> Pays
{
get { return pays.AsQueryable(); }
}
private static List<Pays> CreePays()
{
List<Pays> pays = new List<Pays>();
pays.Add(new Pays() { ID = 1, Nom = "Belgique", Superficie = 30528 });
pays.Add(new Pays() { ID = 2, Nom = "France", Superficie = 670922 });
pays.Add(new Pays() { ID = 3, Nom = "Luxembourg", Superficie = 2586 });
pays.Add(new Pays() { ID = 4, Nom = "Suisse", Superficie = 41290 });
return pays;
}
#region Implémentation de IUpdatable
// ContainerName = Pays
// fullType = fullType de pays
public object CreateResource(string containerName, string fullTypeName)
{
var nouvelleResource = Activator.CreateInstance(Type.GetType(fullTypeName));
pays.Add((Pays)nouvelleResource);
return nouvelleResource;
}
public void SetValue(object targetResource, string propertyName, object propertyValue)
{
PropertyInfo property = targetResource.GetType().GetProperty(propertyName);
property.SetValue(targetResource, propertyValue, null);
}
public object ResolveResource(object resource)
{
return resource;
}
public void SaveChanges()
{
// Persiste les modifications
}
public void DeleteResource(object targetResource)
{
pays.Remove((Pays)targetResource);
}
public object GetValue(object targetResource, string propertyName)
{
var targetProperty = targetResource.GetType().GetProperty(propertyName);
return targetProperty.GetValue(targetResource, null);
}
public object GetResource(IQueryable query, string fullTypeName)
{
var enumerator = query.GetEnumerator();
while (enumerator.MoveNext())
{
if (enumerator.Current != null)
return enumerator.Current;
}
throw new DataServiceException();
}
public void ClearChanges()
{
// Annule les modifications
}
public object ResetResource(object resource)
{
Pays pays = new Pays();
pays.ID = ((Pays)resource).ID;
pays.Superficie = 0;
pays.Nom = null;
return pays;
}
public void AddReferenceToCollection(object targetResource, string propertyName, object resourceToBeAdded)
{
throw new NotImplementedException();
}
public void RemoveReferenceFromCollection(object targetResource, string propertyName, object resourceToBeRemoved)
{
throw new NotImplementedException();
}
public void SetReference(object targetResource, string propertyName, object propertyValue)
{
throw new NotImplementedException();
}
#endregion
}
3°) La troisième étape est de modifier le service ADO.Net data service pour accepter les modifications. On change donc l'enum EntitySetRights de AllRead en All.
public class WebDataServiceAstoria : DataService<CustomDataContext>
{
public static void InitializeService(IDataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
//config.UseVerboseErrors = true;
}
}
4°) Passons au test ! Nous allons tester le cas de l'insert / update / delete via une petite application console !
class Program
{
static void Main(string[] args)
{
CustomDataContext dataContext = new CustomDataContext(new Uri("http://localhost:13094/WebSite2/WebDataServiceAstoria.svc/"));
// On ajoute un nouveau pays !
Pays pays = new Pays() { ID = 10, Nom = "Canada", Superficie = 10 };
dataContext.AddToPays(pays);
dataContext.SaveChanges();
// On modifie son ID et on persiste les modifications
pays.ID = 9;
dataContext.UpdateObject(pays);
dataContext.SaveChanges();
// On efface le nouvel objet créé
dataContext.DeleteObject(pays);
dataContext.SaveChanges();
}
}
5°) Et voici comment on fait en quelques minutes la "persistence" des modifications sous ADO.Net Data Services ! Rien de plus simple non ?