Click here to Skip to main content
15,124,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I create application in c# that connected to SQl server with entity framework
the application is distributed
firs layer is data_access layer that contain the Entity framework model
next layer is contain logic of the application that contains all operation functions insert select update
last layer contains the forms presentation layer
i want to add class copy an entity from data_access And call it in presentation
every entity in framework is connected with other entities
the class is like this

C#
namespace DataAccess
{
    using System;
    using System.Collections.Generic;

    public partial class tbl_Customers
    {
        public tbl_Customers()
        {
            this.tbl_Orders = new HashSet<tbl_Orders>();
        }

        public int cust_ID { get; set; }
        public string cust_Name { get; set; }
        public string cust_Phone { get; set; }
        public string cust_Address { get; set; }

        public virtual ICollection<tbl_Orders> tbl_Orders { get; set; }
    }
}



i want t create class return same values
Posted

1 solution

The notion like "copy a class" makes no sense. You can copy the instance of the class, object, not a class itself. What you need is also called cloning.

The simplest way to clone and object is to call object.MemberwiseClone on your instance. Note that is will create a shallow copy of the object. This is not a problem for your string members, because strings, being reference objects, mimic value semantics and use intern pool, but your collection, being shallow copies, will create a dependency between source object and its copy: when, for example, you add elements in one collection, it will add them in the collection of a copy, because they share the same collection object, keeping two different references to the same object.

To create independent collection members, you would need to clone it: create a brand new one and copy elements independently. Would it be a full deep copy of original object? It depend on the element type of the collection. If tbl_Orders is a reference type, the elements will be shared. If you want to have deep clones in all cases, you would need to solve a more difficult problem: create an universal mechanism of deep cloning. For example, you could create an interface for deep cloning, but then you would need to require all classes participating in your object graph to support it; besides, you would need to implement this interface in all participating classes. In other words, it would be a considerable work, something to think at and design thoroughly.

Please see:
http://en.wikipedia.org/wiki/Shallow_copy#Shallow_copy[^],
http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx[^],
http://social.msdn.microsoft.com/Forums/vstudio/en-US/1dc7cc60-fc62-43d1-b9bd-2666ceef043d/deep-copy-vs-shallow-copy[^].

—SA
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900