Skip to main content

Business Rules and Events

The platform provides business rules and events to execute customized business logic. These class provide an easy way to separate logic for a cleaner and more maintainable project. Additionally, they expose a mechanism to allow for cross-module integration of components and customization.

Business events are raised objects that inform other modules in the system of an application event. Business events are registered in the system by creating a new class that inherits from Eleflex.BusinessRuleEvent. To fire the event, an extension method was created so that you simply create the object, set any properties and call the RaiseEvent() method. This causes all business rules subscribed to this object to be executed against the object. If any business rule being processed returns with a ResponseSuccess equals false, a managed exception will be thrown that will contain a user error message from the business rule that caused the error.

Business rules are classes that contain validation or business logic that subscribe to business events. Business rules are registered in the system by creating a new class that inherits from Eleflex.BusinessRule and it must have the [Eleflex.BusinessRuleProcess] class attribute applied with the object you plan on validating. Rather than registering each business rule individually, you can use the Eleflex.BusinessRuleSet object instead with a reference to all linked business rules. This also must have the [Eleflex.BusinessRuleProcess] class attribute applied in order for the system to dynamic register it.

The Business Repository layer will raise an event with the object being operated on when the insert or the update method is called. This provides a general event that will be called before an object is saved. Assuming this succeeds, the Business Repository will call the Storage Repository next, along with its more granular events.

The Storage Repository layer will raise an event with the object for the following methods during processing:

The following example shows how to create a new business event and how to raise it to let other areas of the system know it has been fired. Note that using the RaiseEvent() extension method will throw a managed exception if any rules fail that it processes.


public partial class ExampleBusinessRuleEvent : BusinessRuleEvent
{
public virtual string Data { get; set; }
}
ExampleBusinessRuleEvent myEvent = new ExampleBusinessRuleEvent ();
myEvent.Data = “test”;
myEvent.RaiseEvent();


The following example demonstrates how to create a class that subscribes to ExampleBusinessRuleEvent being fired, from the above example.


 [BusinessRuleProcessAttribute(typeof(ExampleBusinessRuleEvent))]
public partial class ExampleBusinessRule : BusinessRule
{
	public ExampleBusinessRule()
{
ErrorMessage = “Data cannot be null”;
}
       public override IResponse Execute(IRequestItem<IContext> request)
       {
		IResponse response = base.Execute(request);
		ExampleBusinessRuleEvent exampleEvent = (ExampleBusinessRuleEvent) request.Item.Item;
		If(exampleEvent.Data == null)
			Response.AddMessage(true,this.ErrorMessage);	
            return response;
}
}

This example demonstrates how to create a class that subscribes to the Business Repository raising an event on the SecurityUser object. This event will only be called from the Business Repository when the Insert() or Update() method is called.


[BusinessRuleProcessAttribute(typeof(SecurityUser))]
public partial class SecurityUserBusinessRule : BusinessRule
{
	public SecurityUserBusinessRule()
{
ErrorMessage = “FirstName cannot be null”;
}
       public override IResponse Execute(IRequestItem<IContext> request)
       {
		IResponse response = base.Execute(request);
		SecurityUser user = (SecurityUser) request.Item.Item;
		If(user.FirstName == null)
			Response.AddMessage(true,this.ErrorMessage);	
            return response;
}
}

The next example shows how to create a business rule that will subscribe to the Storage Repository Insert business event.

    
[BusinessRuleProcessAttribute(typeof(RepositoryInsertEvent<SecurityUser>>))]
public partial class ExampleUserBusinessRule : BusinessRule
{
	public ExampleUserBusinessRule()
{
ErrorMessage = “First name cannot be system”;
}
       public override IResponse Execute(IRequestItem<IContext> request)
       {
		IResponse response = base.Execute(request);
		RepositoryInsertEvent<SecurityUser> insertEvent = (RepositoryInsertEvent<SecurityUser>)request.Item.Item;
		If(insertEvent.FirstName == “system”)
			Response.AddMessage(true,this.ErrorMessage);	
            return response;
}
}