I want to specify a column name convention that basically takes a pascal cased field and converts it to all uppercase with underscores. So property OrderId becomes column "ORDER_ID". I also want this convention to be applied only if I don't already specify one in the mapping. So far I have the skeleton below:
public class PascalCaseColumnNameConvention : IPropertyConvention
{
public bool Accept(IPropertyInstance instance)
{
//Not sure what I should have here
}
public void Apply(IPropertyInstance instance)
{
instance.Column(instance.Property.Name.ChangePascalCaseToUnderscore());
}
}
Also is there a better way of channging the case besides an string extension method? Any libraries that already do this sort of thing?
In Accept method in this case you should just return true. This method is to decide whether your convention should apply for a given instance, for example you might want to change the default name only for your int-typed fields etc. If you're defining a general convention, true means just "accept all instances". It can be still overriden by specyfying the column name in the mapping, though.
About changing the case - I don't know such a library, either. But the task is quite simple, so your solution seems very reasonable here.
EDIT
Well, what FNH version are you using? I can't see IPropertyConvention to have bool Accept method.
What you can do here is to implement both IPropertyConvention (with void Apply only) and IPropertyConventionAcceptance (with Accept method working a bit differently). Try something like that (haven't tested):
public class PascalCaseColumnNameConvention : IPropertyConvention, IPropertyConventionAcceptance
{
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => !x.Columns.HasUserDefined());
}
public void Apply(IPropertyInstance instance)
{
instance.Column(instance.Property.Name.ChangePascalCaseToUnderscore());
}
}
I've also found a neat class doing lot of useful string conversions - see Inflector. But anyway, if you need only this particular conversion, I would stay with own simple solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With