I am having trouble with generics. Using examples from here, I want to pass a property for use in EF Core. I have a feeling I am overcomplicating things. Note that I would like to stick with lamba expressions instead of passing something like nameof (which I cannot use in 4.0).
class Foo
{
public string A { get; set; } // some unique string in DbSet for entity Person
public string B { get; set; } // some unique string in DbSet for entity Company
public int GetId<TEntity>(string match, Expression<Func<TEntity, string>> fieldToExamine, Expression<Func<TEntity, int>> fieldWithId, System.Data.Entity.DbContext context)
where TEntity : class
{
int retval = -1;
if (!string.IsNullOrEmpty(match))
{
var expr = (MemberExpression)fieldToExamine.Body;
var prop = (PropertyInfo)expr.Member;
var expr2 = (MemberExpression)fieldWithId.Body;
var idField = (PropertyInfo)expr2.Member;
// this works because everything is explicit
var entity = context.Set<Person>().SingleOrDefault(p => p.PersonName.Equals(match));
// ... but I want that to be generic
// how to reference the property to be evaluated?
//var entity = context.Set<TEntity>().SingleOrDefault(p => ...); // <- HELP?!
if (entity != null)
{
retval = (int)entity.GetType().GetProperty(idField.Name).GetValue(entity, null);
}
}
return retval;
}
}
static void Main(string[] args)
{
// .. omitted database stuff ..
// (for Person) what I want is to match the Name field (here: p.Name) of the Person entity to the Foo property value (here: f.A) and return the database id stored in p.PersonId
int x = f.GetId<Person>(f.A, p => p.PersonName, p => p.PersonId, context);
// same for Company, but different values and fields
int y = f.GetId<Company>(f.B, p => p.CompanyName, p => p.CompanyId, context);
}
Person class and Company class will need to implement either abstract class or interface.
public abstract class BaseEntity
{
public abstract int Id { get; set; }
public abstract string Name { get; set; }
}
public class Person : BaseEntity
{
public int PersonId { get; set; }
public override string Name { get; set; }
public override int Id
{
get { return PersonId; }
set { PersonId = value; }
}
}
public class Company : BaseEntity
{
public int CompanyId { get; set; }
public override string Name { get; set; }
public override int Id
{
get { return CompanyId; }
set { CompanyId = value; }
}
}
Then you can pass p.Name and p.CompanyId.
var entity = context.Set<TEntity>().SingleOrDefault(p => p.Name.Equals(match));
Related
Is there a way to perform a LINQ query on Entity Framework DBSets and only return the properties that have a specific custom attribute?
My goal is to perform a query and return only the properties/columns that I need for an export.
I also want to make this an extension for IEnumerable as I have many separate EF Classes that will use this export attribute.
This is how I am visualizing it:
public class Person
{
[Export]
public string FirstName { get; set; }
[Export]
public string LastName { get; set; }
[Export]
public string Address { get; set; }
[NeverExport]
public string SocialSecurityNumber { get; set; }
}
public void main()
{
var people = PersonRepository.GetAll();
var exportResults = people.GetByAttribute(ExportAttribute);
{
public static IEnumerable<object> GetByAttribute(this IEnumerable<object> list, Attribute attribute)
{
return list
.Select(x =>
// Select properties with { attribute } (ie. [Export])
);
}
I've made a very basic example for your problem. In short, you want to get all Properties, where a specific Attribute is present. You can achive this via reflection. First, you want all properties of a specific type, then only the properties, where a specific attribute is present.
Here is my example code:
using System;
using System.Linq;
using System.Reflection;
namespace Sandbox
{
public class Program
{
public static void Main(string[] args)
{
var attrs = typeof(User).GetProperties().Where(x => x.GetCustomAttributes().Any(y => y.GetType() == typeof(Custom)));
var users = new User[]
{
new User() { ID = 1, Name = "John", Lastname = "Doe" },
new User() { ID = 2, Name = "Jane", Lastname = "Doe" }
};
foreach(var u in users)
{
foreach(var attr in attrs)
{
Console.WriteLine(typeof(User).GetProperty(attr.Name).GetValue(u, null));
}
}
}
}
public class User
{
public string Name { get; set; }
[Custom]
public int ID { get; set; }
[Custom]
public string Lastname { get; set; }
}
public class Custom : System.Attribute
{
}
}
The following code projects any IQueryable to IQueryable<ExpandoObject> and you can use it for exporting data. Additionally code caches projection expression for reusing later in application.
Usage is simple:
var exportResult = anyQuery.ProjectForExport().ToList();
And implementation:
[AttributeUsage(AttributeTargets.Property)]
public class ExportAttribute : Attribute
{
}
public static class ExportTools
{
private static readonly ConstructorInfo _expandoObjectConstructor =
typeof(ExpandoObject).GetConstructor(Type.EmptyTypes) ?? throw new InvalidOperationException();
private static readonly MethodInfo _expandoAddMethodInfo = typeof(IDictionary<string, object>).GetTypeInfo()
.GetRuntimeMethods()
.Single(mi => mi.Name == nameof(IDictionary<string, object>.Add) && mi.GetParameters().Length == 2);
public static class ProjectionCache<T>
{
public static Expression<Func<T, ExpandoObject>> ProjectionExpression { get; } = BuildProjection();
private static Expression<Func<T, ExpandoObject>> BuildProjection()
{
var param = Expression.Parameter(typeof(T), "e");
var properties = typeof(T).GetProperties()
.Where(p => p.GetCustomAttributes().Any(a => a is ExportAttribute)).ToList();
if (properties.Count == 0)
throw new InvalidOperationException($"Type '{typeof(T).Name}' has no defined Export properties.");
var expr = (Expression)Expression.ListInit(
Expression.New(_expandoObjectConstructor),
properties.Select(p =>
{
var readerExpr = Expression.MakeMemberAccess(param, p);
return Expression.ElementInit(_expandoAddMethodInfo, Expression.Constant(p.Name), readerExpr);
}));
var lambda = Expression.Lambda<Func<T, ExpandoObject>>(expr, param);
return lambda;
}
}
public static IQueryable<ExpandoObject> ProjectForExport<T>(this IQueryable<T> query)
{
return query.Select(ProjectionCache<T>.ProjectionExpression);
}
}
I define class BaseController with Generic Class TEntity.
public class BaseController<TEntity> : Controller
Use:
public class ProductController : BaseController<Product>
{
public ProductController(BaseService<Product> baseService)
: base(baseService)
{
}
}
Class Product has Child properties relationship. It's dynamic follow TEntity (class Product)
public partial class Product
{
[Key]
public long CodeId { get; set; }
public int Name { get; set; }
//Config Ref
[NotMapped]
public ICollection<ProductDetail> ProductDetails { get; set; }
}
public partial class ProductDetail
{
[Key]
public long CodeId { get; set; }
public int Name { get; set; }
}
TEntity now is Product. And I get property of ProductDetails.
var tEn = JsonUtilities.ConvertObjectToObject<TEntity>(obj);
var propsInfo = (typeof(TEntity)).GetProperties();
var propsCollection = propsInfo.Where(x => x.PropertyType.IsGenericType
&& x.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>));
foreach (var prop in propsCollection)
{
dynamic childItems = prop.GetValue(tEn);
var genericType = prop.PropertyType.GenericTypeArguments[0];
var tableProp = context.GetType().GetProperties().FirstOrDefault(x => x.Name == genericType.Name);
foreach (dynamic itemChild in childItems)
{
// Convert here for setting some property value
var dy = JsonUtilities.ConvertObjectToObject<dynamic>(itemChild);
foreach (KeyValuePair<string, string> itemKey in dic)
{
itemChild[itemKey.Value.Trim()] = "DATA" ; << dynamic property
}
// QUESTION HERE
var itemChild2 = JsonUtilities.ConvertObjectToObject<ABC???>(dy); <<<<<
}
}
Now, how can I can convert again generic itemChild with Class Object ProductDetail
var dy = JsonUtilities.ConvertObjectToObject<ProductDetail>(itemChild); <<< I can not put ProductDetail here because, It's child of generic object Product.
I have TEntity is Product, and in Product have ProductDetails is CHILD. (I've used name Product & ProductDetail for you easy understand.)
Now, I've TEntity is Product. I can get Child Instant of Class ProductDetail. maybe,call 'childClass'.
After that, I convert 'childClass' to dynamic object, My Question is How to convert again to 'childClass' from dynamic Object.
You need to use an Interface. Every type that might be used in such scenario (ex. ProductDetail) should implement that interface. In this case, there is no need to multiple conversions.
I've recapped this.
namespace Project
{
class Product
{
public int Id {get;set;}
public string Name {get;set;}
public ICollection<ProductDetail> ProductDetails {get;set;}
}
class ProductDetail : IInjectable
{
public int Key {get;set;}
public string Value {get;set;}
}
interface IInjectable
{
int Key {get;set;}
string Value {get;set;}
}
class Worker
{
void Do<TEntity>(TEntity entity, Dictionary<int, string> dic)
{
typeof(TEntity)
.GetProperties()
.Where(p => p.PropertyType.IsGenericType &&
typeof(IEnumerable).IsAssignableFrom(p.PropertyType))
.Select(p =>
{
var genType = p.PropertyType
.GetGenericArguments()
.FirstOrDefault();
return new
{
Property = p,
GenericType = genType,
};
})
.Where(item => typeof(IInjectable).IsAssignableFrom(item.GenericType))
.ToList()
.ForEach(item =>
{
var enumerableValue = item.Property
.GetValue(entity) as IEnumerable;
foreach (var v in enumerableValue)
{
if (v is IInjectable injectable)
{
injectable.Value = dic[injectable.Key];
}
}
});
}
}
}
You can make the call of the generic method using reflection.
MethodInfo method = typeof(JsonUtilities).GetMethod(nameof(ConvertObjectToObject));
MethodInfo generic = method.MakeGenericMethod(typeof(prop.PropertyType));
var itemChild2 = generic.Invoke(this, dy);
I'm having problems trying to get Should().BeEquivalentTo() to work with types that derive from a base class and implement a collection interface:
public class Entity
{
public string Id {get; set;}
public string Name {get; set;}
}
public class Derived : Entity, ICollection<Entity>
{
private List<Entity> m_Children = new List<Entity>();
public string Description { get; set; }
public int Count => ((ICollection<Entity>)m_Children).Count;
public bool IsReadOnly => ((ICollection<Entity>)m_Children).IsReadOnly;
public void Add(Entity item)
{
((ICollection<Entity>)m_Children).Add(item);
}
public void Clear()
{
((ICollection<Entity>)m_Children).Clear();
}
public bool Contains(Entity item)
{
return ((ICollection<Entity>)m_Children).Contains(item);
}
public void CopyTo(Entity[] array, int arrayIndex)
{
((ICollection<Entity>)m_Children).CopyTo(array, arrayIndex);
}
public IEnumerator<Entity> GetEnumerator()
{
return ((ICollection<Entity>)m_Children).GetEnumerator();
}
public bool Remove(Entity item)
{
return ((ICollection<Entity>)m_Children).Remove(item);
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((ICollection<Entity>)m_Children).GetEnumerator();
}
}
The Test
[TestMethod]
public void EquivalenceTest()
{
var expected = new Derived
{
Id = "123",
Name = "abc",
Description = "def"
};
var actual = new Derived
{
Id = "121",
Name = "xyz",
Description = "def"
};
actual.Should().BeEquivalentTo(expected); // This succeeds, but should fail
}
The call to BeEquivalentTo seems to be ignoring the properties that are defined in the object, and only treating the object as a collection.
How can I get the framework to check the properties and the contents of the collection?
Edit
It seems like this is a known issue
Does anyone know of a workaround?
It's a known issue when comparing classes that implements IEnumerable and have extra properties to be compared.
Here's a way to hack the comparison.
public class Entity : IEnumerable<int>
{
private int[] ints = new[] { 1 };
public int Id { get; set; }
public string Name { get; set; }
public IEnumerator<int> GetEnumerator() => ((IEnumerable<int>)ints).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable<int>)ints).GetEnumerator();
}
[TestMethod]
public void EquivalenceTest()
{
var expected = new Entity
{
Id = 1,
Name = "abc",
};
var actual = new Entity
{
Id = 1,
Name = "abc",
};
actual.Should().BeEquivalentTo(expected, opt => opt
.Using<Entity>(e =>
e.Subject.Should().Match<Entity>(f => f.Name == e.Expectation.Name)
.And.Subject.Should().Match<Entity>(f => f.Id == e.Expectation.Id)
.And.Subject.Should().BeEquivalentTo(e.Expectation)
)
.WhenTypeIs<Entity>());
}
I am working with a legacy database, within this database, the data get assigned the maximum length of the column. if the string data is shorter, it will automaticly fill in whitespaces at the end.
What i'm trying to do is trim all these ending whitespaces with every query i do.
i figured one of the better ways would be making an extension method for dapper query using reflection.
But i can't seem to get it to work.
parent entity:
public class Person: BaseEntity
{
public Identification Identification { get; set; }
public Address Address { get; set; }
public Contact Contact { get; set; }
public Family Family { get; set; }
public ICollection<Payment> Payments { get; set; }
}
example of child entity:
public class Address: BaseEntity
{
public string Street { get; set; }
public int Number { get; set; }
public string BoxNumber { get; set; }
public int ZipCode { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
Now i do my join query like this:
var result = _db.QueryTrim<dynamic>(sql, new { userid = id })
.Select(p => new Person()
{
Identification = IdentificationMapper.MapToIdentificationEntity(p),
Address = AddressMapper.MapToAddressEntity(p)}).First();
i am trying to implement something like this but i can't get it to work with query
public static class DapperExtensions {
public static IEnumerable<T> Query<T>(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null) {
var dapperResult = SqlMapper.Query<T>(cnn, sql, param, transaction, buffered, commandTimeout, commandType);
var result = TrimStrings(dapperResult.ToList());
return result;
}
static IEnumerable<T> TrimStrings<T>(IList<T> objects) {
//todo: create an Attribute that can designate that a property shouldn't be trimmed if we need it
var publicInstanceStringProperties = typeof (T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType == typeof (string) && x.CanRead && x.CanWrite);
foreach (var prop in publicInstanceStringProperties) {
foreach (var obj in objects) {
var value = (string) prop.GetValue(obj);
var trimmedValue = value.SafeTrim();
prop.SetValue(obj, trimmedValue);
}
}
return objects;
}
static string SafeTrim(this string source) {
if (source == null) {
return null;
}
return source.Trim();
}
}
In the end i want to trim all string that come out of the database. At the moment i'm tunnelvisioning on the dapperextension, but if there is any better way. Please let me know.
If you are using Dapper version 1.50.2 you can do it in a simpler way.
Create a TypeHandler like the one bellow:
public class TrimmedStringHandler : SqlMapper.TypeHandler<string>
{
public override string Parse(object value)
{
string result = (value as string)?.Trim();
return result;
}
public override void SetValue(IDbDataParameter parameter, string value)
{
parameter.Value = value;
}
}
On the program initialization you must call the AddTypeHandler method of the SqlMapper class like bellow:
SqlMapper.AddTypeHandler(new TrimmedStringHandler());
If you do this, every string that come from database will be trimmed.
I have following situation:
public class BaseClass
{
public string Field1 { get; set; }
public string Field2 { get; set; }
public string Field3 { get; set; }
}
public class Product: BaseClass
{
/* Some methods*/
}
public class Country: BaseClass
{
/* Some methods*/
}
public class MyCustomClass
{
public Product Product { get; set; }
public Country Country { get; set; }
}
I want to create a list of expressions which I would later use to query my MyCustomClass, example
var listOfExpressions= new List<Expression<Func<MyCustomClass, bool>>>();
if (x == 1)
listOfExpressions.Add(x => x.Product.Field1 == "Fruit")
/*.
.
.*/
if (y == 2)
listOfExpressions.Add(x => x.Country.Field2 == "Western")
}
Now this looks really ugly with these bunch of Ifs and I would prefer to have a method in BaseClass for which you would pass int ({1,2,3}) and it would return me the expression. The problem is that I need to have expression of type <MyCustomClass, bool> but in my BaseClass I can't directly get that, is there an way I could acheive this without complicating code too much?
I am thinking about something like this:
public class BaseClass
{
public string Field1 { get; set; }
public string Field2 { get; set; }
public string Field3 { get; set; }
protected Expression<Func<BaseClass, bool>> GetExpression(int key, string value)
{
switch (key)
{
case 1:
return x => x.Field1 == value;
case 2:
return x => x.Field2 == value;
case 3:
return x => x.Field3 == value;
}
}
By now I am stuck of how to get Expression<Func<BaseClass, bool>> to be used in Expression<Func<MyCustomClass, bool>>.
EDIT: The purpose of the expression:
I want to constuct a list of expressions to later use it to query database using Entity framework core. Example:
var query = DbContext.MyCustomClass.AsQueryable();
foreach (var expression in listOfExpressions)
{
query = query.Where(expression);
}
You can build your Expression on you own. Just add another parameter to GetExpression:
public static Expression<Func<MyCustomClass, bool>> GetExpression(
string derrivedName, // "Product" or "Country"
int fieldId,
string value)
{
var x = Expression.Parameter(typeof(MyCustomClass), "x");
var lambda = Expression.Lambda<Func<MyCustomClass, bool>>(
Expression.Equal(
Expression.Constant(value),
Expression.PropertyOrField(
Expression.PropertyOrField(x, derrivedName),
$"Field{fieldId}")
),
x);
return lambda;
}
Now you can use it like:
var exp3 = GetExpression("Product", 1, "Fruit");
This line will create an expression x => ("Fruit" == x.Product.Field1)