locked
How to implement EntityState for my Dto Class? EF5 RRS feed

  • Question

  • Hi,

    Because of I am working wtin Bussiness Layer and I have a number of Dto Class.

    I would like to send only changed data from collection class to DAL(wrap my DbContext)

    public class ProductDto

    {

    public int ID {get; set;}

    public string ProductName {get; set;}

    }

    Then wrap inside

    public ObsevarbleCollection<ProductDto> ListingDataView {get; set;}

    How to send data from the "ListingDataView" which has changed to DAL?

    Or I should add a filed for Dto is "IsModified", Right?

    Friday, October 12, 2012 6:01 PM

Answers

  • Hi Weera,

    Welcome to the MSDN forum.

    Since this class is a DTO which is not tracked by DbContext, you can use additional field to make a mark if you send only changed data from collection. If the data only can be changed once, using a field to control it is a good way. Also, please remember to reset the IsChanged to false after assigning values first time.

        public class ProductDto
        {
            private Int32 Id { get; set; }
            private String productName { get; set; }
            public Int32 ID
            {
                get 
                {
                    return this.Id;
                }
                set
                {
                    if (this.Id != value)
                    {
                        this.Id = value;
                        this.IsChanged = true;
                    }
                }
            }
            public String ProductName
            {
                get 
                {
                    return this.productName;
                }
                set
                {
                    if (this.productName != value)
                    {
                        this.productName = value;
                        this.IsChanged = true;
                    }
                }
            }
            public Boolean IsChanged { get; set; }
        }

    If it can be changed many times, you need a field to store the original values so as to compare the original values in DbContext with the current values. It depends on your requirements.

        public class ProductDto
        {
            private Int32 _ID { get; set; }//Original value
            private String _ProductName { get; set; }//Original value
            private Boolean IsChanged { get; set; }
            public Int32 ID { get; set; }//Current value
            public String ProductName { get; set; }//Current value
            public void DetectChanges()//Call to detect changes
            {
                if (this._ID != this.ID)
                {
                    this.IsChanged = true;
                }
                if (this._ProductName != this.ProductName)
                {
                    this.IsChanged = true;
                }
            }
        }

    Best Regards,


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us


    Monday, October 15, 2012 8:48 AM

All replies

  • Hi Weera,

    Welcome to the MSDN forum.

    Since this class is a DTO which is not tracked by DbContext, you can use additional field to make a mark if you send only changed data from collection. If the data only can be changed once, using a field to control it is a good way. Also, please remember to reset the IsChanged to false after assigning values first time.

        public class ProductDto
        {
            private Int32 Id { get; set; }
            private String productName { get; set; }
            public Int32 ID
            {
                get 
                {
                    return this.Id;
                }
                set
                {
                    if (this.Id != value)
                    {
                        this.Id = value;
                        this.IsChanged = true;
                    }
                }
            }
            public String ProductName
            {
                get 
                {
                    return this.productName;
                }
                set
                {
                    if (this.productName != value)
                    {
                        this.productName = value;
                        this.IsChanged = true;
                    }
                }
            }
            public Boolean IsChanged { get; set; }
        }

    If it can be changed many times, you need a field to store the original values so as to compare the original values in DbContext with the current values. It depends on your requirements.

        public class ProductDto
        {
            private Int32 _ID { get; set; }//Original value
            private String _ProductName { get; set; }//Original value
            private Boolean IsChanged { get; set; }
            public Int32 ID { get; set; }//Current value
            public String ProductName { get; set; }//Current value
            public void DetectChanges()//Call to detect changes
            {
                if (this._ID != this.ID)
                {
                    this.IsChanged = true;
                }
                if (this._ProductName != this.ProductName)
                {
                    this.IsChanged = true;
                }
            }
        }

    Best Regards,


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us


    Monday, October 15, 2012 8:48 AM
  • Hi,

    I have done it, work great, However, thanks.

    I love your code, Do you think this is the right way to do that?

    Actually, I implemented similar your code but more complicated.

    Wednesday, October 17, 2012 6:28 AM
  • Hi Weera,

    In my humble opinion, I think you can send all the DTO data to DAL and write a method in DAL to compare the data in DTO with the data in Context, which can make DTO class more concise and the collection of DTO will be iterated only once (Now you iterate the collection twice: 1. Iterate all members to compare 2. Update values to corresponding records).

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, October 18, 2012 8:44 AM