Next time I have to handle XML I'll be using this class by Grant Skinner.
Next time I have to handle XML I'll be using this class by Grant Skinner.
May 23, 2005 at 11:19 AM in Flex / Air / Flash | Permalink | Comments (0)
I had a problem using the Expression,In object in NHibernate as it can't evaluate properties of a property, for example if you have an Order that has an property name Status of type
OrderStatus and that object has a integer property named orderStatusId.
E.g. (psuedo classes just for this example)
class Order
{
private OrderStatus Status;
}
class OrderStatus
{
private int OrderStatusId;
}
To retrieve a list of orders with orderstatus 's that match pending or shipping where pending=1 and shipped = 2 you may try :
IList Orders = session.CreateCriteria(typeof(Order)).Add(Expression.In("Status.OrderStatusId", states )).List();
where the 'states' argument is an ArrayList of order states, e.g. pending=1, shipping=2,.
This doesn't work as the Expression.In can't evaluate a property of a property, namely the Status.OrderStatusId from the Order object.
You could rewrite this using a bunch of "OR keywords" in the query but doing a bunch of string manipulation however a concatenated query string can lead to SQL injection vulnerabilities and you have to figure out how many "OR statements" there are and if there is only one state to check for there is no OR at all, the code ends up being more complicated and verbose than desired.
This query is being driven by four check boxes on a form to determine which order statuses the user wishes to see so one could be checked (no OR keywords would be created)
e.g.
from totowebapp.DomainModel.Order as o where o.Status.OrderStatusId =1
Or could include all four checkboxes
from totowebapp.DomainModel.Order as o where o.Status.OrderStatusId = 1 OR o.Status.OrderStatusId=2 OR o.Status.OrderStatusId = 3 o.Status.OrderStatusId =4
So an easier way is using the Query object and the "in" keyword
for example : we create an ArrayList named states with the values 1, 2, 4 (assuming pending, shipped and canceled checboxes where clicked)
IQuery query = session.CreateQuery("from totowebapp.DomainModel.Order as o where o.Status.OrderStatusId in (:states)");
query.SetParameterList("states",states);
orders = query.List();
this would result in the query
from totowebapp.DomainModel.Order as o where o.Status.OrderStatusId IN (1,2,4)
This does allow us to query against properties of a property and now we can dynamically build the set that must be matched by populating the states ArrayList. This reduces the amount of code required to match a set of values.
May 22, 2005 at 01:01 PM in Web/Tech | Permalink | Comments (2)
Darron Schall has a nice blog with some great examples of using the neoswiff C# to .SWF compiler I mentioned in an earlier post, I particularly like the XML example he recently posted.
May 20, 2005 at 03:08 PM | Permalink | Comments (0)
I'm using the 'free" editor with typepad and it was giving me the big finger when I tried putting some ASP.NET markup in the edit HTML window to post on Ginormous after considering writing a quick escape pag/webapp in either javascript or .NET to encode the markup I came across this online HTML encoder , simple and saved me 30 minutes of coding my own :)
May 20, 2005 at 02:43 PM in Web/Tech | Permalink | Comments (0)
In one of my applications I have an Order object which has a child object Status, I had a devil of a time figuring out how to get the Datagrid component to reference the child object when binding it to an IList of Order objects.
Example (classes chopped shorter for this example)
public class Order
{
private int orderId;
private OrderStatus status;
public int OrderId
{
get { return orderId; }
set { orderId = value; }
}
public OrderStatus Status
{
get
{
if (status == null)
status = new OrderStatus();
return status;
}
set { status = value; }
}
}
The Status object :
public class OrderStatus
{
private int orderStatusId;
private string name;
public int OrderStatusId
{
get { return orderStatusId; }
set { orderStatusId = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}
In the Datagrid I had to create a ItemTemplate tag and use the DataBinder.Eval method to get the Status from the Order object by first casting the Container.DataItem to an Order object and then accessing the Status object
<asp:DataGrid id="ordersGrid" runat="server" AutoGenerateColumns="False" GridLines="Vertical"
CellPadding="3" BackColor="White" BorderWidth="1px" BorderStyle="None" BorderColor="#999999"
Width="552px">
<Columns>
<asp:BoundColumn DataField="OrderId" HeaderText="Order Id"></asp:BoundColumn>
<asp:BoundColumn DataField="CustomerId" HeaderText="Customer Id"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Order Status">
<ItemTemplate>
<%# DataBinder.Eval( Container.DataItem, "Status.Name") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn ButtonType="LinkButton" Text="View Details" HeaderText="Options" CommandName="ViewDetails"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
The solution was quite simple but it did take some time to figure out initialy having never used Template columns or the DataBinder.
May 20, 2005 at 01:41 PM in Web/Tech | Permalink | Comments (0)
I had to very quickly create a dropdown listbox for the date and year in .NET to be used as part of a credit card capture page, the code turned out to be pretty easy, assume you have two DropDownList components on your page named monthDropDown and yearDropDown
string [] months = new string[12];
for (int i =0; i<12; i++)
{
months[i] = (i+1).ToString("0#"); // include leading "0"
}
string [] years = new string[11];
for (int i=0; i<11; i++)
{
int q= 5+i;
years[i] = q.ToString("0#"); // include leading "0"
}
this.monthDropDown.DataSource = months;
this.monthDropDown.DataBind();
this.yearDropDown.DataSource = years;
this.yearDropDown.DataBind();
To get the value of either you can do :
card.ExpirationDate = this.monthDropDown.SelectedItem.Text + this.YearDropDown.SelectedItem.Text;
which would give you the string "0405" for april 2005.
At some point I should create these as components in the the Visual Studio Toolbox as I'm sure I'll need them regularly.
May 20, 2005 at 01:02 PM in Web/Tech | Permalink | Comments (0)
Came accross this cheap mapping component at maps4flash.com fancier ones are available from flashmaps and DIY Map:Create Clickable Maps in Flash.
May 20, 2005 at 12:46 PM in Flex / Air / Flash | Permalink | Comments (0)
I have a reasonably small project for a client, they want a very basic shopping cart, however the items price is determined by its color and the shipping cost is per item. Once the user clicks submit we capture shipping/billing info and credit card, do the processing with verisigns payflow (my god, what an archaic api that is, please someone write a webservice to it) and if successfuly authorized we then insert the order into the database. I have another admin only area where a shipping clerk can look at the orders, their initial state would be "pending shipment" and once the clerk marked the order as "shipped" the system does a delayed capture on the credit card (actually charge the card) based on the earlier authorization, the order is then updated to "shipped".
There wasn't a huge amount of SQL so we created a DAO (in .NET) and went to work. After a while the transaction management got a little akward and writing SQL and parameterized strings I looked at object mapping relationship tools (ORM's) , I was familiar with the JAVA based Hibernate project and was delighted to find a port called NHibernate.
ORM's do not let you forget how databases work, you must still model/design your schema correctly, implement contraints and foreign key relationships etc, but they do free you from the tedium of reading a dataset and placing the contents in an object.
For example, here is the SQL to create our order table :
CREATE TABLE [dbo].[orders] (
[orderId] [int] IDENTITY (854, 1) NOT NULL ,
[customerId] [int] NOT NULL ,
[authorizationCode] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[orderDate] [datetime] NOT NULL ,
[shippingId] [int] NULL ,
[pnfReference] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[status] [int] NULL
) ON [PRIMARY]
The class that supports the table is :
public class Order
{
private int orderId;
private int customerId;
private string authorizationCode;
private DateTime orderDate;
private int shippingId;
private string pnfReference;
private OrderStatus status;
// assume getter/setter for each property
}
The nhibernate mapping file is :
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="totowebapp.DomainModel.Order, totowebapp" table="Orders"><id name="OrderId" column="orderId" type="Int32">
<generator class="identity" />
</id><property name="CustomerId" column="customerId" type="Int32"/>
<property name="AuthorizationCode" column="authorizationCode" type="String"/>
<property name="OrderDate" column="orderDate" type="Date"/>
<property name="ShippingId" column="shippingId" type="Int32"/>
<property name="PnfReference" column="pnfReference" type="String"/><many-to-one name="Status" class="totowebapp.DomainModel.OrderStatus, totowebapp" column="status"/>
</class>
</hibernate-mapping>
Now the code to read an order from the database into an order object :
ISession session=null;
ITransaction transaction =null;
Order order = null;
try
{
session = SessionManager.GetSession();
transaction = session.BeginTransaction();
order = (Order)session.Get(typeof(Order), orderId);
}
catch (Exception e)
{if (transaction !=null)
transaction.Rollback();
Debug.WriteLine(e);}
finally
{
session.Flush();
session.Close();
}
to write an order you change the line
order = (Order)session.Get(typeof(Order), orderId);
to
session.Save(order);
Finders are also easy, for example to find ordersItems for a particular order :
string tmp = "from totowebapp.DomainModel.OrderItem as oi where oi.OrderId = {0}";
string query = String.Format(tmp, orderId);
IList orderItems = session.Find(query);
An IList can be bound to a Datagrid which makes the UI really simple too.
At some point I'm going to do a real tutorial on NHibernate as I did think it was time consuming to find the correct syntax for Critera and Query objects and this is really due to it being a port from Hibernate and the docs for NHibernate not being up to snuff yet.
I think NHibernate will chop about 30% off most of my database related projects in development time, it looks like a similar technology will replace the current Entity EJB spec when EJB 3 is released, I found a short video that does talk a little about it.
May 19, 2005 at 02:41 PM in Web/Tech | Permalink | Comments (0)
I'm pretty impressed by neoswiff it allows a developer to write gui code in C# and have it compile to a flash player compatible .swf file. They have a simple demo with code , the first simple example has the C# code linked for viewing, the more sophisticated examples require you to download the visual studio plugin or the standalone ide. After you've installed the examples should be under C:\Program Files\GlobFX\NeoSwiff\samples, thanks to Darron Schall for pointing that out.
May 19, 2005 at 01:44 PM in Flex / Air / Flash | Permalink | Comments (2)
I think I'm going to prototype some stuff for my client Norfolk Southern in the ARP framework. We are building a very sophisticated role based authorization tool, it has users, groups, resources, permissions and constraints so you should be able to ask the question "can jim, modify orders over $10,000 on a Tuesday at 2:00pm if he is located in Atlanta or Florida and connected over a wireless device" and the system will say yes or no.
The administration of this is huge, probably 2000 hours of work by the time is 100% complete but we are doing multiple releases.
I've looked into the follow ways of tackling this beast and then my conclusions
I'll be posting some prototypes as I manage to get them done.
May 19, 2005 at 01:05 PM in Flex / Air / Flash | Permalink | Comments (0)