2016-01-20 13 views
6

के डेटा स्रोत के रूप में गतिशील रूप से जेनरेट की गई वस्तु का उपयोग कैसे करें हम इस घटक www.codeeffects.com का उपयोग कर रहे हैं जो हमें ऑब्जेक्ट गुणों के आधार पर व्यवसाय नियम बनाने की अनुमति देता है।कोडइफेक्ट जनरेटर

देखने के HTML इस तरह है:

@{ 
    ViewBag.Title = "Post Example"; 

    Html.CodeEffects().Styles() 
     .SetTheme(ThemeType.Gray) 
     .Render(); 
} 
@using (Html.BeginForm("Evaluate", "Post", FormMethod.Post)) 
{ 
    <div class="area"> 
     <h1 class="title">Post Example</h1> 
     <div style="margin-top:10px;"> 
      <span>Info:</span> 
      <span style="color:Red;">@ViewBag.Message</span> 
     </div> 
     <div style="margin-top:10px;"> 
      @{ 
    Html.CodeEffects().RuleEditor() 
     .Id("ruleEditor") 
     .SaveAction("Save", "Post") 
     .DeleteAction("Delete", "Post") 
     .LoadAction("Load", "Post") 
     .Mode(RuleType.Execution) 
     .ContextMenuRules(ViewBag.ContextMenuRules) 
     .ToolBarRules(ViewBag.ToolBarRules) 
     .Rule(ViewBag.Rule) 
     .Render(); 
      } 
     </div> 
    </div> 
    <div class="area"> 
     <h1 class="title" style="margin-top:20px;">Rule Test Form</h1> 
     @{ 
    Html.RenderPartial("_PatientForm"); 
     } 
    </div> 
} 
@{ 
    Html.CodeEffects().Scripts().Render(); 
} 

नियंत्रक में सूचकांक कार्रवाई इस तरह है:

[HttpGet] 
     public ActionResult Index() 
     { 
      ViewBag.Rule = RuleModel.Create(typeof(Patient)); 
      return View(); 
     } 

वर्ग रोगी इस तरह है:

// External methods and actions 
    [ExternalMethod(typeof(PatientService), "IsToday")] 
    [ExternalAction(typeof(PatientService), "RequestInfo")] 

    // Dynamic Menu Data Sources; details can be found at 
    // http://codeeffects.com/Doc/Business-Rules-Dynamic-Menu-Data-Sources 

    // The getEducationTypes() client-side function declared in /Views/Shared/_Layout.cshtml 
    [Data("Education", "getEducationTypes")] 
    // The List() method declared by the Physician class 
    [Data("Physicians", typeof(Physician), "List")] 
    public class Patient 
    { 
     // C-tor 
     public Patient() 
     { 
      this.ID = Guid.Empty; 
      this.Gender = Gender.Unknown; 
     } 

     // This property will not appear in the Rule Editor - Code Effects component ignores Guids. 
     // Details at http://codeeffects.com/Doc/Business-Rules-Data-Types 
     public Guid ID { get; set; } 

     [Field(DisplayName = "First Name", Description = "Patient's first name", Max = 30)] 
     public string FirstName { get; set; } 

     [Field(DisplayName = "Last Name", Max = 30, Description = "Patient's last name")] 
     public string LastName { get; set; } 

     [Field(DisplayName = "Email Address", ValueInputType = ValueInputType.User, Max = 150, Description = "Email address of the patient")] 
     public string Email { get; set; } 

     [Field(DisplayName = "Date of Birth", DateTimeFormat = "MMM dd, yyyy")] 
     public DateTime? DOB { get; set; } 

     [Field(ValueInputType = ValueInputType.User, Description = "Patient's gender")] 
     public Gender Gender { get; set; } 

     // This field uses the "Physicians" dynamic menu source (declared at class level) 
     [Field(DisplayName = "Physician", DataSourceName = "Physicians", Description = "Patient's primary physician")] 
     public int PhysicianID { get; set; } 

     // This field uses the "Education" client-side dynamic menu source (declared at class level) 
     [Field(DisplayName = "Education", DataSourceName = "Education", Description = "Patient's education level")] 
     public int EducationTypeID { get; set; } 

     [Field(Min = 0, Max = 200, Description = "Current pulse")] 
     public int? Pulse { get; set; } 

     [Field(Min = 0, Max = 200, DisplayName = "Systolic Pressure", Description = "Current systolic pressure")] 
     public int? SystolicPressure { get; set; } 

     [Field(Min = 0, Max = 200, DisplayName = "Diastolic Pressure", Description = "Current Diastolic pressure")] 
     public int? DiastolicPressure { get; set; } 

     [Field(Min = 0, Max = 110, Description = "Current temperature")] 
     public decimal? Temperature { get; set; } 

     [Field(DisplayName = "Headaches Box", Description = "Does the patient have frequent headaches?")] 
     public bool Headaches { get; set; } 

     [Field(DisplayName = "Allergies Box", Description = "Any allergies?")] 
     public bool Allergies { get; set; } 

     [Field(DisplayName = "Tobacco Box", Description = "Does the patient smoke?")] 
     public bool Tobacco { get; set; } 

     [Field(DisplayName = "Alcohol Box", Description = "Alcohol use?")] 
     public bool Alcohol { get; set; } 

     public Address Home { get; set; } 
     public Address Work { get; set; } 

     // This property is used to display outputs of rule actions 
     [ExcludeFromEvaluation] 
     public string Output { get; set; } 

     [Method("Full Name", "Joins together patient's first and last names")] 
     public string FullName() 
     { 
      return string.Format("{0} {1}", this.FirstName, this.LastName); 
     } 

     // Empty overload of the Register method. 
     // No Method attribute is needed here because its 
     // display name is the same as its declared name. 
     [Action(Description = "Registers new patient")] 
     public void Register() 
     { 
      this.Output = "The patient has been registered"; 
     } 

     // Overload of the Register method that takes one param. 
     // Both overloads can be used in Code Effects as two different actions 
     // as long as their display names are different. 
     [Action("Register with a Message", "Registers new patient with additional info")] 
     public void Register([Parameter(ValueInputType.User, Description = "Output message")] string message) 
     { 
      this.Output = message; 
     } 
    } 

