Hi everybody, continuing the previous post. Today I want to give them a very simple concept of using CSLA .NET in a Windows Forms project.

First, in the DemoCSLA.Datos project we will create a partial class that allows us to create some static methods that we can then use throughout the project:

We will put you name Cliente.Designer.cs and declare two methods:

GetClienteByID

public static Cliente GetClienteByID(int ID)
		{
			using (var ctx = new DemoCSLAContainer())
			{
				var customer = ctx.ClienteSet.SingleOrDefault(c => c.ID == ID);
 
				if (customer != null)
					return customer;
 
				throw new InvalidOperationException("No se encuentra el Cliente con el ID especificado");
			}
		}

GetAllClientes

public static IEnumerable GetAllClientes()
		{
 
			using (var ctx = new DemoCSLAContainer())
			{
				return ctx.ClienteSet.ToList();
			}
 
		}

We solve the references and compile.

Then, we must make reference through NuGet the following packages in the project DemoCSLA.Negocio:

Snap6

As you can see CSLA, brings different templates that you can use throughout the project, which once installed do not require to download again on all projects to use, that will discuss below.

We create two folders in the project DemoCSLA.Negocio:

Snap7

Within Editables, we’ll create a class called CustomerEdit and place the following code:

[Serializable]
	public class CustomerEdit : BusinessBase
	{
 
		#region Properties
		public static readonly PropertyInfo IDProperty = RegisterProperty(c => c.ID);
		public int ID
		{
			get { return GetProperty(IDProperty); }
			set { SetProperty(IDProperty, value); }
		}
 
		public static readonly PropertyInfo NameProperty = RegisterProperty(c => c.Name);
		public string Name
		{
			get { return GetProperty(NameProperty); }
			set { SetProperty(NameProperty, value); }
		}
 
		public static readonly PropertyInfo RUCProperty = RegisterProperty(c => c.RUC);
		public string RUC
		{
			get { return GetProperty(RUCProperty); }
			set { SetProperty(RUCProperty, value); }
		}
 
		public static readonly PropertyInfo PhoneProperty = RegisterProperty(c => c.Phone);
		public string Phone
		{
			get { return GetProperty(PhoneProperty); }
			set { SetProperty(PhoneProperty, value); }
		}
 
		public static readonly PropertyInfo EmailProperty = RegisterProperty(c => c.Email);
		public string Email
		{
			get { return GetProperty(EmailProperty); }
			set { SetProperty(EmailProperty, value); }
		}
		#endregion
 
		#region Business Rules
		protected override void AddBusinessRules()
		{
			base.AddBusinessRules();
		}
		#endregion
 
		#region Factory Methods
 
		public static CustomerEdit NewCustomerEdit()
		{
			return DataPortal.Create();
		}
 
		public static CustomerEdit GetCustomerEdit(int id)
		{
			return DataPortal.Fetch(id);
		}
 
		public static void DeleteCustomerEdit(int id)
		{
			DataPortal.Delete(id);
		}
		#endregion
 
		#region Data Access
		private void DataPortal_Fetch(int id)
		{
			var customer = Cliente.GetClienteByID(id);
 
			using (BypassPropertyChecks)
			{
				ID = customer.ID;
				Name = customer.RazonSocial;
				RUC = customer.RUC;
				Phone = customer.Telefono;
				Email = customer.Correo;
			}
			BusinessRules.CheckRules();
		}
 
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Insert()
		{
			using (var ctx = DbContextManager.GetManager())
			{
				var customer = new Cliente();
 
				DoInsertUpdate(customer);
 
				ctx.DbContext.ClienteSet.Add(customer);
				ctx.DbContext.SaveChanges();
			}
		}
 
		private void DoInsertUpdate(Cliente customer)
		{
			using (BypassPropertyChecks)
			{
				customer.ID = ID;
				customer.RazonSocial = Name;
				customer.RUC = RUC;
				customer.Telefono = Phone;
				customer.Correo = Email;
			}
		}
 
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Update()
		{
			using (var ctx = DbContextManager.GetManager())
			{
				var customer = Cliente.GetClienteByID(ID);
 
				DoInsertUpdate(customer);
 
				ctx.DbContext.SaveChanges();
			}
		}
 
		protected override void DataPortal_DeleteSelf()
		{
			DataPortal_Delete(ReadProperty(IDProperty));
		}
 
		[Transactional(TransactionalTypes.TransactionScope)]
		private void DataPortal_Delete(int id)
		{
			using (var ctx = DbContextManager.GetManager())
			{
				var customer = Cliente.GetClienteByID(ID);
 
				ctx.DbContext.ClienteSet.Remove(customer);
				ctx.DbContext.SaveChanges();
			}
 
		}
		#endregion
 
	}

It installs the templates that download CSLA through NuGet us a series of Snippets that you can use while we encrypt:

Snap8

As you can see, the properties of CSLA .NET have a special syntax, this is because each time it reads or writes to a property, CSLA, performs a validation level property to check whether the user has access, if so then it fires an event that informs the class that the contents of the property has changed.

Observe that all kinds of editable objects must inherit from BusinessBase , where T is the class CustomerEdit, and in addition they must have the Serializable attribute, since objects “travel” through the DataPortal.

Then create two classes within the ReadOnly folder:

Snap9

These two classes represent a list of customers, but inheriting classes of CSLA, since its purpose is only reading, in this way we do not use a list of CustomerEdit, because if we would not be using an editing object to display it in a grid you and that would not be as optimal.

Once finished, it is to compile the code and we went to the DemoCSLA.Windows project and through NuGet are also using CSLA Core package.

Snap2

As this is only a first example will place a single form, the maintenance of customer registration, so I used the following design:

Snap10

As you can see, it’s something really simple, the code does not show it because but I would do this very long post, at the end of it you can download it.

Since each business object that we created, they are linkable, we can add to the data sources window:

Snap1

Snap20

These objects are dragged to the design of the form and already it would be as the previous design. If we test the application you can see that the operations are simple to handle and virtually the Windows Forms code is minimal.

If we wanted to add a required field validation control would suffice the assembled System.ComponentModel.DataAnnotations add to the DemoCSLA.Negocio project and add it to each property.

Snap4

In this way, only compiles the entire project, adds an ErrorProvider control to the form, and sets its DataSource property with the BindingSource which contains the editable object, nothing is changed in the code.

Snap5

And ready.

Snap11

As you can see, CSLA .NET enables us to streamline the process of developing business applications for Windows, in the next post, we will see the full application.

ver-github1

I hope you like.

Share and comment.

Anuncios