Skip to content

Commit

Permalink
Merge pull request #514 from paillave/v
Browse files Browse the repository at this point in the history
V
  • Loading branch information
paillave authored Nov 27, 2024
2 parents 2fa6de6 + 73d20f7 commit 725247b
Show file tree
Hide file tree
Showing 11 changed files with 388 additions and 144 deletions.
16 changes: 8 additions & 8 deletions src/Paillave.EntityFrameworkCoreExtension/EfSave/EfSaveEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ private void InsertOrUpdateEntity(bool doNotUpdateIfExists, DbSet<T> contextSet,
{
var entityCondition = _findConditionExpression.ApplyPartialLeft(entity);
var existingEntity = contextSet.AsNoTracking().FirstOrDefault(entityCondition);
if (this._context is MultiTenantDbContext mtCtx)
{
mtCtx.UpdateEntityForMultiTenancy(entity);
}
if (existingEntity == null)
{
_context.Entry(entity).State = EntityState.Added;
Expand All @@ -107,10 +103,14 @@ private void InsertOrUpdateEntity(bool doNotUpdateIfExists, DbSet<T> contextSet,
var val = keyPropertyInfo.GetValue(existingEntity);
keyPropertyInfo.SetValue(entity, val);
}
if (!doNotUpdateIfExists)
{
contextSet.Update(entity);
}
}
if (this._context is MultiTenantDbContext mtCtx)
{
mtCtx.UpdateEntityForMultiTenancy(entity);
}
if (existingEntity != null && !doNotUpdateIfExists)
{
contextSet.Update(entity);
}
}
}
112 changes: 55 additions & 57 deletions src/Paillave.Etl.Autofac/AutofacDependencyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,66 @@
using Autofac;
using Paillave.Etl.Core;

namespace Paillave.Etl.Autofac
namespace Paillave.Etl.Autofac;
public class AutofacDependencyResolver : IDependencyResolver
{
public class AutofacDependencyResolver : IDependencyResolver
private SimpleDependencyResolver _dependencyResolver = new SimpleDependencyResolver();
private readonly IComponentContext _componentContext;
public AutofacDependencyResolver(IComponentContext componentContext) => (_componentContext) = (componentContext);
public object Resolve(Type type)
{
private SimpleDependencyResolver _dependencyResolver = new SimpleDependencyResolver();
private readonly IComponentContext _componentContext;
public AutofacDependencyResolver(IComponentContext componentContext) => (_componentContext) = (componentContext);
public object Resolve(Type type)
{
var res = _dependencyResolver.Resolve(type);
if (res != null) return res;
res = _componentContext.Resolve(type);
_dependencyResolver.Register(type, res);
return res;
}
public T Resolve<T>() where T : class
{
var res = _dependencyResolver.Resolve<T>();
if (res != null) return res;
res = _componentContext.Resolve<T>();
_dependencyResolver.Register<T>(res);
return res;
}
public T Resolve<T>(string key) where T : class
{
var res = _dependencyResolver.Resolve<T>(key);
if (res != null) return res;
res = _componentContext.ResolveKeyed<T>(key);
_dependencyResolver.Register<T>(res, key);
return res;
}
var res = _dependencyResolver.Resolve(type);
if (res != null) return res;
res = _componentContext.Resolve(type);
_dependencyResolver.Register(type, res);
return res;
}
public T Resolve<T>() where T : class
{
var res = _dependencyResolver.Resolve<T>();
if (res != null) return res;
res = _componentContext.Resolve<T>();
_dependencyResolver.Register<T>(res);
return res;
}
public T Resolve<T>(string key) where T : class
{
var res = _dependencyResolver.Resolve<T>(key);
if (res != null) return res;
res = _componentContext.ResolveKeyed<T>(key);
_dependencyResolver.Register<T>(res, key);
return res;
}

public bool TryResolve<T>(out T resolved) where T : class
{
resolved = default;
return _componentContext.TryResolve<T>(out resolved);
}
public bool TryResolve<T>(out T resolved) where T : class
{
resolved = default;
return _componentContext.TryResolve<T>(out resolved);
}

public bool TryResolve<T>(string key, out T resolved) where T : class
{
resolved = default;
return _componentContext.TryResolveKeyed<T>(key, out resolved);
}
public bool TryResolve<T>(string key, out T resolved) where T : class
{
resolved = default;
return _componentContext.TryResolveKeyed<T>(key, out resolved);
}

public bool TryResolve(Type type, out object resolved)
{
return _componentContext.TryResolve(type, out resolved);
}
public bool TryResolve(Type type, out object resolved)
{
return _componentContext.TryResolve(type, out resolved);
}

public object Resolve(Type type, string key)
{
var res = _dependencyResolver.Resolve(type, key);
if (res != null) return res;
res = _componentContext.ResolveKeyed(key, type);
_dependencyResolver.Register(type, res, key);
return res;
}
public object Resolve(Type type, string key)
{
var res = _dependencyResolver.Resolve(type, key);
if (res != null) return res;
res = _componentContext.ResolveKeyed(key, type);
_dependencyResolver.Register(type, res, key);
return res;
}

public bool TryResolve(Type type, string key, out object resolved)
{
resolved = default;
return _componentContext.TryResolveKeyed(key, type, out resolved);
}
public bool TryResolve(Type type, string key, out object resolved)
{
resolved = default;
return _componentContext.TryResolveKeyed(key, type, out resolved);
}
}
}
28 changes: 24 additions & 4 deletions src/Paillave.Etl.XmlFile/Core/XmlObjectReaderV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,44 @@ private class PropertyBag
public IXmlNodeDefinition XmlNodeDefinition { get; }
private readonly List<XmlFieldDefinition> _xmlFieldDefinitions;
private readonly HashSet<string> _valuesPath;
private readonly Dictionary<string, string> _xmlValues = new Dictionary<string, string>();

