MDriven: Transient instance of persistent class - transient

In MDriven I have a persistent class that I need to create a transient instance of.
Is this possible?
I do know that a workaround would be to create as usual and then discard it.
Clarification 2018-02-02 17:04 UTC+1:
I would need to do this without adding anything to the model, compile time.

Yes you are correct - it is not possible to change a class to become transient in runtime.
You can however subclass it... And make the subclass transient... I have had that exact need myself and solved it that way.
Example: The MonthDemandSupplyCorrection is persistent - but the MDSC_Placeholder is not
Update: Since the requirement was clarified to be "not known in design time" - a better answer is to create an ordinary instance and avoid saving it. This can be ensured with the undo-service and memory transactions like this:
EcoSpace.Undo.StartTransaction();
try
{
//Any changes you need
}
finally
{
EcoSpace.Undo.RollbackTransaction();
}

You can try umlrt - work with model, classes, etc at runtime. As an example - https://www.capableobjects.com/2016/07/21/calling-your-own-c-net-things-from-turnkeyserver-side/
Migth help - old free ebook - The ECO III book
Written by Alois Schmid

Related

Doctrine 2 - Referencing a specific associated collection item

In my current model I have 2 entities: Student and StudentOrdersHistory. We're using a history table to record all the orders from the student.
However, we particularly need to work with the latest order history. At first, I used some circular reference like the following:
StudentOrderHistory.student_id --> Student
Student.latest_order --> StudentOrderHistory
The reason for this is that we expect the relation to have hundreds of rows (student here is just an example to make it simple to ask what I need), but since we mostly need to work with the last one, we figured it's pointless loading all of them and then doing some $student->orders->last(), since it'd have to load all the records.
Needless to say, this implementation brought a pain when deleting a Student, as the circular reference won't let me do it without first having to delete the Student.latest_order reference.
Is there anyway I can load into a Student property (like Student::$latestOrder) only the latests one using DQL?
Sure, but you might not be able to do it directly through the Student object. Sacrificing this convenience should get your the performance improvement you want, and in fact doctrine best practices suggest constraining relationships as much as possible.
I haven't tested this code, but you probably know how to get it working. I'd add a method to the OrderRepository, like
public function getLatestForStudent(Student $student)
{
$this->findOneBy(['student' => $student], ['created']);
}

How to list tables in RethinkDb using C++

When I do something in Python or in JavaScript I always have a lot of opportunities, both, to read documentation of a particular library and to try tons of teeny-weeny examples.
Unfortunatelly, in C++ it is not so popular (for what reason?) to provide at least a little ammount of working examples in documentation. Two good examples are C++ clients for MongoDb and RethinkDb.
My question here concerns RethinkDB. In Python I know how to list all table names, not because there is documentation and I'm supposed to dive into the driver code, but just because there is a tiny handy example of doing this:
r.db('test').table_list().run(conn)
And I'm done. In C++ I do not know how to do this - how to list all table names. I do not know even if there is such a method. I wish someone could provide little instructions and share their knowledge.
EDIT
It seems, like I found an appropriate method table_list, but unfortunatelly I do not know how to use it. Besides, it seems that I try to connect to the database in a wrong manner - by this I mean that I connect to the server, but not to a particular database (and again I do not know how to implement this). So, this is what I have now:
std::unique_ptr<R::Connection> conn = R::connect("localhost",28105);
//^^^ I want to connect to a particular database "mydb" - how to do that?
R::Cursor cursor = R::table_list().run(*conn);
for(R::Datum& item : cursor){
do_something(R::write_datum(item).c_str());
// ^^^ is that right???
}
If I do it, like I showed - without specifying the database name, then I get nothing. If, however, I try to connect like this:
R::connect("localhost",28105,"mydb");
then inside for I get an infinite loop. So, I need some help. Thanks!
EDIT
Phew, I found a solution. And I must confess, that it is rather intuitive. I will post it below.
This is the solution:
std::unique_ptr<R::Connection> conn = R::connect("localhost",28105);
R::Cursor cursor = R::db("mydb").table_list().run(*conn);
for(R::Datum& item : cursor){
do_something(R::write_datum(item).c_str());
}
and it works great. I want to thank AtnNn - the sole developper of this great driver.

Can a Custom DataProvider class expose Custom Templates?

