What is the real concept and utility of POCO classes?

9

I am studying and developing a new project and a little studying about Windows Phone 7.1, I saw that they suggest / indicate the use of POCO classes, for database mapping (It seems to me that Windows Phone 7.1 uses some compact version of EF (Entity Framework), I did not study the Windows Phone case very much, because this is not the time to study this in depth, I just studied the basics to understand their concepts .

But what I would like to understand better is:

  • Why using POCO classes?
  • What is the advantage that this can bring?
  • Why is it used in a Project?
  • And what is actually POCO classes? (Conceptually speaking)
  • asked by anonymous 17.09.2014 / 20:33

    2 answers

    9
      
  • Why using POCO classes?
  •   

    A POCO object has no dependency on an external framework .

    Exemplo: In my business layer, I create POCO objects so that this layer does not have dependence on technologies and external frameworks. Thus, I can change technologies and / or frameworks without messing with my business layer (which is the "heart" of the software).

      
  • What is the advantage that this can bring?
  •   
    • Minimizes dependency between layers.
    • Minimize maintenance, if I switch technologies and / or frameworks only the infrastructure layers are affected.
    • Increases your ability to test.
      
  • Why is it used in a Project?
  •   

    I believe that given the benefits mentioned in the other answers, it is interesting to use them.

      
  • And what is actually POCO classes? (Conceptually speaking)
  •   

    "Plain Old CLR Object"

    A class with no attributes that describe the infrastructure concerns, external frameworks, or other responsibilities that your domain objects should not have.

    Examples:

  • We are tied to the Entity Framework if we let it create our entity classes like this:
  • [EdmEntityTypeAttribute(NamespaceName="SistemaOS.Model", Name="Pessoas")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class Pessoas : EntityObject
    {
        [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Int32 id_pessoa
        {
            get
            {
                return _id_pessoa;
            }
            set
            {
                if (_id_pessoa != value)
                {
                    Onid_pessoaChanging(value);
                    ReportPropertyChanging("id_pessoa");
                    _id_pessoa = StructuralObject.SetValidValue(value,"id_pessoa");
                    ReportPropertyChanged("id_pessoa");
                    Onid_pessoaChanged();
                }
            }
        }
    }
    
  • A simple class example POCO :
  • //Ao trocar meu framework de persistência eu não precisarei mexer nessa classe de negócio
    public class Pessoa
    {
        public string Nome { get; set; }
        public string Sobrenome { get; set; }
        public int Cpf { get; set; }
    
        public string NomeCompleto()
        {
            return Nome + " " + Sobrenome;
        }
    }
    
        
    17.09.2014 / 20:51
    3

    What is POCO?

    POCO (Plain Old CLR Object) as well as Plain Old Java Object (POJO) simply means an object that has no internal or external dependencies.

    The term is used to identify an object as a simple object, over and above the complex and specialized objects that frameworks like ORMs generally use.

    Why use POCO? What is the advantage that this can bring?

    • Enables a simple data storage mechanism, and simplifies serialization / passing of data between layers.

    • Very helpful with Dependency Injection and project pattern Repository

    • Decrease Complexity and Dependency between Other Layers (Layers that use refer to the POCO and the POCO does not reference anything) which helps to decrease the coupling

    • Improves "testability" through simplification

    Reference: Wikipedia

        
    17.09.2014 / 20:48