CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
DataPortalProxyFactory.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="DataPortalProxyFactory.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Creates the DataPortalProxy to use for DataPortal call on the objectType.</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Linq;
11using System.Text;
12using Csla.Reflection;
13
15{
22 {
23 private static Type _proxyType;
24
30 public IDataPortalProxy Create(Type objectType)
31 {
33 {
34 if (DataPortalTypeProxyDescriptors.TryGetValue(GetTypeKey(objectType), out DataPortalProxyDescriptor descriptor))
35 {
36 var type = Type.GetType(descriptor.ProxyTypeName);
37 if (ApplicationContext.CurrentServiceProvider != null)
38 {
39 var httpClient = ApplicationContext.CurrentServiceProvider.GetService(typeof(System.Net.Http.HttpClient));
40 if (httpClient == null)
41 return (IDataPortalProxy)MethodCaller.CreateInstance(type, descriptor.DataPortalUrl);
42 else
43 return (IDataPortalProxy)MethodCaller.CreateInstance(_proxyType, httpClient, descriptor.DataPortalUrl);
44 }
45 else
46 {
47 return (IDataPortalProxy)MethodCaller.CreateInstance(type, descriptor.DataPortalUrl);
48 }
49 }
50 }
51
52 if (_proxyType == null)
53 {
54 string proxyTypeName = ApplicationContext.DataPortalProxy;
55 if (proxyTypeName == "Local")
56 _proxyType = typeof(LocalProxy);
57 else
58 _proxyType = Type.GetType(proxyTypeName, true, true);
59 }
60 var provider = ApplicationContext.CurrentServiceProvider;
61 if (provider == null)
62 return (IDataPortalProxy)MethodCaller.CreateInstance(_proxyType);
63 else
64 {
65 if (_proxyType.Equals(typeof(HttpProxy)))
66 {
67 var httpClient = ApplicationContext.CurrentServiceProvider.GetService(typeof(System.Net.Http.HttpClient));
68 if (httpClient == null)
69 return (IDataPortalProxy)MethodCaller.CreateInstance(_proxyType);
70 else
71 return (IDataPortalProxy)MethodCaller.CreateInstance(_proxyType, httpClient);
72 }
73 else
74 {
75 return (IDataPortalProxy)Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(provider, _proxyType);
76 }
77 }
78 }
79
85 public void ResetProxyType()
86 {
87 _proxyType = null;
88 }
89
105 public static Dictionary<string, DataPortalProxyDescriptor> DataPortalTypeProxyDescriptors
106 { get; private set; }
107
108 private static void InitializeDictionary()
109 {
111 DataPortalTypeProxyDescriptors = new Dictionary<string, DataPortalProxyDescriptor>();
112 }
113
119 public static void AddDescriptor(Type objectType, DataPortalProxyDescriptor descriptor)
120 {
121 InitializeDictionary();
122 DataPortalTypeProxyDescriptors.Add(GetTypeName(objectType), descriptor);
123 }
124
130 public static void AddDescriptor(string typeName, DataPortalProxyDescriptor descriptor)
131 {
132 InitializeDictionary();
133 DataPortalTypeProxyDescriptors.Add(typeName, descriptor);
134 }
135
141 public static void AddDescriptor(int resourceId, DataPortalProxyDescriptor descriptor)
142 {
143 InitializeDictionary();
144 DataPortalTypeProxyDescriptors.Add(resourceId.ToString(), descriptor);
145 }
146
152 public static string GetTypeName(Type objectType)
153 {
154 return $"{objectType.FullName}, {objectType.Assembly.FullName.Substring(0, objectType.Assembly.FullName.IndexOf(","))}";
155 }
156
163 [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
164 public static string GetTypeKey(Type objectType)
165 {
166 var attributes = objectType.GetCustomAttributes(typeof(DataPortalServerResourceAttribute), true);
167 if (attributes != null && attributes.Count() > 0)
168 return ((DataPortalServerResourceAttribute)attributes[0]).ResourceId.ToString();
169 else
170 return GetTypeName(objectType);
171 }
172 }
173}
Describes the data portal proxy for use by a specific business object type.
Default data portal proxy factory that creates the IDataPortalProxy instance to use for the DataPorta...
static void AddDescriptor(int resourceId, DataPortalProxyDescriptor descriptor)
Add a proxy descriptor for the specified root business type.
static void AddDescriptor(Type objectType, DataPortalProxyDescriptor descriptor)
Add a proxy descriptor for the specified root business type.
static Dictionary< string, DataPortalProxyDescriptor > DataPortalTypeProxyDescriptors
Gets the list of proxy-type mapping descriptors used to create specific proxy objects for specific bu...
static string GetTypeName(Type objectType)
Returns a type name formatted to act as a key in the DataPortalTypeProxyDescriptors dictionary.
static string GetTypeKey(Type objectType)
Returns the key used by the proxy factory to locate a DataPortalProxyDescriptor in the DataPortalProx...
static void AddDescriptor(string typeName, DataPortalProxyDescriptor descriptor)
Add a proxy descriptor for the specified root business type.
IDataPortalProxy Create(Type objectType)
Creates the DataPortalProxy to use for DataPortal call on the objectType.
void ResetProxyType()
Resets the data portal proxy type, so the next data portal call will reload the proxy type based on c...
Implements a data portal proxy to relay data portal calls to a remote application server by using htt...
Definition: HttpProxy.cs:27
Implements a data portal proxy to relay data portal calls to an application server hosted locally in ...
Definition: LocalProxy.cs:23
Specifies a server resource required by a business type so the data portal can route any calls to the...
Interface implemented by client-side data portal proxy objects.