schotime / NPoco

Simple microORM that maps the results of a query onto a POCO object. Project based on Schotime's branch of PetaPoco
Apache License 2.0
848 stars 302 forks source link

Help to the use Fluent in NPoco #618

Closed mchomem closed 3 years ago

mchomem commented 3 years ago

Hello @schotime

I need a help to correctly use Fluent from NPoco.

I'm not able to use Fluent from NPoco.

Searching by examples and issues too, i don't found something which helps me.

If you have a full tutorial (with source code), will be of big help to my knowledge.

Next, my attempt to use NPoco's fluent mapping:

  1. Initial call made in the Program class via Windows Console application:
    
    using MCHomem.NPoco.Proto.ConsoleApp.Views;
    using MCHomem.NPoco.Proto.Models.Repositories;
    using System;

namespace MCHomem.NPoco.Proto.ConsoleApp { class Program { static void Main(string[] args) { Console.Title = "NPoco CRUD";

        // Obviously I didn't do it right here, because if you remove that line the mapping is done in the same way.
        new TestAppContext().Setup();

        new MenuCon().ShowMenu();
    }
}

}


2. The context connection class

using MCHomem.NPoco.Proto.Models.Mappings; using NPoco; using NPoco.FluentMappings; using NPoco.SqlServer;

namespace MCHomem.NPoco.Proto.Models.Repositories { public class TestAppContext { public DatabaseFactory DbFactory { get; set; }

    public Database Get()
    {
        return new SqlServerDatabase(AppSettings.Get("DataConnection"));
    }

    public void Setup()
    {
        FluentConfig fluentConfig = FluentMappingConfiguration.Configure(new EmployeeMapping());

        DbFactory = DatabaseFactory.Config(x =>
        {
            x.UsingDatabase(() => new SqlServerDatabase(AppSettings.Get("DataConnection")));
            x.WithFluentConfig(fluentConfig);
            x.WithMapper(new Mapper());
        });
    }
}

}


3. My POCO class

using System;

namespace MCHomem.NPoco.Proto.Models.Entities { public class Employee { public Int32? EmployeeID { get; set; } public String Name { get; set; } public String DocumentNumber { get; set; } public Boolean? Active { get; set; } public DateTime CreatedIn { get; set; } public DateTime? UpdatedIn { get; set; } } }


4. My Fluent Mapping class

using MCHomem.NPoco.Proto.Models.Entities; using NPoco.FluentMappings;

namespace MCHomem.NPoco.Proto.Models.Mappings { public class EmployeeMapping : Map { public EmployeeMapping() {
this.TableName("Employee"); this.PrimaryKey("EmployeeID"); this.Columns(x => { x.Column(c => c.EmployeeID); x.Column(c => c.Name); x.Column(c => c.DocumentNumber); x.Column(c => c.Active); x.Column(c => c.CreatedIn); x.Column(c => c.UpdatedIn); }); } } }


5. CRUD

using MCHomem.NPoco.Proto.Models.Entities; using NPoco; using System.Collections.Generic; using System.Text;

namespace MCHomem.NPoco.Proto.Models.Repositories { public class EmployeeRepository : ICrud { public void Create(Employee entity) { using (IDatabase db = new TestAppContext().Get()) { db.Insert(entity); } }

    public void Delete(Employee entity)
    {
        using (IDatabase db = new TestAppContext().Get())
        {
            db.Delete(entity);
        }
    }

    public Employee Details(Employee entity)
    {
        using (IDatabase db = new TestAppContext().Get())
        {
            return db.SingleOrDefaultById<Employee>(entity.EmployeeID);
        }
    }

    public List<Employee> Retreave(Employee entity)
    {
        using (IDatabase db = new TestAppContext().Get())
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select");
            sql.Append(" *");
            sql.Append(" from");
            sql.Append(" Employee");
            sql.Append(" where");
            sql.Append(" (@0 is null or EmployeeID = @0)");
            sql.Append(" and (@1 is null or [Name] like '%' + @1 + '%')");
            sql.Append(" and (@2 is null or DocumentNumber = @2)");
            sql.Append(" and (@3 is null or Active = @3)");

            return db.Fetch<Employee>(sql.ToString(), entity?.EmployeeID, entity?.Name, entity?.DocumentNumber, entity?.Active);
        }
    }

    public void Update(Employee entity)
    {
        using (IDatabase db = new TestAppContext().Get())
        {
            db.Update(entity);
        }
    }
}

}



See my project, where i try to use NPoco Fluent mapping : [My test project](https://github.com/mchomem/MCHomem.NPoco.Proto/tree/master)
schotime commented 3 years ago

You setup up the dbfactory correctly but you never call it to get the database. In your Get method of your context change to the following.

return DbFactory.GetDatabase();

Your context class should also be a singleton and be injected rather than newing it up each time.

mchomem commented 3 years ago

Hello @schotime .

Very thanks for your help.

I did the updates in my project with your instructions. Now, is using fluent mapping from NPoco.

Strong hug