Skip to content

Commit

Permalink
ManyToOne, otras mejoras
Browse files Browse the repository at this point in the history
  • Loading branch information
ErnestoNCarrea committed Feb 18, 2017
1 parent 17267da commit 255678e
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 26 deletions.
10 changes: 10 additions & 0 deletions Orm/Attributes/Association.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;

namespace Lazaro.Orm.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public abstract class Association : System.Attribute
{
}
}
10 changes: 10 additions & 0 deletions Orm/Attributes/Id.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;

namespace Lazaro.Orm.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class Id : System.Attribute
{
}
}
10 changes: 10 additions & 0 deletions Orm/Attributes/ManyToMany.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;

namespace Lazaro.Orm.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class ManyToMany : Association
{
}
}
10 changes: 10 additions & 0 deletions Orm/Attributes/ManyToOne.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;

namespace Lazaro.Orm.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class ManyToOne : Association
{
}
}
10 changes: 10 additions & 0 deletions Orm/Attributes/OneToMany.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;

namespace Lazaro.Orm.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class OneToMany : Association
{
}
}
10 changes: 10 additions & 0 deletions Orm/Attributes/OneToOne.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;

namespace Lazaro.Orm.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class OneToOne : Association
{
}
}
5 changes: 1 addition & 4 deletions Orm/ColumnTypes.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lazaro.Orm
{
public enum ColumnTypes
{
Serial,
Relation,
Association,
Integer,
MediumInt,
SmallInt,
Expand Down
13 changes: 12 additions & 1 deletion Orm/EntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,18 @@ public void Persist(object entity)

foreach(var Col in ClassMetadata.Columns) {
if (Col.GeneratedValueStategy == GeneratedValueStategies.None) {
InsertOrUpdate.ColumnValues.AddWithValue(Col.Name, ClassMetadata.ObjectInfo.GetColumnValue(entity, Col));
object ColValue;

if (Col.AssociationMetada != null && Col.AssociationMetada.AssociationType != AssociationTypes.None) {
// Association column. Get value from property on the other side
object OtherEndEntity = ClassMetadata.ObjectInfo.GetColumnValue(entity, Col);
ColValue = Col.AssociationMetada.OtherEndClass.ObjectInfo.GetColumnValue(OtherEndEntity, Col.AssociationMetada.OtherEndColumn);
} else {
// Regular member-to-column value
ColValue = ClassMetadata.ObjectInfo.GetColumnValue(entity, Col);
}

InsertOrUpdate.ColumnValues.AddWithValue(Col.Name, ColValue);
}
}

Expand Down
83 changes: 62 additions & 21 deletions Orm/Mapping/AnnotationMetadataFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Lazaro.Orm.Attributes;

namespace Lazaro.Orm.Mapping
{
Expand All @@ -27,6 +29,7 @@ public void Init()
} */
}


public void ScanFolder(string folderName, bool recursive)
{
var Files = System.IO.Directory.EnumerateFiles(folderName, "*.dll", recursive ? System.IO.SearchOption.AllDirectories : System.IO.SearchOption.TopDirectoryOnly);
Expand Down Expand Up @@ -67,40 +70,78 @@ private void ScanAssembly(Assembly assembly)
this.ClassMetadata.Add(Cls.FullName, ClsMeta);
}
}
}


this.FillAssociationInfo();
}


