NBusiness integration with Visual Studio 2008

The Visual Studio Extensibility (VSX) team has recently written a blog post announcing the Visual Studio 2008 SDK 1.0. I am pretty excited for this I will be probably wrapping up the last touches on NBusiness v2.1 soon and release it so I can get on to working with Visual Studio 2008. I’m pretty excited about it actually. Once I release it I will write a blog post outlining some of the changes and new features.
The one question I have at this point is when hosting providers (such as Go Daddy) will be providing the .net Framework v3.5 on their servers. I believe that they don’t even have v3.0 right now so I’m a little skeptical that they’ll be providing support for 3.5 anytime soon. However it really seems like that, even though 3.0 have been out for a while, a lack of official tooling has caused a lot of people to stick with 2.0 and VS2005 for the time being. There are lots of exciting things about VS2008 and .NET 3.5 and now that it’s all officially released I bet we’ll see a surge in applications using this technology and probably prompting Go Daddy (and others) to support it.
Along these lines I have been playing around with the new MVC framework some and I have to say… it’s pretty damn nice. I am all for dropping the overly complex ASP.NET page life cycle we currently have to deal with. Other than some issue I was having with rendering a custom view (could be my fault but it’s basically an Alpha preview so I expected a few issues, I’m not really worried) I definitely prefer this model. Without a doubt NBusiness will fit in nicely as the “model” portion of the framework.
I also went ahead and used the LINQ to SQL datasets that come with the v3.5 framework to see how that works to… wow it’s great. In fact I’m sort of going through a bit of an identity crisis with NBusiness as I try to decide if it’s even worth bothering to try to compete with it. I’m trying to come up with enough benefits in NBusiness to outweigh the benefits of LINQ to SQL before I decide to really go for it.
So here are a few of the reasons that I have come up with so far, they’re mostly just cerebral arguments at this point I can’t say that they’re correct because when it comes down to it I think that it’s practicality that really decides what is better.

In favor of NBusiness

NBusiness supports business objects

Data validation, access and authorization rules are all very easy to setup in NBusiness. You could still probably do this with the objects generated by the linq to sql datasets but it doesn’t appear to be built right into the base class objects nor is there a way to specify these things in the designers.

NBusiness is not dependent on a database for metadata

The datasets are dependent on having an active server connection, I’m not sure how it works exactly but I’m guessing it is pulling in the schema each time you build. I’m not sure how well I like this, there are definitely lots of problems with this such as the situation where the database has yet to even be designed or you have a large database with lots of columns and tables. There are ways to deal with these situations but I’m not sure as a matter of principle that I really like having my build process dependent on pulling meta-data out of another process at build time. It would be really nice if you could just have SQL scripts lying around and it was able to infer the schema out of that but as far as I know that isn’t how it works. Also, of course mapping your objects to your database 1:1 is undesirable and it is yet to be seen how editing a data set in design mode will be when working with a large database full of ugly naming conventions and missing key constraints and all of the other various problems legacy databases tend to have.
In the case of NBusiness, your entity definitions are the metadata for your build process. It can generate for you a simple database based on those definitions but in the case of an existing database or legacy database will do the mapping in stored procedures yourself but you end up with business objects rather than a data access layer.

NBusiness code is template driven

You have full control over the code that is generated. Maybe I’m wrong about this but I didn’t see any obvious way to change what is output by the designer when you build your project. The objects that the designer creates are partial classes, so that’s nice that they can be extended but what if you want to control everything about their output? It seems (at first glance) that they are breaking one of the 5 laws of code generation.

NBusiness prefers stored procedures

I’m a little skeptical about the dynamic SQL approach used by LINQ. I mean it’s nice to be able to do dynamic sql but it seems wise to use stored procedures as much as possible and probably exclusively. For small applications where you’re trying to rapidly produce results and performance isn’t really that big of a deal for certain uncommon calls, sure do dynamic SQL. But the backbone of your application should really be with stored procedures. I believe it is possible to call stored procedures still with these datasets but I really seems like there is a heavy focus on LINQ and dynamic SQL which will no doubt result in entire applications using dynamic SQL everywhere, which I’m skeptical of.
I have recently added support for dynamic sql in NBusiness (v2.1). It’s obviously not as well tested and robust as LINQ to SQL at this point but it’s pretty good I think. In the 3.0 version there will probably be a way to use LINQ directly on NBusiness objects just like with these datasets too (I’ll probably write another post about this). But I would like to keep the preference of NBusiness strongly on stored procedures as much as possible.

In favor of LINQ to SQL Datasets

Awesome tooling

Of course the VS integration blows NBusiness out of the water. It’s fast and pretty and everything works like it should. This alone is a good reason to use it over NBusiness. I believe I can get NBusiness to this point eventually but it is definitely going to be hard. Also there is nothing extra to download or install to get this working; it’s just built into VS 2008, very hassle free.

Very lightweight, simple, flexible

