Sunday, September 30, 2007

FleXtense Crud Example (Flex + .Net + Database)

Click picture for watching tutorial video




We sometimes need to reach server in the applications of flex which we have developed.There are some alternatives (HttpService,WebService etc.) to reach the server through the Flex.
In this example, we will make a crud application by using the asp.net web services and FleXtense.
The applications for this example are,
FleXtense
Flex Builder 2.0
Visual Studio 2005
Ms Sql Server

First of all, we make a database table which we use ,script of user table.

CREATE TABLE [dbo].[Users](
[ID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[Name] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Surname] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Age] [int] NULL,
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Afterwards,we make the necessary web service to work with datas on this table. There are 4 methods, GetUsers,UpdateUser,DeleteUser and AddUser in our web service. We make a class the type of User to reach datas as object oriented. Make another class of usercollection type in this class collection. Our web methods will work by these objects.

public class User
{
#region fields
private int id;
private string name;
private string surname;
private int age;
#endregion

#region C\tor
public User(int id, string name, string surname, int age)
{
this.id = id;
this.name = name;
this.surname = surname;
this.age = age;
}

public User()
{

}
#endregion

#region Properties
public int Id
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Surname
{
get { return surname; }
set { surname = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
#endregion
}


User Collection class


public class UserCollection:List<User>
{
public UserCollection()
: base()
{ }
}


The business layer that includes complex processes and entity classes which had values returned from Data Access Layer


public class UserController
{
#region GetUsers
public static UserCollection GetUsers()
{
IDataReader reader = UserData.GetUsers();
UserCollection collection = new UserCollection();
try
{
while (reader.Read())
{
User user = new User();
user.Id = reader.GetInt32(0);
if (!reader.IsDBNull(1))
user.Name = reader.GetString(1);
if (!reader.IsDBNull(2))
user.Surname = reader.GetString(2);
if (!reader.IsDBNull(3))
user.Age = reader.GetInt32(3);

collection.Add(user);
}

return collection;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (!reader.IsClosed)
reader.Close();
}
}
#endregion

#region UpdateUser
public static void UpdateUser(User user)
{
UserData.UpdateUser(user.Id, user.Name, user.Surname, user.Age);
}
#endregion

#region DeleteUser
public static void DeleteUser(int userId)
{
UserData.DeleteUser(userId);
}
#endregion

#region AddUser
public static void AddUser(string name, string surname, int age)
{
UserData.AddUser(name, surname, age);
}
public static void AddUser(User user)
{
AddUser(user.Name, user.Surname, user.Age);
}
#endregion

}


And finally, make the Userservice class in side of asp.net web service.

public class UserService : System.Web.Services.WebService
{

[WebMethod]
public UserCollection GetUsers() // There is a return value in usercollection type.
{
return UserController.GetUsers();
}
[WebMethod]
public OperationState UpdateUser(User user) //A parameter is taken from user type.
{
try
{
UserController.UpdateUser(user);
/*OperatinState is an enum which has two items are successful and unsuccessful. */

return OperationState.Successful;

}
catch
{
return OperationState.UnSuccessful;
}

}

[WebMethod]
public OperationState DeleteUser(int userId)
{
try
{
UserController.DeleteUser(userId);
return OperationState.Successful;
}
catch
{
return OperationState.UnSuccessful;
}

}

[WebMethod]
public OperationState AddUser(User user)
{
try
{
UserController.AddUser(user);
return OperationState.Successful;
}
catch
{
return OperationState.UnSuccessful;
}

}
}


You can look at the codes which have produced by Flex from here.
You can download the example with the link below
Download

Labels:

3 Comments:

At October 18, 2007 11:26 AM , Blogger Jayson said...

I got a problem here, what version of asp.net you've used? can you provide me a simple doc or toturial on how to compile it in .net

Thanks a lot,
jaschar702

 
At October 18, 2007 11:37 AM , Blogger Jayson said...

I'm having issue on this part:

UserCollection

----it says Invalid token '>' in class, struct, or interface member declaration

 
At July 2, 2008 8:37 AM , Anonymous Anonymous said...

i've downloaded the crud example and the following message has appeared in the flex project "unable to open 'd:\FleXtense\library\bin\Core.swc'"
what's Core.swc???
I've downloaded the FleXtense ActionScript Core Library from your site (flextense.core.swc) but i don't know where i'm supposed to put it. help please.
[]'s

 

Post a Comment

<< Home