Mapping profiles #148
-
Hello,
This approach seems to be more modular. Since nested properties are still not available via |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I have a workaround that provides some functionality similar to this. It could be extended to work this way if necessary, I think, without too much further issues. In my ApplicationSieveProcessor class, I have the following: public class ApplicationSieveProcessor : SieveProcessor
{
public ApplicationSieveProcessor(IOptions<SieveOptions> options, ISieveCustomSortMethods customSortMethods, ISieveCustomFilterMethods customFilterMethods) : base(options, customSortMethods, customFilterMethods)
{
}
protected override SievePropertyMapper MapProperties(SievePropertyMapper mapper)
{
List<MethodInfo> fluentMethods = Assembly.GetExecutingAssembly().GetReferencedAssemblies()
.SelectMany(a => Assembly.Load(a).GetTypes().Where(t => t != GetType() && t.GetMethods(BindingFlags.Static | BindingFlags.Public)
.Any(m => m.ReturnType == typeof(SievePropertyMapper) &&
m.GetParameters().Length == 1 &&
m.GetParameters().Any(p => p.ParameterType == typeof(SievePropertyMapper))))
.SelectMany(t => t.GetMethods(BindingFlags.Static | BindingFlags.Public)
.Where(m => m.ReturnType == typeof(SievePropertyMapper) &&
m.GetParameters().Length == 1 &&
m.GetParameters().Any(p => p.ParameterType == typeof(SievePropertyMapper))))
).ToList();
fluentMethods.ForEach(fm => mapper = (SievePropertyMapper)fm.Invoke(fm.Name, new[] { mapper }));
mapper = base.MapProperties(mapper);
return mapper;
}
} Basically, find any static method in this Assembly or any Referenced Assembly which takes a SievePropertyMapper and returns a SievePropertyMapper, invoke it with mapper, and then pass it to base. Afterwards, in any Model or DTO I have that might use Sieve, I can do the following: public class Foo
{
public int FooId { get; set; }
[Sieve(CanFilter = true, CanSort = true)]
public string Name { get; set; }
public virtual Bar { get; set; }
public static SievePropertyMapper MapProperties(SievePropertyMapper mapper)
{
mapper.Property<Foo>(f => f.Bar.Name)
.CanFilter()
.CanSort()
.HasName("BarName");
mapper.Property<Foo>(f => f.Bar.State)
.CanFilter()
.CanSort()
.HasName("BarState");
}
} All the configuration for the entity is with the entity, and I can mix and match decorators and fluid API! |
Beta Was this translation helpful? Give feedback.
-
Hi, If anyone is interested, I created #162 that follows an approach inspired by the Entitty Framework EntityType Conifgurations to split mappings into modules/profiles/configurations or however you want to call them. |
Beta Was this translation helpful? Give feedback.
-
#162 is approved. |
Beta Was this translation helpful? Give feedback.
Hi,
If anyone is interested, I created #162 that follows an approach inspired by the Entitty Framework EntityType Conifgurations to split mappings into modules/profiles/configurations or however you want to call them.