简体   繁体   中英

Is this a DTO or a POCO or a combination of the two C#

So below I have a class I created as an example of what I have been seeing and actually even started using this pattern myself as it tends to be quite useful for me. The problem is I have been calling it POCO but yet I believe a POCO really shouldn't have actual Data access included. I believe that is more for DTOs. So the question here is this DTO, POCO, a combination of the two or neither?

namespace MAINAPP.DAL
{

    public class SomeModel
    {
        public int SomeModelID { get; set; }
        public String Name { get; set; }

        public static bool DeleteSomeModel(int SomeModelID)
        {
            bool returnstatus = true;
            int statusint = 0;
            try
            {
                using (SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["SomeDBString"].ConnectionString))
                {
                    sc.Open();
                    StringBuilder query = new StringBuilder();
                    query.Append("DELETE  FROM SomeModels WHERE SomeModelID = @SomeModelID");

                    using (SqlCommand comm = new SqlCommand(query.ToString(), sc))
                    {
                        comm.Parameters.Add(new SqlParameter("@SomeModelID", SomeModelID));
                        statusint = comm.ExecuteNonQuery();


                        comm.Dispose();
                    }
                    sc.Close();
                    sc.Dispose();
                }

            }
            catch (SqlException se)
            {
                var exceptionresult = se.Message;

            }
            if (statusint > 0)
            {
                returnstatus = true;
            }
            else
            {

                returnstatus = false;
            }
            return returnstatus;
        }

    }
}

That is not a POCO nor is it a DTO.

A POCO is a Plain old CLR object, meaning that it has no dependencies and no base class derivations. The object you presented has a dependency on System.Data.

A DTO is traditionally used as a data contract only. DTO means that it really shouldn't have behavior because that behavior is not serializable. If someone were to consume the object from a WSDL definition and didn't have your class representation, they could not reproduce the DeleteSomeModel method.

If you want POCO classes, I'd suggest using the Repository pattern for your data access or use something like Code First in Entity Framework.

Here is an example using the repository pattern

public class SomeModel
{
    public int SomeModelID { get; set; }
    public String Name { get; set; }
}

public class SomeModelRepository
{
    public void Delete(int id)
    {
       // delete logic here
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM