Overwriting default conventions


On my objects I need dates that are more meaningful..
So i created

public class CustomDatetimeMemberConvention : ITypeFieldConvention, ITypePropertyConvention
    Random rand = new Random();

    public void Apply(ITypePropertyConventionContext context)
        context.SetValue(DateTime.Now.AddDays(Math.Floor(365 * rand.NextDouble())));

    public void Apply(ITypeFieldConventionContext context)
        context.SetValue(DateTime.Now.AddDays(Math.Floor(365 * rand.NextDouble())));

    public void SpecifyRequirements(ITypeMemberConventionRequirements requirements)
        requirements.Type(x => x == typeof(DateTime));
        requirements.Type(x => x == typeof(DateTime?));
this basically generates a sometime in the next year.
my question is, how do i set up the container or engine or whatever to use this whenever it finds a date instead of your default conventions.
the documentation section on conventions doesn't seem to have this info available and i've tried to follow the code but i'm getting lost...


robashton wrote Jan 13, 2011 at 12:41 PM

This is a known problem with 1.1, you do it by doing

.Conventions(c = > c.Include<MyConvention>

Or something to that end, I am on my phone and cannot look it up.

However, if you want to mix default conventions in there is no notion of custom conventions taking precedence so you either have to not use them, or declare your custom ones before you call DefaultConventions (and even that may not work)

1.1 will look at the namespace and apply a mod to make built in conventions the last resort

sp4ceman wrote Feb 8, 2011 at 1:25 PM

a late late reply just to say that mixing and matching works fine as long as you make sure that the custom conventions are registered before you register the default conventions. when autopoco is looking for conventions it basically finds the first one in the list matching the type its looking for and returns it.

i've added a bunch of methods to my tests that look more or less like

private void registerConventions(IEngineConventionConfiguration c)
        //put in all custom conventions first

        //last thing you do is add the default conventions
and then call it by _factory = AutoPocoContainer.Configure(x => { x.Conventions(c => { registerConventions(c); }); x.Include(typeof(T)); });

robashton wrote Feb 8, 2011 at 1:55 PM

Okay - that's a good workaround - I spose I need to make it explicit somehow too :-)