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
gulp
azure-active-directory
typo3
sql-server-2014
display
windows-server-2008
apache-storm
blob
azure-database-mysql
thermal-printer
sdk
browser-cache
kendo-asp.net-mvc
collision-detection
meson-build
install
handlebars.js
box-api
mamp
ldap-query
point-of-sale
osrm
openstack-horizon
dat.gui
facebook-marketing-api
template10
amazon-data-pipeline
ibatis
printf
aiml
aws-devicefarm
descriptor
fibonacci
oracle-golden-gate
functional-dependencies
scrapy-spider
datastax-enterprise
aws-certificate-manager
sasl
windows-mobile
jms-topic
paper-trail-gem
drawrect
intermec
visual-studio-debugging
outlook-2010
oracle-bpm-suite
stencyl
var
instances
cowboy
ng-lightning
rexx
python-venv
xmldom
react.rb
django-tables2
node-java
fckeditor
aot
multi-select
node.js-client
robotc
turbolinks
racket-student-languages
mac-app-store
x12
boost-range
mate-desktop
netlink
vitamio
trendline
nslocalizedstring
instabug
textblob
hexagonal-tiles
litedb
android-networking
optimistic-locking
static-files
grunt-contrib-connect
qf-test
powerpoint-2010
pyalgotrade
rda
slimscroll
infor-eam
acrofields
xcode6.1-gm-seed
lastinsertid
scidb
ironworker
source-depot
mosix
setcookie
invalidoperationexception
inflate
beep
winrt-async
flatten
mysql-connector
commonsware
nsundomanager
visual-leak-detector
automapper-2
zend-validate
virtual-printer
qvariant
fxcopcmd
browser-state
miniport
konsole
focus-stealing
user-preferences
jettison

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