DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
MappingModel.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 
5 using Deveel.Data.Linq;
6 
7 using IQToolkit.Data.Common;
8 using IQToolkit.Data.Mapping;
9 
10 namespace Deveel.Data.Mapping {
11  public sealed class MappingModel {
12  private readonly Dictionary<Type, TypeMapping> typeMappings;
13  private readonly List<Relationship> relationships;
14 
15  internal MappingModel() {
16  typeMappings = new Dictionary<Type, TypeMapping>();
17  relationships = new List<Relationship>();
18  }
19 
20  internal void Map(TypeMapping mapping) {
21  typeMappings[mapping.Type] = mapping;
22  }
23 
24  internal void AddRelationship(Relationship relationship) {
25  if (relationship == null)
26  throw new ArgumentNullException("relationship");
27 
28  if (!IsMapped(relationship.SourceType))
29  throw new ArgumentException(String.Format("Type '{0}' source of the relationship is not mapped.", relationship.SourceType));
30  if (!IsMapped(relationship.TargetType))
31  throw new ArgumentException(String.Format("Type '{0}' destination of the relationship is not mapped.", relationship.TargetType));
32 
33  var typeMapping = GetMapping(relationship.SourceType);
34  if (!typeMapping.IsMemberMapped(relationship.SourceMember))
35  throw new ArgumentException(String.Format("Member '{0}' in type '{1}' source of the relationship is not mapped",
36  relationship.SourceMember, relationship.SourceType));
37 
38  relationships.Add(relationship);
39  }
40 
41  public IEnumerable<Type> Types {
42  get { return typeMappings.Keys.AsEnumerable(); }
43  }
44 
45  public IEnumerable<TypeMapping> TypeMappings {
46  get { return typeMappings.Values.AsEnumerable(); }
47  }
48 
49  public IEnumerable<Relationship> Relationships {
50  get { return relationships.AsReadOnly(); }
51  }
52 
53  public bool IsMapped(Type type) {
54  return typeMappings.ContainsKey(type);
55  }
56 
57  public TypeMapping GetMapping(Type type) {
58  if (type == null)
59  throw new ArgumentNullException("type");
60 
61  TypeMapping mapping;
62  if (!typeMappings.TryGetValue(type, out mapping))
63  return null;
64 
65  return mapping;
66  }
67 
68  public Relationship GetRelationship(Type sourceType, string sourceMember) {
69  return relationships.FirstOrDefault(x => x.SourceType == sourceType &&
70  x.SourceMember == sourceMember);
71  }
72 
73  internal QueryMapping CreateQueryMapping() {
74  return new DeveelDbMapping(this);
75  }
76  }
77 }
readonly Dictionary< Type, TypeMapping > typeMappings
Definition: MappingModel.cs:12
void Map(TypeMapping mapping)
Definition: MappingModel.cs:20
void AddRelationship(Relationship relationship)
Definition: MappingModel.cs:24
readonly List< Relationship > relationships
Definition: MappingModel.cs:13
TypeMapping GetMapping(Type type)
Definition: MappingModel.cs:57
Relationship GetRelationship(Type sourceType, string sourceMember)
Definition: MappingModel.cs:68