private readonly Dictionary<string, string> _xmlValues = new();
public PropertyBag(string sourceName, IXmlNodeDefinition xmlNodeDefinition)
{
SourceName = sourceName;
this.XmlNodeDefinition = xmlNodeDefinition;
_xmlFieldDefinitions = xmlNodeDefinition.GetXmlFieldDefinitions().ToList();
this._valuesPath = _xmlFieldDefinitions.Select(i => i.NodePath).ToHashSet();
}

public void SetValue(string key, string? value)
{
if (string.IsNullOrWhiteSpace(value))
return;

// ^(?<segment>/(?<segmentName>[^/[]+)([[](?<segmentFilter>(?<segmentFilterAttribute>[^]=]+)=""(?<segmentFilterValue>[^""]*)"")[]])?)+$
// /zer[sdfsdfsd="er"]/wfxf/trtr[qdff="bg"]
// https://regex101.com/r/tG1jF6/1
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
if (_valuesPath.Contains(key))
_xmlValues[key] = value;
}

// public void SetValue(string key, string? value, Dictionary<string, string> xmlAttributes)
// {
// if (string.IsNullOrWhiteSpace(value))
// return;
// if (TryGetPathMatch(key, xmlAttributes, out var pathMatch) && pathMatch != null)
// _xmlValues[pathMatch] = value;
// }
// private bool TryGetPathMatch(string key, Dictionary<string, string> xmlAttributes, out string? pathMatch)
// {
// pathMatch = null;
// if (_valuesPath.Contains(key))
// {
// pathMatch = key;
// return true;
// }
// return false;
// }

