Application Design Assistance

Topics: General
Apr 28, 2011 at 6:41 PM

I could use some advice on how to design my application inside Orchard if anyone has some...

I'm writing an application that is going to run within Orchard, but the only interaction it's going to have with Orchard is User information and only to the point that I need to store the current Orchard userid to tag my table entries as belonging to that person. I don't need to have any interaction with my application from the Orchard Administration other than 2 basic permissions settings which I have already done...Admin to get to certain pages, Authenticated to get to the rest.

All of the examples and documentation I have found so far have dealt with Content Types and Content Parts which I'm not sure is the best approach for my application. I need to be able to write and retrieve information from a database, so I have set up a model that will do this and migrations file that created the tables.

What is the best method for me to store, retrieve, and display my data in this type of scenario? Is there a Module or something that I can use as an example to follow?

Your help is very much appreciated.

Apr 28, 2011 at 7:16 PM

You can use IRepository<TRecord> to perform CRUD operations on the database. You should be able to find a number of examples of IRepository in the Orchard source code, as there are quite a lot of tables that work without being content parts.

But really it depends what your tables are and what data you're modelling as to the best approach you should take.

Coordinator
Apr 28, 2011 at 7:49 PM

Another question might be why do you think you need to use Orchard on this project?

Apr 28, 2011 at 7:55 PM
Edited Apr 28, 2011 at 7:56 PM

I figured that question would be coming =)

I want to use Orchard to manage every other aspect of the website other than the application portion I'm writing. The content management is perfect for everything except the engine that drives my application, so I basically want to add my application on as a module. While not giving away the entire thing, I'm basically building a fantasy gaming application that I want to run within Orchard. I hope that kind of gives you an insight into what the application will be like. I need to maintain data such as sports, teams, games, game results, etc...and all of that tied back to the users of Orchard as they compete against each other in my application.

I originally wrote the gaming application in ASP.NET Forms...so not only am I trying to convert it to MVC, I'm trying to do it within Orchard at the same time. A bit of a double whammy for my experience level with MVC right now.

Coordinator
Apr 28, 2011 at 8:05 PM

Makes sense. So yes, Pete's advice is good.

Apr 28, 2011 at 9:47 PM

Well...I ended up building the first set of data as a content part. It honestly just seems easier that way and I couldn't seem to figure how to use IRepository with anything other than a content part.

So...I have a contentpart and a contentpartrecord in my Models folder and I created a handler for the contentpartrecord. I ran the data migration and updated my module and presto...I have a table. I manually added a record to the table for testing. I created a controller which is rendering a view with some basic text on it. What I can't seem to figure out now is how to retrieve that record so I can pass it to my view. I'm used to using entity data modeling, so the Orchard way is totally new.

So...say my model has 2 items which translates to 3 columns in the table since Orchard added the Id column:

Id
game_nm
game_act_ind 

Can you very briefly instruct me on how I would return all records in this table and also how I would return just one record by specifying the game_nm?

Sorry to be such a noob...and thanks for the help

Coordinator
Apr 28, 2011 at 10:27 PM

You get an IRepository<YourRecordType> and then get Table on it. It's IQueryable, which means that you can query it with Linq. You can also Fetch with a predicate.

Apr 28, 2011 at 11:11 PM

Got it!  That works and I'm bringing back the record from the table. Thanks for guiding me through that. I'm assuming I can use regular Linq statements to do CRUD operations as Pete suggested?

I think if I combine this with some ViewModels to bring in data from different tables, I should be all set!

Coordinator
Apr 28, 2011 at 11:14 PM

