npoco


How do I format a query to fetch an aggregate root with just related ids


Let's say I have the following object in my domain.
[TableName("work_space")]
public class WorkSpace
{
public long Id { get; set; }
[Column(Name="owner_id")]
public long OwnerId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public IEnumerable<int> OrgIds { get; set; }
public IEnumerable<int> SettingIds { get; set; }
public IEnumerable<int> UserIds { get; set; }
public IEnumerable<long> WorkViewIds { get; set; }
}
This is one way to fetch the data that I need.
SELECT ws.*, wsu.user_id as UserId, wss.setting_id as SettingId, wso.org_id as OrgId, wv.id as WorkViewId
FROM work_space ws
LEFT OUTER JOIN work_space_user wsu ON ws.id = wsu.work_space_id
LEFT OUTER JOIN work_space_setting wss ON ws.id = wss.work_space_id
LEFT OUTER JOIN work_space_org wso ON ws.id = wso.work_space_id
LEFT OUTER JOIN work_view wv ON ws.id = wv.work_space_id
WHERE ws.id = #0
How is this usually done in NPoco? Would I use a multi Result set fetch? Some sort of Fetch one to Many? Do I flag my ID collections as Result or Ignore Columns? I'm just not finding any examples like this in the docs.
This is the most intuitive way to do this I have found to do this so far. It works ok for a single entity, but when I want to fetch collections of them, I'm having n+1 problems as go back to the database for each object in the collection to fetch the relations.
public WorkSpace GetWorkSpace(int id)
{
using (Database db = DbFactory.VSurveyDbFactory.GetDatabase())
{
WorkSpace workspace = db.SingleById<WorkSpace>(id);
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.Append(#"SELECT * FROM work_space_user WHERE work_space_id = #0;");
sqlBuilder.Append(#"SELECT * FROM work_space_setting WHERE work_space_id = #0;");
sqlBuilder.Append(#"SELECT * FROM work_space_org WHERE work_space_id = #0;");
sqlBuilder.Append(#"SELECT * FROM work_view WHERE work_space_id = #0;");
Tuple<List<int>, List<int>, List<int>, List<long>> results = db.FetchMultiple<int, int, int, long>(sqlBuilder.ToString(), id);
workspace.UserIds = results.Item1;
workspace.SettingIds = results.Item2;
workspace.OrgIds = results.Item3;
workspace.WorkViewIds = results.Item4;
return workspace;
}
}

Related Links

NPoco mapping primarykey error
Nopoc Execute() method returns -1
Creating a new database using NPoco
Creating Custom Ignore attribute only for Update methods?
NPoco One-To-Many and Many-To-Many
Any example of using join in query applyable for npoco
How do I format a query to fetch an aggregate root with just related ids

Categories

HOME
c#
service
fortran
window.open
opencv4android
google-docs-api
cdn
markdown
pip
google-my-business-api
apache-httpclient-4.x
ndepend
drush
watson-dialog
browsermob
comma
lambda-calculus
opendaylight
openstack-horizon
internet-explorer-7
caldav
turn.js
monad-transformers
xna
lotusscript
beaker-notebook
ng-tags-input
email-client
gsuite
chartist.js
inspec
r-factor
mobile-development
unity3d-5
rtos
flickr
repeat
realloc
framebuffer
nscollectionview
mybatis-generator
ui-grid
sknode
delphi-xe
web-technologies
cakephp-3.2
text-formatting
confluent
riemann
wink
playframework-2.3
node-java
apache-commons-cli
msiexec
esprima
blitz3d
portaudio
turbolinks
dimensionality-reduction
eclipse-mars
particle-system
sprockets
php-java-bridge
jmap
bbedit
nugetgallery
periodic-processing
firefox-os
jsondoc
dday
opendolphin
gtkwave
imageicon
libgcrypt
android-togglebutton
textblob
consul-template
opennebula
tidesdk
perfect-scrollbar
optimistic-locking
xcode5
jsonmodel
cs193p
enquire.js
image-preloader
sqldatasource
imaplib
adabas
jfilechooser
kohana-orm
azure-pack
dynamic-expresso
azure-scheduler
latex-suite
select2-rails
string-length
urlfetch
quit
dancer
scriptmanager
capitalization
flexbuilder
auto-compile
functional-specifications
noise-reduction
netbeans7.0
mysql-logic
calendarextender
wtsapi32
bootstrap-protocol

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App