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.
DataServiceContextManager.cs
Go to the documentation of this file.
1#if !NETFX_CORE && !MONO && !(ANDROID || IOS) && !NETSTANDARD2_0 && !NET5_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 public class DataServiceContextManager<C> where C : System.Data.Services.Client.DataServiceContext
23 {
24
25 private static object _lock = new object();
26 private C _context;
27
36 {
37
38 lock (_lock)
39 {
40 var contextLabel = GetContextName(path.ToString());
42 if (ApplicationContext.LocalContext.Contains(contextLabel))
43 {
44 mgr = (DataServiceContextManager<C>)(ApplicationContext.LocalContext[contextLabel]);
45 }
46 else
47 {
48 mgr = new DataServiceContextManager<C>(path);
49 ApplicationContext.LocalContext[contextLabel] = mgr;
50 }
51 return mgr;
52 }
53 }
54
55 private DataServiceContextManager(Uri path)
56 {
57 _context = (C)(Reflection.MethodCaller.CreateInstance(typeof(C), path));
58 }
59
64 {
65 get
66 {
67 return _context;
68 }
69 }
70
71 private static string GetContextName(string path)
72 {
73 return "__ctx:" + path;
74 }
75
84 public List<T> GetEntities<T>()
85 {
86 List<T> returnValue = new List<T>();
87 foreach (var oneEntityDescriptor in _context.Entities)
88 {
89 if (oneEntityDescriptor.Entity is T)
90 {
91 returnValue.Add((T)oneEntityDescriptor.Entity);
92 }
93 }
94 return returnValue;
95 }
96
109 public T GetEntity<T>(string keyPropertyName, object keyPropertyValue)
110 {
111 T returnValue = default(T);
112 foreach (T oneEntity in GetEntities<T>())
113 {
114 if (keyPropertyValue.Equals(Csla.Reflection.MethodCaller.CallPropertyGetter(oneEntity, keyPropertyName)))
115 {
116 returnValue = oneEntity;
117 break;
118 }
119 }
120 return returnValue;
121 }
122 }
123}
124#endif
Provides an automated way to reuse an ADO.NET Data Services context object within the context of a si...
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.
static DataServiceContextManager< C > GetManager(Uri path)
Gets the DataServiceContext object for the specified URI.