Subsonic 3 : get only certain columns - serialization

I use : Subsonic 3, SQL Server 2008, Json.Net
Here is my problem :
I have a house table with id,lot number, address, location, ownerid and an owner table with id,name, description.
I'd like to get only the owner name and address of house where location is "San Francisco".
How can I do this with Subsonic?
My problem is I can only get a typed list or a datareader, I don't understand how to only get an array or a simple list.
What is the best solution? Create a custom class ? Create a View ? Anything else ?
My aim is to then serialize the data to send it back to my application, but serialization is seriously long because of the numerous relationships (My example here is simplified, there are indeed : house - owner - city - country etc.), whereas I need only 2 fields...
Thank you

Take a look at Some Issues about Rob Conery's repository pattern ?, rob provides the answer to exactly what you're asking for.
If the information is only going to be used in one place, maybe use a struct with two fields?

Related

REST solutions when not strictly referring to an actual resource

Take a simple table person:
CREATE TABLE person
(
id bigint,
name nvarchar(128),
age int
)
You can represent this in a REST interface:
GET /person
GET /person/5
PUT /person
POST /person/5
PATCH /person/5
DELETE /person/5
This interface would expect 2 parameters:
{
name: 'Joe',
age: 16,
}
You could then define an API that would expect those two parameters, and even make the age optional.
However, suppose you wanted to define a model on the client side that wanted to do fancy things with this person table, such as pulling all teenagers, how would you best represent this?
I suppose I could do something this, only support GET, and then arbitrarily require different parameters which matched the query needs:
GET /person/teenager
However, I don't know that this would properly meet all use cases. For example, I believe REST urls should only have nouns, and I am not sure how to put something like this into noun form:
GET /person/by-age
Any ideas / references / suggestions?
The most common way to constrain your person listing results is to use query parameters. You can define your query parameters in any way that is useful for your API. One example might be GET /person?age=13..20 to get only teenagers. Another example might be GET /person/?filter=age>=13,age<20.

DQS Composite Domain -- Raw Data Name Field Has Both People And Company Names

I'm trying to build a knowledge base in SQL Server 2012 DQS. In the raw data that I am trying to run through DQS, I've got a field that is a full name field that unfortunately can hold both human and company names. I was wondering if anyone had advice on how to cleanse and parse this field using DQS without having to purchase a reference data service so that if its a company name, it puts the whole field into the FirstName or LastName domain but if its not a company name then it parses the whole name out into the appropriate domains.
I set up a composite domain named FullName which is made up of FirstName, MiddleName and LastName domains and mapped it to my raw data. I then used the composite domain knowledge based parsing to try to parse these values into their respective fields but unfortunately it tries to parse company names as if they were people so I get a lot of last names like "& Associates Inc", etc.
It seems like the easiest way to tackle this would be to create a domain of business words and have a composite domain rule that basically says, if any of the words in the FullName domain are in the BusinessWord domain then put the whole name in the FirstName domain, else try to parse the name into first, middle, last. Is this type of lookup beyond the capabilities of DQS?
So far the only thing i can find for DQS CD rules is to create rules that are hardcoded, i.e. if city Is "London" then change country to "England", but I would like a more dynamic rule that says if BusinessWord In FullName then put the whole thing in FirstName. Just wondering if anyone who is trying out DQS has come across a problem like this and if its solvable without having to pay a ridiculous amount of money for a reference data service.
If you can't clean names and addresses with this thing then what is it good for?
Thanks in advance.
I would tackle this by using the KB in a DQS task as part of an SSIS workflow. This blog post has a reference on how to use the DQS task. For your situation I would use the output of the status column (define a rule) and then redirect the output as per your business rules via additional SSIS tasks.
I know this is not ideal, however I think it would solve the immediate task at hand.

Loading a entity IDs CSV column as hydrated entities in NHibernate

