terça-feira, 28 de julho de 2015

Microsoft Enterprise Library

Continue with Microsoft Enterprise Library?

Microsoft Enterprise Library 6


Enterprise Library is made up of application blocks, each aimed at managing specific crosscutting concerns. Crosscutting concerns are those tasks that you need to accomplish in several places in your application. When trying to manage crosscutting concerns there is often the risk that you/different team members will implement slightly different solutions for each task at each location in your application, or that you will just forget them altogether. Writing entries to a system log file or Windows Azure table storage, dealing with transient error conditions and validating user input are typical crosscutting concerns. While there are several approaches to managing them, the Enterprise Library application blocks make it a whole lot easier by providing generic and configurable functionality that you can centralize and manage.

You can see above a little code used with this library.



using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using System.Data.Common;
using System.Data;

namespace JustforNow.DAL
{
    public class ValorDAL
    {
        public List<Valor> Get(Valor pValorFilter)
        {
            List<Valor> valores = new List<Valor>();

            StringBuilder sql = new StringBuilder();
            sql.Append("SELECT LPK_ID, LPK_FK_PRDCAMP_ID, LPK_DATA_CRIACAO, LPK_DATA_ALTERACAO, LPK_VALOR FROM VALOR ");
            sql.Append("WHERE (ISNULL(@LPK_ID, '') = '' OR LPK_ID = @LPK_ID)");

            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();

            using (DbCommand dbCommand = db.GetSqlStringCommand(sql.ToString()))
            {

                db.AddInParameter(dbCommand, "@LPK_ID", DbType.Int32, pValorFilter.Id.HasValue ? pValorFilter.Id : (object)DBNull.Value);

                using (IDataReader reader = db.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        Valor valor = new Valor();

                        if (reader["LPK_ID"] != DBNull.Value && !string.IsNullOrEmpty(reader["LPK_ID"].ToString()))
                        {
                            valor.Id = Convert.ToInt32(reader["LPK_ID"]);
                        }

                        if (reader["LPK_VALOR"] != DBNull.Value && !string.IsNullOrEmpty(reader["LPK_VALOR"].ToString()))
                        {
                            valor.valor = reader["LPK_VALOR"].ToString();
                        }

                        if (reader["LPK_FK_PRDCAMP_ID"] != DBNull.Value && !string.IsNullOrEmpty(reader["LPK_FK_PRDCAMP_ID"].ToString()))
                        {
                            valor.ProdutoCampoId = Convert.ToInt32(reader["LPK_FK_PRDCAMP_ID"]);
                        }

                        if (reader["LPK_DATA_CRIACAO"] != DBNull.Value && !string.IsNullOrEmpty(reader["LPK_DATA_CRIACAO"].ToString()))
                        {
                            valor.DataCriacao = Convert.ToDateTime(reader["LPK_DATA_CRIACAO"]);
                        }

                        if (reader["LPK_DATA_ALTERACAO"] != DBNull.Value && !string.IsNullOrEmpty(reader["LPK_DATA_ALTERACAO"].ToString()))
                        {
                            valor.DataAlteracao = Convert.ToDateTime(reader["LPK_DATA_ALTERACAO"]);
                        }

                        valores.Add(valor);
                    }
                }
            }

            return valores;
        }
    }
}


Postar um comentário