Skip to content

Fluent Configuration

Wade Baglin edited this page Dec 28, 2015 · 4 revisions

Fluent configuration is new feature of PetaPoco. Its design focus was on ease of use and powerful configuration possibilities. Using the traditional constructors, one would have to instantiate then possibly configure each PetaPoco instance. With the new fluent configuration, this is all handled in an elegant manner.

We believe the easiest way to explain this feature is through examples. The API is very much self-documenting and covers 100% of the configuration options. In fact, the fluent configuration API makes some of the harder parts to configure supper easy. We would suggest any new comer start with this.

Example - Basic: Shortest path to an instance

    var db = DatabaseConfiguration.Build().Create();

Example - Basic: Configure a few options

    var db = DatabaseConfiguration
        .Build()
        .UsingCommandTimeout(180)
        .WithAutoSelect()
        .WithoutNamedParams()
        .Create();

Example - Complex: PostgreSQL conventional table and column naming format

var db = DatabaseConfiguration.Build()
         .UsingConnectionNameg("MyConnection")
         .UsingDefaultMapper<ConventionMapper>(m =>
         {
             // produces order_line
             m.InflectTableName = (inflector, tn) => inflector.Underscore(tn);
             // Or for order_lines m.InflectTableName = (inflector, tn) => inflector.Pluralise(inflector.Underscore(tn));

             // produces order_line_id
             m.InflectColumnName = (inflector, cn) => inflector.Underscore(cn);
         })
         .Create();

Example - Complex: IOC/Container

var container = new SimpleInjector.Container();

container.RegisterSingleton<IDatabaseBuildConfiguration>(DatabaseConfiguration.Build()
    .UsingCommandTimeout(180)
    .WithAutoSelect()
    .WithNamedParams()
    .UsingConnectionStringName("myAppsConnection")
    .UsingDefaultMapper<ConventionMapper>(), Lifestyle.Transient);

container.Register<IDatabase>(() => container.GetInstance<IDatabaseBuildConfiguration>().Create());