views:

228

answers:

2

Hello,

I’m new to PLINQO, I’m fairly new to LINQ2SQL and I’m adding a new DAL (called DAL2) in parallel with our old existing DAL. Our idea is to start using linq2sql for every new thing we add and slowly start moving old DAL usage to the new DAL2.

As we all know, DLINQ is nice for simple things but in more complicated scenarios (many2many, deattached objects, etc.) issues arise instantly and new need to hack around it. PLINQO (together with CodeSmish) comes to the rescue and adds all the necessary structure and tools to make it more usable. So far, so good.

Now I already have my DAL2 (I’m using Managers). It “works”. But I have a few doubts.

I understand that returning an object composed of the following query:

select name,  Count(*) as Total from SomeTable

…is not a weird scenario in most DB applications. The query is a simple example of what would be a nice anonymous type.

Now for a second, imagine a table structure like this.

Tag (1) <—> (n) PatientTag (n) <—> (1) Patient

The idea is to know how many patients each tag has. A simple inner join would fix this with SQL, between tag and patient tag. I don’t even need access to Patient table for that. (I just need the count).

After all, all I want is for example:

TAG1, 33

TAG2, 21

TAG3, 6

etc…

Suppose I manage to create a linq2sql query that does it:

   var result1 = from pt in dc.PatientTag
                 join t in dc.Tag on pt.TagId equals t.TagId
                 select new { TagName = t.TagName };


   var result2 = from q in result1
                 group q by q.TagName into gp
                 select new { TagName = gp.Key, Total = gp.Count() };

(I’m new to LINQ so if the above is not good, forgive my lack of “LINQissm”)

Given that I cannot return that “new” anonymous type (unless I go for Object and reflection which is not my intention), I assume I have to create a “helper” class to contain the two things (name and total). The idea is that the above code should sit somewhere in the datalayer or the business layer, and not in the UI’s code.

Now the real question:

If the above is true, where should I create that “helper” if I want to return that result (or array of results) to a UI (for proper displaying and handling)?

1) In DAL2/Helper/TagNameTotal.cs (to put an example)

2) In a BLayer/Helpers/TagNameTotals.cs?

3) None of the above? (or either?)

If the above is not true, what am I incorrectly thinking?

Isn’t it normal when someone wants to pass the result of a query to the UI and modify it? Say in the above example I’d like to change TagName in the UI (maybe that’s not the best example but it applies).

Excuse me but I still find the whole LINQ2SQL to be a little bit crude when it’s used outside a webproject or a simple app. These are basic things we’ve been doing with ADO.NET (and recordsets before that) forever.

Make a select/join/group/crazySQL, modify, commit the changes back.

PLINQO is nice because it takes away the hassle of LINQ2SQL in its crude form (many 2 many, deatached, context regeneration, cache, etc.), but it’s still LINQ, so DLINQ practices must apply.

What am I missing?

note: Do not confuse PLinq with PLinqO.

A: 

This sounds to me like a shameless plug for the PLinqO product. I hope this is not your intent. I looked at PLinqO and perhaps would have got it except you had to purchase CodeSmith to get it. No thanks.

I have built a full featured 3-tier platform using L2S, and I encountered and dealt with issues related to M:M relationships, detached entities (especially with updates), context handling etc. without the use of PLinqO and any other 3rd party tool. It can be done, once you understand how L2S works. I think our solution is pretty elegant and our performance is quite good.

For those reading this, if you want to spend money on third party tools, invest in LinqPad and Linqer. You can pick up both of these tools for less than $75 total; they are fantastic. And I'm not associated with either of them.

Randy

Randy Minder
Although the utility (or lack of) PLINQO was not the question, you haven’t really come to understand the value of CodeSmith. Codesmith is all about code generation. I suggest you re-read about it.
Martín Marconcini
I understand very well what CodeSmith does. But, I don't like having to purchase one thing to get what I really wanted in the first place. If we decide we need full blown code generation, we're going to go with Reegenerator (http://www.reegenerator.com). It's much less expensive.
Randy Minder
I can’t comment on reegenerator, but PLINQO and CodeSmith are fantastic, only there’s not much documentation and the videos are outdated. It is obviously possible to use CodeSmith once (trial), PLINQO, generate all and never regenerate the code again, but the beauty of CodeSmith is the regenerate button, which will preserve all your changes and will bring new stuff from the DB. PLINQO only uses LINQ technology, it’s nothing fancy, but it’s all done.
Martín Marconcini
The cost is neither here nor there, really. I know that my use of code generation saved me several days on my current project. PLINQO, as a free framework, was a big help as well acquainting me to the ins and outs of LINQ to SQL. The CodeSmith license was about 5% of the invoice for one month of work. I used NHibernate on my last project and I'm likely to use it again, but shucks. I'm impressed with Codesmith and PLINQO.
sympatric greg
@Sympatric Gred - Are you also aware that PLINQO's implementation of M2M support is completely flawed (by their own acknowledgment)? Several months ago I ran across a bug in their M2M support and sent them a support email. They acknowledged their M2M support was "completely flawed" and didn't know when it would be fixed.
Randy Minder
Your trippin Randy - I know this post was a long time ago now, but I have been using CodeSmith for years -- Code generation is key man, even if you don't use the Plinqo framework, you can make your own templates to automate parts of your job for common frameworks, etc. I recommend you test it out yourself. Not only Plinqo, but other frameworks it will generate for you, and then you just go in and make your own adjustments. The post I felt was a bit harsh -- and I wasn't even looking for info on this. Just caught my eye. I'm sure your way is great also, just don't shut the door on others.
TravisWhidden
+2  A: 

I have answered this post in the codesmith community, however I was thinking about this and came across this post. You may find this post some help

Basically you create a mini class and use the let statement to define a sub set then lazy load it when you need it....

Rippo
Thanks for pointing that other post. Will review both ideas.
Martín Marconcini
No problem, glad to be of service to a fellow PLinqo user. Doesn't seem to be many of us around!
Rippo