I have a number of database table that looks like this:
EntityId int : 1
Countries1: "1,2,3,4,5"
Countries2: "7,9,10,22"
I would like to have NHibernate load the Country entities identifed as 1,2,3,4,5,7,9 etc. whenever my EntityId is loaded.
The reason for this is that we want to avoid a proliferation of joins, as there are scores of these collections.
Does fetching the countries have to happen as the entity is loaded - it is acceptable that you run a query to fetch these? (You could amend your DAO to run the query after fetching the entity.) The reason I ask is that simply running a query compared to having custom code invoked when entities are loaded requires less "plumbing" and framework innards.
After fecthing your entity, and you have the Country1,Country2 lists, You can run a query like:
select c from Country c where c.id in (:Country1)
passing :Country1 as a named parameter. You culd also retrieve all rows for both sets of countries
select Entity e where e.id in (:Country1, :Country2)
I'm hoping the country1 & country2 strings can be used as they are, but I have a feeling this won't work. If so, you should convert the Strings to a collection of Integers and pass the collection as the query parameter.
EDIT: The "plumbing" to make this more transparent comes in the form of the IInterceptor interface. This allows you to plug in to how entities are loaded, saved, updated, flushed etc. Your entity will look something like this
class MyEntity
{
IList<Country> Country1;
IList<Country> Country2;
// with public getter/setters
String Country1IDs;
String Country2IDs;
// protected getter and setter for NHibernate
}
Although the domain object has two representations of the list - the actual entities, and the list of IDs, this is the same intrusion that you have when declaring a regular ID field in an entity. The collections (country1 and Country2) are not persisted in the mapping file.
With this in place, you provide an IInterceptor implementation that hooks the loading and saving. On loading, you fetch the value of the countryXID property an use to load the list of countries (as I described above.) On saving, you turn the IList of countries into an ID list, and save this value.
I couldn't find the documentation for IInterceptor, but there are many projects on the net using it. The interface is described in this article.
No you cannot, at least not with default functionality.
Considering that there is no SPLIT string function in SQL it would be hard for any ORM to detect the discreet integer values delimited by commas in a varchar column. If you somehow (custom sql func) overcame that obstacle, your best shot would be to use some kind of component/custom user type that would still make a smorgasbond of joins on the 'Country' table to fetch, in the end, a collection of country entities...
...but I'm not sure it can be done, and it would also mean writing from scratch the persistence mechanism as well.
As a side note, I must say that i don't understand the design decision; you denormalized the db and, well, since when joins are bad?
Also, the other given answer will solve your problem without re-designing your database, and without writing a lot of experimental plumbing code. However, it will not answer your question for hydration of the country entities
UPDATE:
on a second thought, you can cheat, at least for the select part.
You could make a VIEW the would split the values and display them as separate rows:
Entity-Country1 View:
EntityId Country
1 1
1 2
1 3
etc
Entity-Country2 View:
EntityId Country
1 7
1 9
1 10
etc
Then you can map the view

Check My Database Design / PHP/MySQL

I'm currently working on improving my database to make room for growth. As it stands, different users have different 'permissions' to areas of the website. Some users have permissions to multiple areas of the website.
I'd like some feedback if I'm doing this in the most efficient way:
tblUsers:
usrID usrFirst usrLast phone //etc....
1 John Doe
2 Jane Smith
3 Bill Jones
tblAreas:
id name
1 Marketing
2 Support
3 Human Resources
4 Media Relations
tblPermissions:
id usrID areaID
1 1 2
2 1 4
3 2 1
4 3 3
Right now, for each "area", I have separate directories. However, I'd like to minimize all of these directories down to one main directory, and then redirect users on logging in to their appropriate 'area' based upon their permissions.
Does it sound like I'm doing this correctly? I've never created a multi-layered site with different permissions and different groups of people, thus, I'm certainly open to learning more on how to do this correctly.
Thanks very much!
The general design is ok. The issues that pop out on me relate to naming.
SQL doesn't need hungarian notation -- generally considered unnecessary / bad (tblUsers -> users).
I wouldn't prefix table-names to column-names ...
... except for column "id" which should always include your table name (i.e. areaId)
Your "first" and "last" column don't make sense (hint: firstName)
I'd rename tblPermissions -> userAreas
Depending on your programming language and database, I'd also recommend using underscore instead of capitalization for your table/column-names.
As for using separate directories for different groups, I'd advise against it. Have the security-checks in your code instead of your directory layout.
Reasoning:
What happens when somebody decides that support is also allowed to do some marketing stuff? Should you change your code, or add a record into your database?
Or what if you have overlapping actions?
#brianpeiris: A couple of things come to mind:
No need for column aliases in JOINs
Makes it easier to search through code ("foo_id" gives less results than "id")
JOIN USING (foo_id) instead of JOIN ON (foo.id=bar.id).
The schema looks fine.
I would suggest that you put access control in the controller and base it of of URL path, so that you are not coding it into every section.
Yes, this seems like it is addressing your need perfectly from the database side.
The challenge will be using the data as simply and declaratively as possible. Where is the right place to declare what "area" you are in? Does each page do this, or is there a function that calculates it, or can your controllers do it? as someone suggests. The second part is evaluating the current user against this. Ideally you end up with a single function like "security_check_for_area(4)" that does it all.