हालांकि हम प्रतिबिंब का उपयोग करके निर्मित एक डाइनैमिक ऑब्जेक्ट बनाना चाहते हैं, मैंने जिस विधि को बनाया है वह यह है:

private static object CreateOurNewObject() 
     { 
      string _xml = "<root>" + 
       "<column name=\"Name\">Miron</column>" + 
       "<column name=\"LastName\">Abramson</column>" + 
       "<column name=\"Blog\">www.blog.mironabramson.com</column>" + 
       "</root>"; 

      XmlDocument xmlDoc = new XmlDocument(); 
      xmlDoc.LoadXml(_xml); 

      // create a dynamic assembly and module 
      AssemblyName assemblyName = new AssemblyName(); 
      assemblyName.Name = "tmpAssembly"; 
      System.Reflection.Emit.AssemblyBuilder assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); 
      ModuleBuilder module = assemblyBuilder.DefineDynamicModule("tmpModule"); 

      // create a new type builder 
      TypeBuilder typeBuilder = module.DefineType("BindableRowCellCollection", TypeAttributes.Public | TypeAttributes.Class); 

      // Loop over the attributes that will be used as the properties names in out new type 
      foreach (XmlNode node in xmlDoc.SelectSingleNode("root").ChildNodes) 
      { 
       string propertyName = node.Attributes["name"].Value; 

       // Generate a private field 
       FieldBuilder field = typeBuilder.DefineField("_" + propertyName, typeof(string), FieldAttributes.Private); 
       // Generate a public property 
       PropertyBuilder property = 
        typeBuilder.DefineProperty(propertyName, 
            PropertyAttributes.None, 
            typeof(string), 
            new Type[] { typeof(string) }); 

       // The property set and property get methods require a special set of attributes: 

       MethodAttributes GetSetAttr = 
        MethodAttributes.Public | 
        MethodAttributes.HideBySig; 

       // Define the "get" accessor method for current private field. 
       MethodBuilder currGetPropMthdBldr = 
        typeBuilder.DefineMethod("get_value", 
               GetSetAttr, 
               typeof(string), 
               Type.EmptyTypes); 

       // Intermediate Language stuff... 
       ILGenerator currGetIL = currGetPropMthdBldr.GetILGenerator(); 
       currGetIL.Emit(OpCodes.Ldarg_0); 
       currGetIL.Emit(OpCodes.Ldfld, field); 
       currGetIL.Emit(OpCodes.Ret); 

       // Define the "set" accessor method for current private field. 
       MethodBuilder currSetPropMthdBldr = 
        typeBuilder.DefineMethod("set_value", 
               GetSetAttr, 
               null, 
               new Type[] { typeof(string) }); 

       // Again some Intermediate Language stuff... 
       ILGenerator currSetIL = currSetPropMthdBldr.GetILGenerator(); 
       currSetIL.Emit(OpCodes.Ldarg_0); 
       currSetIL.Emit(OpCodes.Ldarg_1); 
       currSetIL.Emit(OpCodes.Stfld, field); 
       currSetIL.Emit(OpCodes.Ret); 

       // Last, we must map the two methods created above to our PropertyBuilder to 
       // their corresponding behaviors, "get" and "set" respectively. 
       property.SetGetMethod(currGetPropMthdBldr); 
       property.SetSetMethod(currSetPropMthdBldr); 
      } 

      // Generate our type 
      Type generetedType = typeBuilder.CreateType(); 

      // Now we have our type. Let's create an instance from it: 
      object generetedObject = Activator.CreateInstance(generetedType); 

      // Loop over all the generated properties, and assign the values from our XML: 
      PropertyInfo[] properties = generetedType.GetProperties(); 

      int propertiesCounter = 0; 

      // Loop over the values that we will assign to the properties 
      foreach (XmlNode node in xmlDoc.SelectSingleNode("root").ChildNodes) 
      { 
       string value = node.InnerText; 
       properties[propertiesCounter].SetValue(generetedObject, value, null); 
       propertiesCounter++; 
      } 

      //Yoopy ! Return our new genereted object. 
      return generetedObject; 
     } 