Also look for examples in existing modules, in particular for persisting data: you should only need Linq for querying, not for writing (see other methods on IRepository<T>.

Apr 28, 2011 at 11:22 PM

Any suggestions for a particular module that shows persisting data through a Controller? Most modules seem to use a driver.

Coordinator
Apr 28, 2011 at 11:25 PM

Pretty much any admin controller should do the trick.

Apr 28, 2011 at 11:31 PM

Will do...thanks again!

Apr 29, 2011 at 7:52 PM

One more quick question on this topic...

Generating my Views in this particular manner, via Controller, does my View populate in the "Content" zone of the theme? Is there a way to populate different aspects of my View into other zones or am I locked into the "Content" zone and just have to style my Views however I want them to display inside "Content"?

Thanks!

Coordinator
Apr 29, 2011 at 7:54 PM

You can get the Layout shape from the work context, and from there get a specific zone and inject any shape in there. See the Buy From Amazon module from the gallery for an example (not from a controller but it makes little difference).

Apr 29, 2011 at 8:58 PM
Edited Apr 29, 2011 at 9:00 PM

Gotcha...almost there I think.

In my View, I can put the following

 

@{WorkContext.Layout.AsideSecond.Add("TEST", "5");}

 

and it inserts the word TEST in that zone.

Can I use WorkContext inline in my View to display content or do I have to pass it a shape. For example, I tried this

 

@{WorkContext.Layout.AsideSecond.Add(New.test(), "5");}

 

and added a test.cshtml file to my Views folder and it picked it up and displayed it. I tried to pass it the Model, but it wouldn't take it.

 

So I guess my disconnect is if I use WorkContext directly in the View, how do I either render the code for my View inside it, or if I pass it a shape, how do I get my Model over to the shape?

Or am I supposed to be doing all of the WorkContext coding in my Controller?

Coordinator
Apr 29, 2011 at 9:07 PM

New.test(Model: whatever) should do the trick. But you can also do New.test(foo:"bar") and then from the template @Model.foo will display "bar".

Coordinator
Apr 29, 2011 at 9:13 PM

You can do New.Foo(Bar) to set the inner Model property to the Bar object.
And you can also use a New.Foo().Bar("bar").Baz("baz")

Apr 29, 2011 at 9:48 PM
Edited Apr 29, 2011 at 9:49 PM
Can I only pass objects within the model to the template or can I pass
the entire model itself so that I can decide in the template which
model objects to render?
darn phone...sorry for the double post.
Apr 29, 2011 at 9:48 PM
Edited Apr 29, 2011 at 10:11 PM

Can I pass the entire Model or part of a ViewModel to the template?

For example, I'm sending my View a ViewModel. I'd like to pass part of that ViewModel to a template that renders in one WorkContext and another part of that ViewModel to a template that renders in a different WorkContext.

View code

 

@model IEnumerable<Raptor.MyRealityPicks.Models.ShowPartRecord>
<h2>@T("You've reached the Show Page")</h2>
@{WorkContext.Layout.AsideSecond.Add(New.test(Model), "5");}

The above code was just based off a IEnumerable of show names. I also tried this

 

@{WorkContext.Layout.AsideSecond.Add(New.test(Model:Model), "5");}

to no avail.

 

The object that gets passed is of type IShapeProxy, so it throws an error. 

I think I'm lost on the "whatever" part you described above...what is it that I can actually pass after "Model: " that I can use in my template?

and what has to be in my template that the WorkContext is calling to accept whatever I'm passing?

Thanks again for your patience guys.

Apr 29, 2011 at 10:26 PM

I think I worked through it...if I pass it as New.test(shows:@Model), it seems to pass the entire model and then I can pick it up in the template using Model.shows

Alright...I think I'm good now. Thanks again!

Apr 30, 2011 at 10:21 AM
psenechal wrote:

I figured that question would be coming =)

I want to use Orchard to manage every other aspect of the website other than the application portion I'm writing. The content management is perfect for everything except the engine that drives my application, so I basically want to add my application on as a module. While not giving away the entire thing, I'm basically building a fantasy gaming application that I want to run within Orchard. I hope that kind of gives you an insight into what the application will be like. I need to maintain data such as sports, teams, games, game results, etc...and all of that tied back to the users of Orchard as they compete against each other in my application.

I originally wrote the gaming application in ASP.NET Forms...so not only am I trying to convert it to MVC, I'm trying to do it within Orchard at the same time. A bit of a double whammy for my experience level with MVC right now.

What I would just say here is that I'd approach this all as content. Sports, teams, games, game results - can all be implemented as content items and this will bring benefits (e.g. you easily could add Ratings to any of them using the existing ratings module).

In general I'd define content as "anything that is seen by the user". So things like stats counters might be in another table, but anything you're surfacing to front-end display should just be content and use Orchard's whole rendering pipeline.