It’s so easy once you have your database created and the objects it makes are so lightweight and simple there is virtually no learning curve. It’s so light weight it’s almost worth just saying ‘screw it’ to the robust business object in favor sweet simplicity. I’m actually struggling with this one the most at this point. I suspect that a larger application may push this theory over the edge however.

Promotes database to be designed separately

I have this theory that having a single source of meta-data (or the source of truth) rather than being dependent on a database’s schema for meta-data is superior. However I’m having some doubts right now. Shouldn’t it be true that separating the concerns of the database and the objects by handling them in separate tools be the most correct theory? What is the best strategy for bridging these two together? The implementation of DLINQ is so good it’s causing me to doubt myself here.
I’m still leaning towards continuing on with NBusiness and with some of the new 3.0 features I have been kicking around in my head it will be even better than it is now but I just want to make sure I’m not wasting my time here. I want to really think about it and make sure I continue on, not as a victim of my own ego but because it is genuinely a worthwhile endeavor.

Entity Inheritance

Inheritance as it pertains to objects is pretty easy to understand and represent. There are lots of complications and benefits that most developers these days understand well. It is also possible to represent inheritance in a relational model, which is effectively a 1:1 relationship between tables. For example you might have the following table schema:

Person

Customer

Employee

pid

cid

eid

Name

 

Office

Ssn

 

 

In this schema you would have foreign keys going from Customer and Employee to person. It’s a way to normalize your data that is similar to inheritance. This is something that I have wanted to support in NBusiness but it is a little tricky as you might imagine. What principles of class inheritance apply to Entities as well, I’m not entirely sure? In the case of entities it’s mostly about inheriting data and behaviors and you don’t really have encapsulation or polymorphism.

I’ve been thinking a lot about this after someone at last weekend’s code camp asked me a question about whether or not it was possible to have entities only retrieve subsets of data. The answer to that questions is a tentative ‘yes’ but in fact it would require a fair amount of manual work to get your stored procedures to map back to the correct tables. It’s possible but you’d lose a lot of the benefits of code generation.

So after thinking about this a bit I realized that the two problems (entity inheritance and retrieving subsets of data) had something in common, which I’m calling data hiding for now. In both situations you may only want a subset of data and behaviors from the base entity so I’m thinking of adding a “hide” keyword which will allow you to not inherit fields, properties and rules from the base entity.

In fact I’m thinking about the possibility of creating a new type called use case or something like that. A use case would be just like an entity except that it can only inherit fields, it cannot declare them, sort of like a reverse abstract class. Also a use case must always inherit from an entity. I would imagine that you could create sub families below your root entity definitions and here you would create specific use case definitions. Or maybe model would be a better keyword? Actually I rather like that now that I think about it. The sub families would be more like the use case while the models will describe the various components of a use case.

So suppose you have a Customer entity, if a user logs in and is a Customer you have one use case where they are managing a customer data and have a variety of behaviors associated with that. In this use case they may only have a subset of customer data available to them for editing. Now you may also have a different use case where an administrator of the application logs in and needs to be able to edit a different subset (or complete set) of customer data. As it is now if you declared two different Customer entities for the different use cases you would need to then create the tables and stored procedures to allow them to share the data. With entity inheritance and models you would be able to much more effectively express data hiding while maintaining the ability to share data.

I will work out the details a little bit more but for now check out my wiki page on Entity Inheritance with a little more detail and an example. These features are starting to clarify a bit more in my head; you can look forward to a few other cool features soon such as:

·         Dynamic SQL Fetching (v2.1)

·         CSLA Templates

·         VS 2008 compatibility (v3.0)

·         LINQ support (?)

·         Entity Attributes

·         Partial Entities

·         Custom authorize / access rules

·         Validation rules that apply to only certain CRUD operations

·         Validations warnings and info

Also, one last interesting thing is that I fired off an email To Scott Guthrie at Microsoft the other day asking him about the MVC framework they’re working on. He responded by telling me that they’ll be giving me an advanced preview of the framework in a couple of weeks which is really exciting! It really looks like NBusiness could be a natural fit for the MVC framework and I’m pretty excited to check it out. I’m glad to see that they’re having such an open relationship with open source projects.

NBusiness v2.0.1 Released

This release fixes a few issues that were reported to me related to sibling relationships, the schema updater and the schema updater menu items. Let me know if you still have problems with the menu items because I can’t seem to duplicate the issue on my development machine or test VPC. I have done a few things that I think might help but it’s really hard to say 100%.
Anyway, with the sibling relationships I realized that there was no guarantee as to the order of the relationships. Meaning if you had Person and Address entities, in order to create the intermediate tables in the sql scripts template, the template would either name it PeopleAddresses or AddressesPeople… depending on the order of the entities. With no guarantee as to the order there was no guarantee what the table would be named. This would cause custom scripts to blow up or running the schema updater on a database with a table of the other name would result in the database not being rebuilt properly either. So now the template will order all of the entities alphabetically before attempting to create sibling related scripts, which will guarantee that all of the tables will be predictably named. In the example above you should alwasy get AddressesPeople from now on.

