Using your NBusiness objects

Once you have created your Entities and compiled them into a class library you are ready to start using them in your main project.
Add a reference
The first thing you want to do is add a reference to your generated assembly from your main project. You may also need to add a reference to NBusiness.dll. In your main project you may now access all of your entities as objects. Suppose you have the following Entities:
using NBusiness.Templates;

family Simple as EntityBase, EntityCollectionBase
{
	entity A as EntityBase, EntityBaseCollection
	{
		field auto id int Aid;
		field string Data;
	
		relationship BList with B as child;
	}
	entity B as EntityBase, EntityBaseCollection
	{
		field auto id int Bid;
		field int Aid;
		field strng Data;

		relationship A with A as parent;
	}
}

Assuming you are using the default templates you will have 4 business objects created for you:
public class A : EntityBase { ... }
public class ACollection : EntityCollectionBase<A> { ... }
public class B : EntityBase { ... }
public class BCollection : EntityCollectionBase<B> { ... }

And you will be able to reference them in your project in ways similar to the following code:
using System;
using Simple;

class Program
{
	static void Main(string [] args)
	{
		foreach(A a in ACollection.FetchAll())
		{
			Console.WriteLine("a - " + a.Data);
			foreach(B b in a.BCollection)
			{
				Console.WriteLine("\tb - " + b.Data);
				if(!b.A.Equals(a))throw new Exception("Error!");
			}
		}
	}
}

Data Access

Config settings
You will need to add some settings to your configuration file to tell NBusiness how to do data access. Here is an example of this:
<configuration>
	<configSections>
		<section name="NBusiness" type="NBusiness.Data.Settings, NBusiness"/>
	</configSections>
	<connectionStrings>
		<add name="DBConn" connectionString="Data Source=.\sqlexpress;Initial Catalog=Example;Integrated Security=True;" providerName="System.Data.SqlClient"/>
	</connectionStrings>
	<NBusiness 
		ConnectionStringName="DBConn" 
		ProviderType="NBusiness.Providers.SqlProvider, NBusiness"
                                AuthorizeDefault="True"
                                AccessDefault="True" />
</configuration>


Data Access
The default business object framework generates code that assumes certain stored procedures will exist. There is a stored procedure template that comes with NBusiness (NBusiness.Templates.Sql.SqlScripts) designed to generate these stored procedures for you. It will also generate all of the Tables and Foreign Keys for you. You may choose to not use the default generated stored procedures but you may need to make them yourself in this case. Additionally, a custom provider may be needed if you do not wish to use the default system of using stored procedures.

Next: Using the entity data source control

Last edited Sep 5, 2007 at 2:47 AM by justinc, version 10

Comments

No comments yet.