CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
DataServiceContextManager.cs
Go to the documentation of this file.
1#if !NETSTANDARD2_0 && !NET5_0 && !NET6_0
2//-----------------------------------------------------------------------
3// <copyright file="DataServiceContextManager.cs" company="Marimer LLC">
4// Copyright (c) Marimer LLC. All rights reserved.
5// Website: https://cslanet.com
6// </copyright>
7// <summary>Provides an automated way to reuse </summary>
8//-----------------------------------------------------------------------
9using System;
10using System.Collections.Generic;
11
12namespace Csla.Data
13{
22 [Obsolete("Use dependency injection", false)]
23 public class DataServiceContextManager<C> : Core.IUseApplicationContext
24 where C : System.Data.Services.Client.DataServiceContext
25 {
26
27 private static object _lock = new object();
28 private C _context;
29
30 ApplicationContext Core.IUseApplicationContext.ApplicationContext { get => ApplicationContext; set => ApplicationContext = value; }
31 private ApplicationContext ApplicationContext { get; set; }
32
41 {
42
43 lock (_lock)
44 {
45 var contextLabel = GetContextName(path.ToString());
47 if (ApplicationContext.LocalContext.Contains(contextLabel))
48 {
50 }
51 else
52 {
53 mgr = new DataServiceContextManager<C>(path);
54 ApplicationContext.LocalContext[contextLabel] = mgr;
55 }
56 return mgr;
57 }
58 }
59
60 private DataServiceContextManager(Uri path)
61 {
62 _context = (C)(ApplicationContext.CreateInstanceDI(typeof(C), path));
63 }
64
69 {
70 get
71 {
72 return _context;
73 }
74 }
75
76 private static string GetContextName(string path)
77 {
78 return "__ctx:" + path;
79 }
80
89 public List<T> GetEntities<T>()
90 {
91 List<T> returnValue = new List<T>();
92 foreach (var oneEntityDescriptor in _context.Entities)
93 {
94 if (oneEntityDescriptor.Entity is T)
95 {
96 returnValue.Add((T)oneEntityDescriptor.Entity);
97 }
98 }
99 return returnValue;
100 }
101
114 public T GetEntity<T>(string keyPropertyName, object keyPropertyValue)
115 {
116 T returnValue = default(T);
117 foreach (T oneEntity in GetEntities<T>())
118 {
119 if (keyPropertyValue.Equals(Csla.Reflection.MethodCaller.CallPropertyGetter(oneEntity, keyPropertyName)))
120 {
121 returnValue = oneEntity;
122 break;
123 }
124 }
125 return returnValue;
126 }
127 }
128}
129#endif
Provides consistent context information between the client and server DataPortal objects.
object CreateInstanceDI(Type objectType, params object[] parameters)
Creates an object using 'Activator.CreateInstance' using service provider (if one is available) to po...
ApplicationContext(ApplicationContextAccessor applicationContextAccessor)
Creates a new instance of the type
ContextDictionary LocalContext
Returns the application-specific context data that is local to the current AppDomain.
Provides an automated way to reuse an ADO.NET Data Services context object within the context of a si...
DataServiceContextManager< C > GetManager(Uri path)
Gets the DataServiceContext object for the specified URI.
T GetEntity< T >(string keyPropertyName, object keyPropertyValue)
Gets a list of the entities by key.
C DataServiceContext
Gets the DataServiceContext object.
List< T > GetEntities< T >()
Gets a list of the entities of the specified type from the context.