रोगी के बजाए उस ऑब्जेक्ट का उपयोग करने के लिए हम इंडेक्स एक्शन में लाइन को कैसे बदल सकते हैं? टाइपऑफ (ऑब्जेक्ट), काम नहीं करेगा।

उत्तर

1

अपने TypeBuilder का उपयोग करके, आप अपने जेनरेट किए गए प्रकार के मौजूदा या कस्टम इंटरफ़ेस पर और फिर अपने प्रकार के रूप में उपयोग करने का प्रयास कर सकते हैं।

ViewBag.Rule = RuleModel.Create(typeof(IMyInterface)); 
+0

जो काम नहीं करेगा क्योंकि मैं टाइप टाइप (mydinamobject) नहीं कर सकता। सार्वजनिक एक्शन रिसैट इंडेक्स() { आईएमआईइंटरफेस myObject = (IMyInterface) ऑब्जेक्टबिल्डर। क्रेतेऑन न्यू ऑब्जेक्ट(); ViewBag.Rule = नियम मॉडेल।बनाएँ (typeof (IMyInterface)); वापसी देखें(); } –

+0

@Didier क्या आप अपनी मूल पोस्ट को उस कोड के साथ अपडेट कर सकते हैं, जिसे आप लिखने की कोशिश कर रहे हैं, और संकलक से प्राप्त त्रुटि प्रदान करते हैं? मुझे यकीन नहीं है कि आप 'टाइपऑफ (mydinamobject)' बनाने की कोशिश कर रहे हैं? अगर मेरी पिछली पोस्ट भ्रामक थी, 'IMyInterface' वास्तविक इंटरफ़ेस नहीं है, केवल प्लेसहोल्डर है। – Chase

+0

कृपया इस प्रश्न को यहां देखें http://stackoverflow.com/questions/34929745/could-not-find-or-load-assembly-tmpassembly अपने अद्यतन कोड –

3

CreateOurNewObject से एक हिस्से को निकालें() विधि:

typeBuilder.AddInterfaceImplementation(typeof(IMyInterface)); 

अब वस्तु आपके द्वारा बनाया गया (उपरोक्त उदाहरण में) टाइप IMyInterface, जिसे फिर आप अपने Index कार्रवाई में इस्तेमाल कर सकते हैं है जो एक प्रकार के निर्माण से संबंधित है। इसे CreateType (स्ट्रिंग xml) पर कॉल करें।

AssemlbyBuilderAccess.Run को Assembly BUilderAccess.RunAndSave में बदलें। फिर, एक बार टाइप बनने के बाद, assemblyBuilder.Save() विधि को कॉल करें। इसे उस स्थान पर सहेजें जहां असेंबली.लोड लोड होगा (उदा। बिन या .NET temp फ़ोल्डर्स में से एक), या किसी अन्य स्थान को खोज पथ में इतनी देर तक।

प्रकार बनाने और वस्तुओं को तुरंत चालू करने के लिए इसका उपयोग करें।

फिर, सूचकांक में, फोन

Type myType = CreateType(xml); 
RuleModel.Create(myType); 

आप मूल्यांकन कर रहे हैं बाहर एक ही प्रकार का उपयोग सुनिश्चित करें (यह हर बार पुनर्जीवित नहीं है)। जिसका अर्थ है कि आपको इसे पहले लोड करना होगा।

Type myType = Assembly.Load(assemblyName); 
object myObject = Activator.CreateInstance(myType); 
//...populate myObject with necessary values based on your xml 
Evaluator ev = new Evaluator(myType, rule); 
bool result = ev.Evaluate(myObject); 

या आप DynamicEvaluator, जो केवल myObject.GetType कॉल इस्तेमाल कर सकते हैं()

DynamicEvaluator ev = new DynamicEvaluator(rule); 
bool result = ev.Evaluate(myObject); 

यह काम करना चाहिए। यहां महत्वपूर्ण हिस्सा यह है कि आप अपनी असेंबली को पहले सहेजते हैं (इसे इस समय स्मृति से नहीं पढ़ा जा सकता है), और यह एक फ़ोल्डर में है जो एक खोज पथ का हिस्सा है ताकि विधानसभा.लोड (नाम) इसे पा सके।

+2

शानदार! धन्यवाद। कोड प्रभाव नियमों के बारे में मेरा एक अलग सवाल था लेकिन आपके जवाब ने मुझे काम करने के लिए कुछ दिया। – Lacash