Hosted Website Installations

 
After working with a hosted website for a little while it’s easy to come to the conclusion that deployment can be a real pain in the neck. Large 3rd party web hosts such as Go Daddy (which is who hosts this site currently) certainly do not give you an easy way to migrate certain changes to your website. If your website is simple HTML and you’re doing some minor incremental changes from time to time then it’s not really a big deal but when it comes to a dynamic, datadrive website site, even one as simple as this one, deployment is painful.
 
The problem comes from not having direct access to the database. The database is securely hidden behind a firewall but as annoying as this is you wouldn’t really want it any other way. VPN access would be nice but it’s understandable why this isn’t available. As a solution they typically give you access to some web front end to your database and while this is better than nothing, frankly, these applications usually suck. I’ve found that not only are they usually pretty slow and visually confusing but they are typically not compatible with the same transactional scripts that you would run in your own development environment.
 
[Example of GoDaddy’s Web DB Frontend]
 
So this is the cruxt of the problem. The first time I went to deploy my site I had a rather long script full of table creates and primary keys and foreign keys and lengthy stored procedures only to attempt to run it in this web front end and have it fail miserably. I then removed the GO statements from my srcript, since it didn’t seem to like them, and I ended up with a spattering of tables and sprocs and had to go through and manually delete a lot of things. It was very painful and tiresome.
 
So after doing this about once you quickly realize there must be a better solution. The sample code posted with this blog is an example of how to make a reusable hosted website installation package. It’s pretty simple but that’s usually the best way to do things. You’ll probably want to refactor the code in this example to fit in with your project but here is the basic idea behind everything.
 
First off, you have your installer classes. You have an abstract InstallVersion class which is essentially a collection of InstallActions along with some version information. You will create one of these classes for each version you roll out to your server. It’s probably a good idea (and not much work) to keep all of your scripts and install version classes along the way rather than just upgrading the same one over and over. The reason for this is just in case you need to rebuild a database due to a migration, a crash or perhaps a new deployment without any of the old data. In those cases you may be stuck with nothing but ALTER TABLEs at the end and you’ll need to install everything from step 1 to 100 to get back to where you want to be.
 
Your first script will have mostly CREATE statments (probably from the complete.sql generated by NBusiness) but each subsequent one will have ALTERS as well as other things needed to massage your data into the new formats.
 
There are a couple of already made InstallActions created for running sql scripts but you can imagine all sorts of other actions such as ones that delete files, create/edit/delete Users and Roles if you’re using ASP.NET Role/Membership providers. You could also imagine some actions that might do something like send an email to make sure you have your sites SMTP configurations setup correctly.
 
To configure your installer simply add a custom install configuration section to your web.config. Check out the sample to see how to do this. Then you create a page in a secure directory (accessible only by admins) and add the Installer control to that page. The installer control will look at your configuration settings and load your install classes and ask them if they have been installed or not. If they have not then buttons will be created for that version, ready for you to execute the install actions. We’re using NBusiness here to keep track of installed versions but you could just as easily come up with a scheme for saving files or something. Just remember that you don’t always have write permissions in medium trust like you do in your full trust development environement so always be sure to add the <trust level=”Medium” /> to your web.config so you can have a development environment with the same restrictions!
 
This is just one way to accomplish these goals. I’ve tried to add some semblance of a pattern and reuse to this problem and while this isn’t perfect (notably a lack of transactions across InstallActions) it’s MUCH better than nothing while still remaining pretty simple.
 
Enjoy!

NBusiness v2.0 Released!

After working for over a year on this project I have finally achieved my original goals! It’s been a long and intense year of coding for me and I’ve loved every second of it. I still have some major features ahead of me and (no doubt) lots of debugging still but I have managed to produce something that I feel is pretty good quality and will be beneficial to everyone that uses it.

Download NBusiness v2.0 here.

Of course there are still some known issues to work out and plenty of room for improvement in the entity authoring experience, especially as it relates to intellisense in the C#/VB code beside. I’m not sure how to fix this though I have looked into it some already but I can say that it’s a top priority here.

When releasing this build I had to decide whether or not I should release an “imperfect” build or delay it longer but fix all of the issues I was seeing. If this was a commercial project I probably would have tried to work out more of the issues first but since it is (mostly) only me I felt it wasn’t worth delaying things much longer, since I can only do so much so fast. However, most of the known issues are annoyances and not critical show stoppers anyway. Despite these few remaining problems I’m very happy with this release and find myself wanting to use it with any new project. In fact it’s hard to imagine doing it any other way at this point!

Here is a screen shot of NBusiness integrated into Visual Studio (click to view fullsize):

I’m going to try to make some screen casts pretty soon too. I have never really done that before but I think a couple simple screen casts would be pretty helpful for people.

And please everyone, let’s make use of the forums and work items on codeplex!