I have an object that looks like this:
public class MyObject
{
public int UserID {get;set;}
public List<SomeObjects> {get;set;}
public List<SomeOtherObjects {get;set}
public MyObject TheObjectForDB {get;set} //HERE: creating a new object
public MyObject(int TheUserID)
{
this.UserID = TheUserID;
}
public LoadTheObjectFromDatabase()
{
MyQueries TheQueries = new MyQueries();
TheObjectForDB = TheQueries.LoadFromDB(UserID) //HERE: object for DB work
this.List<SomeObjects> = TheObjectForDB.List<SomeObjects>;
this.List<SomeOtherObjects> = TheObjectForDB.List<SomeOtherObjects>;
}
}
The database query essentially returns the same object type:
LoadFromDB = from...
select new MyObject(TheUserID){...}
How can I remove this redundancy where I need to instantiate another object of the same type to populate the initial object?
Thanks for your suggestions.
Make
LoadTheObjectFromDatabase()astaticmethod which returns a newMyObject.Then, instead of doing
MyObject newObj = new MyObject(); newObj.LoadTheObjectFromDatabase();, you’d just doMyObject newObj = MyObject.LoadTheObjectFromDatabase();