private ColumnMetadataCollection ScanClass(Type clsType)
{
var Res = new ColumnMetadataCollection();

var Members = clsType.GetMembers(BindingFlags.Instance | BindingFlags.GetField | BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Public);
foreach(MemberInfo Mbr in Members) {
var ColAttrs = Mbr.GetCustomAttributes(typeof(Attributes.Column), true);
if(ColAttrs.Length > 0) {
var ColumnAttr = ColAttrs[0] as Attributes.Column;
var NewCol = new ColumnMetadata()
{
MemberName = Mbr.Name,
Id = ColumnAttr.Id,
GeneratedValueStategy = ColumnAttr.GeneratedValueStategy,
Name = ColumnAttr.Name,
Type = ColumnAttr.Type,
Length = ColumnAttr.Length,
Precision = ColumnAttr.Precision,
Nullable = ColumnAttr.Nullable,
Unique = ColumnAttr.Unique,
};
if(Mbr is PropertyInfo) {
NewCol.PropertyInfo = Mbr as PropertyInfo;
NewCol.MemberType = NewCol.PropertyInfo.PropertyType;
} else if (Mbr is FieldInfo) {
NewCol.FieldInfo = Mbr as FieldInfo;
NewCol.MemberType = NewCol.FieldInfo.FieldType;
if(ColAttrs.Length == 1) {
var NewCol = ColumnMetadataFromColumnAttribute(Mbr, ColAttrs[0] as Attributes.Column);

var IdAttrs = Mbr.GetCustomAttributes(typeof(Attributes.Id), true);
if (IdAttrs.Length == 1) {
NewCol.Id = true;
}

var AssocAttrs = Mbr.GetCustomAttributes(typeof(Attributes.Association), true);
if (AssocAttrs.Length == 1) {
NewCol.AssociationMetada = AssociationMetadataFromAttribute(AssocAttrs[0] as Attributes.Association);
}

Res.Add(NewCol);
}

}

return Res;
}

private AssociationMetadata AssociationMetadataFromAttribute(Association assocAttr)
{
var Res = new Mapping.AssociationMetadata();

if (assocAttr is Attributes.ManyToMany) {
Res.AssociationType = AssociationTypes.ManyToMany;
} else if (assocAttr is Attributes.ManyToOne) {
Res.AssociationType = AssociationTypes.ManyToOne;
} else if (assocAttr is Attributes.OneToMany) {
Res.AssociationType = AssociationTypes.OneToMany;
} else if (assocAttr is Attributes.OneToOne) {
Res.AssociationType = AssociationTypes.OneToOne;
}

return Res;
}

protected ColumnMetadata ColumnMetadataFromColumnAttribute(MemberInfo mbrInfo, Attributes.Column colAttr)
{
var Res = new ColumnMetadata()
{
MemberName = mbrInfo.Name,
Id = colAttr.Id,
GeneratedValueStategy = colAttr.GeneratedValueStategy,
Name = colAttr.Name,
Type = colAttr.Type,
Length = colAttr.Length,
Precision = colAttr.Precision,
Nullable = colAttr.Nullable,
Unique = colAttr.Unique,
};

if (mbrInfo is PropertyInfo) {
Res.PropertyInfo = mbrInfo as PropertyInfo;
Res.MemberType = Res.PropertyInfo.PropertyType;
} else if (mbrInfo is FieldInfo) {
Res.FieldInfo = mbrInfo as FieldInfo;
Res.MemberType = Res.FieldInfo.FieldType;
}

return Res;
Expand Down
12 changes: 12 additions & 0 deletions Orm/Mapping/AssociationMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Lazaro.Orm.Mapping
{
public class AssociationMetadata
{
public AssociationTypes AssociationType { get; set; } = AssociationTypes.None;

public ClassMetadata OtherEndClass { get; set; }
public ColumnMetadata OtherEndColumn { get; set; }
}
}
13 changes: 13 additions & 0 deletions Orm/Mapping/AssociationTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace Lazaro.Orm.Mapping
{
public enum AssociationTypes
{
None = 0,
OneToOne,
ManyToOne,
OneToMany,
ManyToMany
}
}
2 changes: 2 additions & 0 deletions Orm/Mapping/ColumnMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class ColumnMetadata
public PropertyInfo PropertyInfo { get; internal set; }
public FieldInfo FieldInfo { get; internal set; }

public AssociationMetadata AssociationMetada { get; set; }

public bool Id { get; internal set; }
public GeneratedValueStategies GeneratedValueStategy { get; internal set; }
public string Name { get; internal set; }
Expand Down
19 changes: 19 additions & 0 deletions Orm/Mapping/MetadataFactoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,24 @@ public ClassMetadata GetMetadataForClass(string className)
throw new ApplicationException("No metadata found for class " + className);
}
}

protected void FillAssociationInfo()
{
// Fill both ends on association columns
foreach (var Cls in this.ClassMetadata.Values) {
foreach(var Col in Cls.Columns) {
if(Col.AssociationMetada != null) {
if (Col.AssociationMetada.AssociationType == AssociationTypes.ManyToOne) {
var OtherEnd = GetMetadataForClass(Col.MemberType);

Col.AssociationMetada.OtherEndClass = OtherEnd;

// We only support association to Id column for now
Col.AssociationMetada.OtherEndColumn = OtherEnd.Columns.GetIdColumns()[0];
}
}
}
}
}
}
}
8 changes: 8 additions & 0 deletions Orm/Orm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Attributes\Association.cs" />
<Compile Include="Attributes\Id.cs" />
<Compile Include="Attributes\ManyToOne.cs" />
<Compile Include="Attributes\OneToOne.cs" />
<Compile Include="Attributes\OneToMany.cs" />
<Compile Include="Attributes\ManyToMany.cs" />
<Compile Include="Attributes\Model.cs" />
<Compile Include="Attributes\Column.cs" />
<Compile Include="Attributes\Entity.cs" />
Expand Down Expand Up @@ -84,6 +90,8 @@
<Compile Include="IdentifierStategies.cs" />
<Compile Include="IEntityManager.cs" />
<Compile Include="IRepository.cs" />
<Compile Include="Mapping\AssociationMetadata.cs" />
<Compile Include="Mapping\AssociationTypes.cs" />
<Compile Include="Mapping\ClassMetadata.cs" />
<Compile Include="Mapping\ClassMetadataCollection.cs" />
<Compile Include="Mapping\ColumnMetadata.cs" />
Expand Down

0 comments on commit 255678e

Please sign in to comment.