nhibernate and DDD suggestion

I am fairly new to nHibernate and DDD, so please bear with me.
I have a requirement to create a new report from my SQL table. The report is read-only and will be bound to a GridView control in an ASP.NET application.
The report contains the following fields Style, Color, Size, LAQty, MTLQty, Status.
I have the entities for Style, Color and Size, which I use in other asp.net pages. I use them via repositories. I am not sure If should use the same entities for my report or not. If I use them, where I am supposed to map the Qty and Status fields?
If I should not use the same entities, should I create a new class for the report?
As said I am new to this and just trying to learn and code properly.
Thank you
For reports its usually easier to use plain values or special DTO's. Of course you can query for the entity that references all the information, but to put it into the list (eg. using databinding) it's handier to have a single class that contains all the values plain.
To get more specific solutions as the few bellow you need to tell us a little about your domain model. How does the class model look like?
generally, you have at least three options to get "plain" values form the database using NHibernate.
Write HQL that returns an array of values
For instance:
select e1.Style, e1.Color, e1.Size, e2.LAQty, e2.MTLQty
from entity1 inner join entity2
where (some condition)
the result will be a list of object[]. Every item in the list is a row, every item in the object[] is a column. This is quite like sql, but on a higher level (you describe the query on entity level) and is database independent.
Or you create a DTO (data transfer object) only to hold one row of the result:
select new ReportDto(e1.Style, e1.Color, e1.Size, e2.LAQty, e2.MTLQty)
from entity1 inner join entity2
where (some condition)
ReportDto need to implement a constructor that has all this arguments. The result is a list of ReportDto.
Or you use CriteriaAPI (recommended)
session.CreateCriteria(typeof(Entity1), "e1")
.CreateCriteria(typeof(Entity2), "e2")
.Add( /* some condition */ )
.Add(Projections.Property("e1.Style", "Style"))
.Add(Projections.Property("e1.Color", "Color"))
.Add(Projections.Property("e1.Size", "Size"))
.Add(Projections.Property("e2.LAQty", "LAQty"))
.Add(Projections.Property("e2.MTLQty", "MTLQty"))
.SetResultTransformer(AliasToBean(typeof(ReportDto)))
.List<ReportDto>();
The ReportDto needs to have a proeprty with the name of each alias "Style", "Color" etc. The output is a list of ReportDto.
I'm not schooled in DDD exactly, but I've always modeled my nouns as types and I'm surprised the report itself is an entity. DDD or not, I wouldn't do that, rather I'd have my reports reflect the results of a query, in which quantity is presumably count(*) or sum(lineItem.quantity) and status is also calculated (perhaps, in the page).
You haven't described your domain, but there is a clue on those column headings that you may be doing a pivot over the data to create LAQty, MTLQty which you'll find hard to do in nHibernate as its designed for OLTP and does not even do UNION last I checked. That said, there is nothing wrong with abusing HQL (Hibernate Query Language) for doing lightweight reporting, as long as you understand you are abusing it.
I see Stefan has done a grand job of describing the syntax for that, so I'll stop there :-)