public object CreateRow()
{
var objectBuilder = new ObjectBuilder(XmlNodeDefinition.Type);
Expand Down
92 changes: 92 additions & 0 deletions src/Paillave.Etl.XmlFileTests/XmlObjectReaderV2Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,98 @@ namespace Paillave.Etl.XmlFileTests;

public class XmlObjectReaderV2Tests
{
// [Fact]
// public void Read_SimpleXmlWithAttributes_ParsesCorrectly3()
// {
// // Arrange
// var xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
// <root rootId=""a root id"">
// <data>root data</data>
// <person id=""1"" firstName=""John"" lastName=""Doe"">
// <age>30</age>
// <extraFields>
// <field name=""street"">123 Main St</field>
// <field name=""city"">Springfield</field>
// </extraFields>
// </person>
// <person id=""2"" firstName=""Coucou"">
// <age>32</age>
// </person>
// <company id=""3"" name=""MyCompany"">
// <age>32</age>
// <extraFields>
// <field name=""city"">Big City</field>
// </extraFields>
// </company>
// </root>";

// var definition = new XmlFileDefinition();
// definition.AddNodeDefinition(
// "person",
// "/root/person",
// i => new TestPerson
// {
// Id = i.ToXPathQuery<int>("/root/person/@id"),
// FirstName = i.ToXPathQuery<string>("/root/person/@firstName"),
// LastName = i.ToXPathQuery<string>("/root/person/@lastName"),
// Age = i.ToXPathQuery<int>("/root/person/age"),
// Street = i.ToXPathQuery<string>(@"/root/person/extraFields/field[@name=""street""]"),
// City = i.ToXPathQuery<string>(@"/root/person/extraFields/field[@name=""city""]"),
// RootId = i.ToXPathQuery<string>("/root/@rootId"),
// RootData = i.ToXPathQuery<string>("/root/data"),
// });
// definition.AddNodeDefinition(
// "company",
// "/root/company",
// i => new TestCompany
// {
// Id = i.ToXPathQuery<int>("/root/company/@id"),
// Name = i.ToXPathQuery<string>("/root/company/@name"),
// Street = i.ToXPathQuery<string>(@"/root/company/extraFields/field[@name=""street""]"),
// City = i.ToXPathQuery<string>(@"/root/company/extraFields/field[@name=""city""]"),
// RootId = i.ToXPathQuery<string>("/root/@rootId"),
// RootData = i.ToXPathQuery<string>("/root/data"),
// });

// var results = new List<XmlNodeParsed>();
// var reader = new XmlObjectReaderV2(definition, "test", results.Add);

// // Act
// using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
// {
// reader.Read(stream, CancellationToken.None);
// }

// var people = results.FindAll(i => i.NodeDefinitionName == "person").ToList();
// var person1 = people[0].Value as TestPerson;
// Assert.Equal(1, person1.Id);
// Assert.Equal("John", person1.FirstName);
// Assert.Equal("Doe", person1.LastName);
// Assert.Equal(30, person1.Age);
// Assert.Equal("123 Main St", person1.Street);
// Assert.Equal("Springfield", person1.City);
// Assert.Equal("a root id", person1.RootId);
// Assert.Equal("root data", person1.RootData);
// var person2 = results[1].Value as TestPerson;
// Assert.Equal(2, person2.Id);
// Assert.Equal("Coucou", person2.FirstName);
// Assert.Null(person2.LastName);
// Assert.Equal(32, person2.Age);
// Assert.Null(person2.Street);
// Assert.Null(person2.City);
// Assert.Equal("a root id", person2.RootId);
// Assert.Equal("root data", person2.RootData);

// var companies = results.FindAll(i => i.NodeDefinitionName == "company").ToList();
// var company1 = companies[0].Value as TestCompany;
// Assert.Equal(3, company1.Id);
// Assert.Equal("MyCompany", company1.Name);
// Assert.Null(company1.Street);
// Assert.Equal("Big City", company1.City);
// Assert.Equal("a root id", company1.RootId);
// Assert.Equal("root data", company1.RootData);
// }

[Fact]
public void Read_SimpleXmlWithAttributes_ParsesCorrectly2()
{
Expand Down
2 changes: 1 addition & 1 deletion src/SharedSettings.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<Version>2.1.36-beta</Version>
<Version>2.1.37-beta</Version>
<PackageIcon>NugetIcon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Authors>Stéphane Royer</Authors>
Expand Down
21 changes: 21 additions & 0 deletions src/Tutorials/Paillave.Etl.Samples/DataAccess/SimpleTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace Paillave.Etl.Samples.DataAccess
{
public class SimpleTable
{
public int Id { get; set; }
public string Name { get; set; }
}
public class SimpleTableConfiguration : IEntityTypeConfiguration<SimpleTable>
{
public void Configure(EntityTypeBuilder<SimpleTable> builder)
{
builder.ToTable(nameof(SimpleTable));
builder.HasKey(i => i.Id);
builder.Property(i => i.Id).UseIdentityColumn();
}
}
}
Loading

0 comments on commit 725247b

Please sign in to comment.