This is what I am doing,
public class Order
{
// defined all properties here
public Order() {}
// I want this constructor to run each time I do o.Add();
public Order(Order o) {
this.ID = o.ID;
this.Title = o.Title;
this.Status = CalculateStatus(o);
this._StartDate = GetDate(o.StartDate);
this._EndDate = GetDate(o.EndDate);
}
public Order Add()
{
// other business logic
this.ID = new OrderDataAccess().AddOrder(this.Title, this.Status, this._StartDate, this._EndDate);
return this;
}
}
public Order[] AddOrders(Order[] orderCollections)
{
foreach(Order o in orderCollections)
{
o = new Order(o).Add();
}
// return object with update order's ID
return orderCollections;
}
Where I am Stuck
foreach(Order o in orderCollections)
{
o = new Order(o).Add();
}
Error
Cannot assign to "o" because it is a 'foreach iteration variable'
Please don't tell me what error means as I know what I am trying to do is wrong, I can't pass iterate and update object "o" at same time, but I am stuck with how to fix this issue following best practices.
Edit
Because AddOrders is a WCF method, I am not sure when it receives paramters with collections of objections, it runs my constructor Order (Order o) or not, let say WCF is calling this method from JS rest call, how WCF is going to know if I want it to intialize my object with constructor Order (Order o)
I want this constructor Order(Order o)
to run each time I do o.Add();
Edit 2
Sequence of execution I always want..
Order(Order o)
because, I also have update, delete functions which I didn't added to keep this simple, my seconds constructors updates properties as needed using my dataAccess layer, I can't trust WCF clients providing me data I exactly need. I am pretty new to OOP, I don't understand the concept how calling WCF method AddOrders will ever execute my seconds constructor without me creating an instance of object again, and if I create a new instance to call 2nd constructor, I am not sure how to update references of objects WCF is initializing in it's array.
Just call the Add
-method without any assignment:
foreach(Order o in orderCollections)
{
o.Add();
}
There is neither any reason to return this
from your Add
method, nor is there any reason to update the object being enumerated!
Your Add
method mutates the object itself, any reference you have to that object will be updated.
public Order[] AddOrders(Order[] orderCollections)
{
foreach(Order o in orderCollections)
{
o.Add();
}
// orderCollections maintains REFERENCES to orders, so they all have their ID updated
return orderCollections;
}
As AddOrders
is a WCF call, when it serializes the result the ID
property of each order will have been filled by the call to Add
, and the caller will receive it.
I'm still slightly concerned that the object you're communicating to the caller knows about Data Access, but that's a different story.
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.