This project is read-only.

Custom select to create one object

Nov 29, 2010 at 10:40 AM

Been looking at the custom select methods. These seem pretty useful, but I wanted to suggest another option for them that could maybe make them easier to use.

Currently, I have added a custom select method to select a single object. However, the generated method still returns a List object, which I then need to access to get a single object out of it.

I wondered if the custom select methods could have an extra parameter to indicate whether the method would return a single object or a list; that way, you could determine whether the method would return a custom object or a custom object List.

Also, I wondered why the SelectUsingPK method returns a collection rather than a single object - it seems odd to have to receive a collection to then have to access the collection to get the actual object that you wanted on the first method call.

Let me know if you think that is a possibility, or whether I've just misunderstood how things work...



Nov 30, 2010 at 1:50 AM

First when calling a select command you can use LINQ by issuing the FirstOrDefault() method to get the first object. You can use this to get the first object if one exists or null. Even if the method returned a single object you would have to check it for null anyway so there is no code saved. Simply use LINQ to get what you want with one line of code.

Also as to the SelectUsingPK method, the collection objects returned a collection and the business object returned a single object (same method). This was done because it was though the collection object method should return a collection. This is not really a good point and as such will be changed. In the next version the SelectUsingPK will always return a single object.

Nov 30, 2010 at 8:31 AM

Hadn't thought of the LINQ solution - thanks.

However, I'm struggling to fit this in to the context of an existing subdomain, and I don't know if I'm missing the point - hopefully you can clarify.

I presume the idea of subdomains is so with a single Persist() you can save all related changes within a single transaction so it all either works or fails. However, I've found it works in a different way to what I imagined. 

Say I have loaded a collection of User objects already. As a result of my processing, I now need to find and edit another specific user using a custom select method. If I create a separate subdomain, I can do this and use your FirstOrDefault suggestion, but then the Persist() for the initial subdomain won't save changes to this. If I use the existing subdomain, the User object returned (if one is actually returned) is now a part of the initial Users collection, in which case I can't use FirstOrDefault, and I don't know whether the custom select command returned anything.

Have I misunderstood? If I already have a Users collection and want to call a custom select command to find either another single User, or another selection of Users, do I have to create a second subdomain and have two Persist() commands (thereby losing a single transaction), or is there a way to keep it all in a single transaction but still identify separately the User(s) loaded by the second select command?

Thanks for your help - hope that makes sense...