Hey,
I have a small challange that I guess/hope can be implemented in a low level fashion.
In my base model I have a UserGuid that is persisted in the DB, but I will also have a "virtual" property UserName that is added from a Dictionary (Cached from the db) after the object has been loaded.
How can I hijack this so I can enrich my model just after Db load?
Best Christian,
There is no class handler in RepoDB, I am thinking of it before. But I guess it is a bit high-level and edge case which would only happen 1% or 0.5% of all cases. But, there is a feature called Property Handler in which you can hook up (DURING the object is being transformed), (not AFTER the object has been loaded).
I would assume you have the models below (or almost identical).
public class YourBaseModel
{
public Guid UserGuid { get; set; }
[PropertyHandler(typeof(UserNamePropertyHandler))]
public virtual string UserName {get; set; }
}
public class YourEntityModel : YourBaseModel
{
public string Property1 { get; set; }
...
}
You should create the Property Handler like below.
public class UserNamePropertyHandler : IPropertyHandler<string, string>
{
public string Get(string input, ClassProperty property)
{
// This event is being triggered before the property is set back to the model
// --->>>> Get the cache data here
}
public string Set(string input, ClassProperty property)
{
// This event is being triggered before the property is saved to the DB
}
}
If you do not like the attribute PropertyHandler, you can also do map it via FluentMapper like below.
FluentMapper.Entity<YourEntityModel>().PropertyHandler<UserNamePropertyHandler >(e => e.UserName);
Hope this helps!
Okay kind a work, but it seems like I can only work on one property.
Situation:
Property A is mapped in DB
Property B is only in model.
As I see this, I can only manipulate property A based on field A (value in Db).
But not set/manipulate property B based on the value of A?
Is that correct?
Yes, you are really needing a class handler in which we did not implemented. Also, I just realized that the property handler may not be triggered as it is not mapped to any column of your DB.
Are you blocked here? I am just a bit pushy on the reco above, but upon further understanding your case, IMHO, I would say that you just simply customize the 'getter' of your property in the Model and get the value directly from cache or from any other Model properties.
Maybe you do not need an event of the 'After Model Load', but maybe a trick below would do?
public class YourBaseModel
{
public Guid UserGuid { get; set; }
public virtual string UserName => GetUserNameValue();
private string GetUserNameValue()
{
var value = "Get From Cache";
return $"User-{UserGuid}-{value}";
}
}
public class YourEntityModel : YourBaseModel
{
public string Property1 { get; set; }
...
}
Hey @Kodestuen - how is the status on your side here?
Hi @mikependon,
I would like to keep my model as clean as possible and my challenge is that I fetch the UserName from the DB itself (cached)
private string GetUserNameValue(Dictionary<Guid, string> users)
{
//var value = "Get From Cache"; //Should not know this internally
return users[UserGuid];
}
I have tried to solve it in my (repository handler).
But need to surround this on all functions before they return. All, Select, Insert etc.
```
public IEnumerable
{
list.ForEach(f => f.UserName = f.CreatedByUserId.GetUserName(DbHandler)); //GetUserName is an extension method
return list.AsEnumerable();
}
So yes a ClassHandler :-)
[ClassHandler(typeof(SomeClassHandler))]
public class YourBaseModel
{
public Guid UserGuid { get; set; } // in DB
public string UserName {get; set; } // SomeClassHandler will set this value based on UserGuid
}
public class SomeClassHandler: IClassHandler
{
public YourBaseModel Get(YourBaseModel object)
{
// Manipulate/transform as you like....
return object;
}
}
```
To be honest, it is not that difficult to introduce this. I am thinking to include this on the next beta release. But I am just afraid that my upcoming release is now bloating up. You can see the upcoming release here. A lot of requests from F# community and some other additional requests from C# community has also been introduce, including this? :)
In anyway, let me think of it and will let you know once I included it on the next beta release. This is a good reconsideration.
@mikependon you are the best. I think this project is some of the coolest work I ever have seen. THANK you :-)
Thanks! Just leave this one open, I will make a comment here moving forward. For now, please do a manual work around on your end. Cheers!
@Kodestuen - would you be able to check the class handler support on the latest release (RepoDB v1.12.0-beta1)?
Closing this ticket now as the Class Handler feature is introduced to support this. Please do let us know if you have any further questions.