Two applications of AOP (second)

Source: Internet
Author: User
Tags aop bool empty tostring

Two applications of AOP: Entity set Update (dateentitylistupdate), deferred loading (lazyload) (bottom)

Lazyloadablesink class

//*******************************************************************
Module: Implementing deferred Load Message sinks
Date: 2009-9-19 14:08:58
Author: Faib
Copyrights: Copyright Faib Studio 2009
Official website: http://www.faib.net.cn
Email: faib920@126.com
Note:
//*******************************************************************
Using System;
Using System.Reflection;
Using System.Runtime.Remoting.Messaging;
Using FaibClass.Data.Operation;
Namespace FaibClass.Data.Aspect
{
<summary>
Implements a deferred load message sink.
</summary>
Internal class Lazyloadablesink:imessagesink
{
Private IMessageSink M_nextsink;
Private MarshalByRefObject M_target;
private static Object syncRoot = new Object ();
Public Lazyloadablesink (MarshalByRefObject target, IMessageSink nextsink)
{
Lock (SyncRoot)
{
M_target = target;
M_nextsink = Nextsink;
}
}
Public IMessage SyncProcessMessage (IMessage msg)
{
IMethodReturnMessage Returnedmessage;
Handlemessage (msg, FALSE, out returnedmessage);
return returnedmessage;
}
Public Imessagectrl asyncprocessmessage (IMessage msg, IMessageSink Replysink)
{
IMethodReturnMessage Returnedmessage;
Handlemessage (MSG, True, out returnedmessage);
Return M_nextsink.asyncprocessmessage (msg, replysink);
}
Public IMessageSink Nextsink
{
get {return m_nextsink;}
}
<summary>
Process messages
</summary>
<param name= "MSG" ></param>
<param name= "IsAsync" ></param>
<param name= "Returnedmessage" ></param>
private void Handlemessage (IMessage msg, bool IsAsync, out IMethodReturnMessage returnedmessage)
{
Returnedmessage = null;
if (! IsAsync)
{
if (msg is IMethodCallMessage)
{
Type EntityType = M_target. GetType ();
IMethodCallMessage MCM = (imethodcallmessage) msg;
BOOL Isfill = (bool) entitytype.getproperty ("Innerisfill", BindingFlags.Instance | BindingFlags.NonPublic). GetValue (m_target, NULL);
To determine whether to get a property or field or call the GetValue method
BOOL Isproperty =!isfill && (MCM. Methodname.length > 4 &&
McM. Methodname.substring (0, 4) = = "get_" && MCM. MethodName!= "Get_innerisfill" &&
McM. MethodName!= "Get_innerdatastate") | | McM. MethodName = = "Fieldgetter" | |
McM. MethodName = = "GetValue");
Object oldvalue = null;
MemberInfo minfo = null;
Property Name Field Name
String propertyname = String. Empty;
if (Isproperty)
{
Field
if (MCM). MethodName = = "Fieldgetter")
PropertyName = MCM. INARGS[1]. ToString ();
GetValue method
else if (MCM. MethodName = = "GetValue")
PropertyName = MCM. Inargs[0]. ToString ();
Property
Else
PropertyName = MCM. Methodname.replace ("get_", "");
Minfo = Utility.getmember (m_target. GetType (), PropertyName);
if (Minfo = null)
Isproperty = false;
Determines whether a child entity set, reference entity, reference property
else if (!minfo. IsDefined (typeof (Subentitylistattribute), true) &&
!minfo. IsDefined (typeof (Referenceentityattribute), true) &&
!minfo. IsDefined (typeof (Referencepropertyattribute), true)
Isproperty = false;
if (Isproperty)
OldValue = Utility.getmembervalue (M_target, Minfo, PropertyName);
Read the database when the value is empty
if (isproperty && oldvalue = null)
{
Remove the cached Action object
String key = Datacachekeymanager.getentityinnerdata (EntityType);
Object innerdata = Innercache<object[]>. Get (key);
if (innerdata!= null)
{
Construct the Action object
Object[] _innerdata = (object[]) innerdata;
Datahelper data = (datahelper) activator.createinstance ((Type) _innerdata[0]);
if (string. IsNullOrEmpty (data. ConnectionString))
Data. ConnectionString = _innerdata[1]. ToString ();
Operationargs Operargs = new Operationargs (data);
QueryBuilder query = new QueryBuilder (data. Createparameters ());
Child entity set
if (minfo. IsDefined (typeof (Subentitylistattribute), true)
{
Subentitylistattribute Keyattribute = Datamappingmanager.getsubentitylistkey (EntityType, propertyName);
if (Keyattribute!= null)
{
Idataentitylist list = (idataentitylist) activator.createinstance (Keyattribute.entitylisttype);
Type Refentitytype = list. Modeltype;
Take primary key value
Object value = ((dataentity) m_target). GetValue (Keyattribute.primarykey);
if (value!= null)
{
Association relationship
Query. Append (querycompare.equal, Keyattribute.foreignkey, value);
if (!string. IsNullOrEmpty (keyattribute.condition))
{
Query. Append (Queryrelation.and, keyattribute.condition);
}
Querying entity Sets
List = Selectoperator.select (Operargs, refentitytype, query, NULL, NULL);
Returnedmessage = new ReturnMessage (list, NULL, 0, NULL, MCM);
((dataentity) m_target). SetValue (PropertyName, Returnedmessage.returnvalue);
}
}
}
Referencing entities
else if (minfo. IsDefined (typeof (Referenceentityattribute), true)
{
Referenceentityattribute Keyattribute = Datamappingmanager.getreferenceentitykey (EntityType, propertyName);
Take primary key value
Object value = ((dataentity) m_target). GetValue (Keyattribute.foreignkey);
if (value!= null)
{
Association relationship
Query. Append (querycompare.equal, Keyattribute.primarykey, value);
Object result = Getoperator.get (Operargs, keyattribute.referencetype, query, NULL, NULL);
Returnedmessage = new ReturnMessage (result, NULL, 0, NULL, MCM);
((dataentity) m_target). SetValue (PropertyName, Returnedmessage.returnvalue);
}
}
Referencing properties
else if (minfo. IsDefined (typeof (Referencepropertyattribute), true)
{
Referencepropertyattribute Keyattribute = Datamappingmanager.getreferencepropertykey (EntityType, propertyName);
Take primary key value
Object value = ((dataentity) m_target). GetValue (Keyattribute.foreignkey);
if (value!= null)
{
Association relationship
Query. Append (querycompare.equal, Keyattribute.primarykey, value);
Dataentity entity1 = Getoperator.get (Operargs, keyattribute.referencetype, query, NULL, NULL);
if (entity1!= null)
{
Object result = Entity1. GetValue ((Keyattribute as Referencepropertyattribute). Referencepropertyname);
Returnedmessage = new ReturnMessage (result, NULL, 0, NULL, MCM);
((dataentity) m_target). SetValue ((Keyattribute as Ikeyattribute). property, result);
}
}
}
Data. Dispose ();
}
}
}
}
if (returnedmessage = null)
Returnedmessage = (imethodreturnmessage) m_nextsink.syncprocessmessage (msg);
}
Else
{
Returnedmessage = null;
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.