Good morning everyone.
I have a webapi project assembled with the repository pattern and I need to make a modification to the answer that the _repository.getByFilter(...)
method offers me. This returns a IQueryable
and I need to go through all the objects in this list checking if any of the properties has a custom attribute ( EncryptedData
). If so, encrypt the value.
The problem is that these objects can have properties that are ICollection
or other objects of own classes.
To try to solve this, I have mounted these three functions:
private IQueryable<T> EncryptDecrypt(IQueryable<T> queryable, bool encrypt)
{
var encriptedProperty = typeof(T).GetProperties().Where(
p => p.CustomAttributes.Any(at => at.AttributeType == typeof(Konecta.Core.Attributes.EncryptedData))
);
var collectionProperty = typeof(T).GetProperties().Where(p => p.PropertyType.Name.Contains("ICollection"));
if (encriptedProperty.Count() != 0 || collectionProperty.Count() != 0)
{
foreach (T obj in queryable)
{
if (encriptedProperty.Count() > 0)
{
foreach (var ep in encriptedProperty)
{
ep.SetValue(obj, Convert.ChangeType(DecryptDB(ep.GetValue(obj, null).ToString()), ep.PropertyType), null);
}
}
if (collectionProperty.Count() > 0)
{
foreach (var cp in collectionProperty)
{
EncryptDecrypt((ICollection<cp.PropertyType>)cp.GetValue(obj, null), true);
}
}
}
}
return queryable;
}
private ICollection<T> EncryptDecrypt(ICollection<T> collection, bool encrypt)
{
var encriptedProperty = typeof(T).GetProperties().Where(
p => p.CustomAttributes.Any(at => at.AttributeType == typeof(Konecta.Core.Attributes.EncryptedData))
);
var collectionProperty = typeof(T).GetProperties().Where(p => p.PropertyType.Name.Contains("ICollection"));
if (encriptedProperty.Count() == 0 && collectionProperty.Count() == 0)
{
foreach (T obj in collection)
{
if (encriptedProperty.Count() > 0)
{
foreach (var ep in encriptedProperty)
{
ep.SetValue(obj, Convert.ChangeType(DecryptDB(ep.GetValue(obj, null).ToString()), ep.PropertyType), null);
}
}
if (collectionProperty.Count() > 0)
{
foreach (var cp in collectionProperty)
{
EncryptDecrypt((ICollection<cp.PropertyType>)cp.GetValue(obj, null), true);
}
}
}
}
return collection;
}
private T EncryptDecrypt(T entity, bool encrypt)
{
return entity;
}
In my business logic layer I have the following line:
EncryptDecrypt(_repository.SelectByFilter(where), true);
So I get that, the result of SelectByFilter
between the first of my functions (still halfway), I check if any property has my custom attribute and, if so, the encrypted.
Then I check if any property is a collection, and send the value to the second function.
The problem is that the line
EncryptDecrypt((ICollection<cp.PropertyType>)cp.GetValue(obj, null), true);
of the first function gives me an error:
can not convert
ICollection<cp.PropertyType>
toIQueryable<T>
.
Can someone tell me what I'm doing wrong?
I would also like to know if someone can tell me some more efficient way to do all this so as not to need so many functions.
Greetings and thanks.