映射POCO在ADO.NET上实现存储库模式

时间:2017-08-08 16:56:00

标签: c# ado.net repository-pattern

我试图在ADO.NET和C#上实现存储库模式。

我已经编写了我的存储库基类,当我实现Clients Repository时,我的代码如下所示:

class Clientes_rep : RepositoryBase<Clientes_mdl>
{
    AdoNetContext _context;

    public Clientes_rep(AdoNetContext context) : base(context)
    {
        _context = context;
    }

    public override void Save(Clientes_mdl entity)
    {
        using (var command = _context.CreateCommand())
        {                
            if (entity.ID_Cliente== 0)
            {
                //New Client
                command.CommandText = @"INSERT INTO Clientes (RazonSocial, RazonFantasia) 
                    VALUES(@RazonSocial, @RazonFantasia)";                    
            }
            else
            {
                //Actualizacion
                command.CommandText = @"UPDATE Clientes SET RazonSocial=@RazonSocial, RazonFantasia=@RazonFantasia) 
                    WHERE ID_Cliente=" + entity.ID_Cliente;                        
            }

            // Parámetros
            command.AddParameter("RazonSocial", entity.RazonSocial);
            command.AddParameter("RazonFantasia", entity.RazonFantasia);
            command.ExecuteNonQuery();
        }            
    }

    protected override void Map(IDataRecord record, Clientes_mdl entity)
    {
        entity.RazonSocial = (string)record["RazonSocial"];
        entity.RazonFantasia = (string)record["RazonFantasia"];            
    }
}

现在,Clients表有30个字段,因此我正在寻找一种更有效的映射到POCO类的方法。有什么想法吗?

0 个答案:

没有答案