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.
DisposeScopeTest.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
7using Csla.TestHelpers;
9using Microsoft.VisualStudio.TestTools.UnitTesting;
10
12{
13 [TestClass]
14 public class DisposeScopeTest
15 {
16
17 [TestMethod]
19 {
20 // CSLA should not dispose of the default service provider.
21 IServiceCollection serviceCollection = new ServiceCollection();
22 serviceCollection.AddScoped<DisposableClass>();
23 serviceCollection.AddCsla();
24
25 var services = serviceCollection.BuildServiceProvider();
26 IDataPortal<ClassA> dataPortal = services.GetRequiredService<IDataPortal<ClassA>>();
27
28 var classA = dataPortal.Fetch();
29 var classB = classA.ChildB;
30
31 Assert.AreEqual(classA.DisposableClass.Id, classB.DisposableClass.Id, "Ids must be the same");
32 Assert.IsFalse(classA.DisposableClass.IsDisposed, "Object must not be disposed");
33 }
34
35 }
36
37 public class DisposableClass
38 : IDisposable
39 {
40 public Guid Id { get; } = Guid.NewGuid();
41 public bool IsDisposed { get; private set; } = false;
42 public void Dispose()
43 {
44 IsDisposed = true;
45 }
46 }
47
48 public class ClassA : BusinessBase<ClassA>
49 {
50 public ClassB ChildB { get; set; }
51 public DisposableClass DisposableClass { get; set; }
52
53 [Fetch]
54 private void Fetch([Inject]DisposableClass disposable, [Inject] IDataPortal<ClassB> classBDataPortal)
55 {
56 DisposableClass = disposable;
57
58 if (disposable.IsDisposed)
59 {
60 throw new ObjectDisposedException(nameof(disposable));
61 }
62
63 ChildB = classBDataPortal.Fetch();
64
65 if (disposable.IsDisposed)
66 {
67 throw new ObjectDisposedException(nameof(disposable));
68 }
69 }
70 }
71
72 public class ClassB : BusinessBase<ClassB>
73 {
74 public DisposableClass DisposableClass { get; set; }
75 public Guid Id { get; set; }
76
77 [Fetch]
78 private void Fetch([Inject]DisposableClass disposable)
79 {
80 DisposableClass = disposable;
81
82 if (disposable.IsDisposed)
83 {
84 throw new ObjectDisposedException(nameof(disposable));
85 }
86 }
87 }
88}
This is the base class from which most business objects will be derived.
Definition: BusinessBase.cs:38
Interface defining the members of the data portal type.
Definition: IDataPortalT.cs:17
object Fetch(params object[] criteria)
Called by a factory method in a business class to retrieve an object, which is loaded with values fro...