I am currently in the process of writing a custom DataProvider. Using the Intergrate External Data documentation.
I've managed to show the external data in the Sitecore back end. However whenever I try to view the data in the items I created, I am getting an error
Null ids are not allowed. <br> Parameter name: displayName
There seems to be precious little on the subject on how to create a custom DataProvider on the Sitecore Developer Network.
The example on their website seems to only show how to import a SINGLE item into a static database. However I am simply trying to merge some items into the hierarchy and I can't find any useful documentation.
It seems that one of your methods that should return an ID doesn't. It might be GetChildIds and/or GetParentId.
Nick Wesselman wrote a good article about it gathering all the information including an example on the Marketplace. I think that is your best start. You can read it here.
Turns out I needed to include at the very least, the Fields->Section->Template in the GetParent method. To be on the safe side I included the Fields/Sections/Templates into my implementations of
GetChildIDs
GetItemDefinition
GetParentID
It wasn't obvious that this was the case, since I had in fact implemented the GetTemplates method correctly, and I had expected that should be enough.

Repository Pattern

I've got a quick question regarding the use of repositories. But the best way to ask is to show a bit of pseudocode and you guys tell me what the result should be
Get a record from the repository with ID of 1 (assume it exists)
Edit a couple of properties
Query the repository again for an item with ID of 1
Result = ??
Should I get the object with updated values or the object without (original state), bearing in mind that since updating the values of properties (step 2) I have not told the repository to update this record.
I think I should get a copy of the original item and not a reference to the edited version.
Please tell me what is correct.
Cheers
The repository pattern is suppose to act like a collection of your objects, so ideally I think it should return the same object instance which would have the updates in it.
Generally there is an identity map somewhere so your repositories can keep track of what has already been loaded. With an identity map, when you fetch an object with the same Id you should always get the already loaded object back regardless of how many times. This is how all more sophisticated ORMs work and is generally a good practice. An identity map helps keep things in sync while you are in the same transaction and saves you some data access.
NHibernate's session has an identity map it keeps track of so you don't have to worry about trying to implement your own in your repositories. Also I believe you can use NHibernate's stateless session if you want to load another instance without change tracking, but I'm not positive on that.
Judging from your past questions I'm assuming you are using LINQ/C#?
If you are using a DataContext and you haven't called SubmitChanges() then you should get back the original unchanged object.
Just tested it. I was wrong, you get back the changed object.
If you set ObjectTrackingEnabled = false on the DataContext you will get the unchanged object.

Repository Pattern - POCOs or IQueryable?

I'm new to the Repository Pattern and after doing a lot of reading on the web I have a rough understanding of what is going on, but there seems to be a conflict of ideas.
One is what the IRepository should return.
I would like to deal in ONLY Pocos so I would have an IRepository implementation for every aggregate root, like so:
public class OrangeRepository: IOrangeRepository
{
public Orange GetOrange(IOrangeCriteria criteria);
}
where IOrangeCriteria takes a number of arguments specific to finding an Orange.
The other thing I have is a number of data back-ends - this is why I got into this pattern in the first place. I imagine I will have an implementation for each, e.g
OrangeRepositoryOracle, OrangeRepositorySQL, OrangeRepositoryMock etc
I would like to keep it open so that I could use EF or NHibernate - again if my IOrangeRepository deals in POCOs then I would encapsulate this within the Repository itself, by implementing a OrangeRepositoryNHibernate etc.
Am I on the right lines?
Thanks
EDIT: Thanks for the feedback, I don't have anyone else to bounce these ideas off at the moment so it is appreciated!
Yes, your version is the safest / most compatible one. You can still use it with about any resources, not only data access ones, but with web services, files, whatever.
Note that with the IQueryable version you still get to work based on your POCOs classes, but you are tied to the IQueryable. Also consider that you could be having code that uses the IQueryable and then turns out it you hit a case where one of the repository's ORM doesn't handle it well.
I use the same pattern as you do. I like it a lot. You can get your data from any resources.
But the advantage of using IQuerable is that you do not have to code your own criteria API like the OrangeCriteria.
When NHibernate gets full Linq support then I may switch to the IQueryable.
Then you get
public class OrangeRepository: IOrangeRepository {
public IQueryable<Orange> GetOranges();
}