BizRules / Referencing Custom Assemblies

Mar 30, 2010 at 9:48 PM
Edited Mar 30, 2010 at 9:50 PM

Hi Andrea,

First off I'd like to say Grazie mille for your great work.

The possibility to add BizRules to an item is going to become a life saver in an upcoming project.

I was wondering though, is it possible to reference a custom class in the BizRule code. I.e. can I do the following:

 

 

using System;
using System.Security.Principal;
using System.IO;
using System.Data;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using NetSqlAzMan;
using NetSqlAzMan.Interfaces;

>>> using UncleZen.Biz

namespace My_Application.BizRules
{
public sealed class MyBizRule : IAzManBizRule
{
public MyBizRule()
{ }

public bool Execute(Hashtable contextParameters, IAzManSid identity, IAzManItem ownerItem, ref AuthorizationType authorizationType)
{
//Insert your code here //Assign authorizationType to some AuthorizationType value to force CheckAccess result for this item. int articleID = (int) contextParameters["artID"];

>>> ArticleMananger manager = ArticleMananger.CreateManager(articleID);



if (manager.CanEdit(identity.StringValue))
{
authorizationType = AuthorizationType.Allow;
}
else { authorizationType = AuthorizationType.Deny; } return true;
}
}
}

 

 

Thanks in advance!

Zen

Coordinator
Apr 1, 2010 at 6:58 AM

Hi,

yes you can, using Assembly runtime loading.

Look at the following example:

Define a new test assembly FooTest.dll like this:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace FooTest

{

    public class MyFoo

    {

        public void SayHello()

        {

            MessageBox.Show("Hello !");

        }

    }

}

Define a new Biz Rule like this:

using System;

using System.Security.Principal;

using System.IO;

using System.Data;

using System.Collections;

using System.Collections.Specialized;

using System.Collections.Generic;

using System.Text;

using NetSqlAzMan;

using NetSqlAzMan.Interfaces;

using System.Reflection;

namespace Test.BizRules

{

    public sealed class BizRule : IAzManBizRule

    {

        public BizRule()

        { }

        public bool Execute(Hashtable contextParameters, IAzManSid identity, IAzManItem ownerItem, ref AuthorizationType authorizationType)

        {

            //Insert your code here

            //Assign authorizationType to some AuthorizationType value to force CheckAccess result for this item.

            Assembly a = Assembly.LoadFile(@"YourAssemblyPath\FooTest.dll");

            System.Type myFooType = a.GetType("FooTest.MyFoo");

            Object myFooInstance = Activator.CreateInstance(myFooType);

            myFooInstance.GetType().InvokeMember("SayHello", BindingFlags.Default | BindingFlags.InvokeMethod, null, myFooInstance, null);

            return true;

        }

    }

}

And all works fine.

Regards,

Andrea.

__________________________________
Andrea Ferendeles
NetSqlAzMan Project Coordinator  
E-mail aferende@hotmail.com Web http://netsqlazman.codeplex.com

Apr 1, 2010 at 12:40 PM

Thank you very much!

Zen