c#


Object can't exist without and outside another object [closed]


I want to have a program in C# that object A can't be created without object B and also when object B is destroyed, object A would be demolished automatically. so existence of object A would be completely dependent on existence of Object B.
I am aware that it is possible to force A inside B's contractor, however I want to make it impossible to create A outside B in any part of the application. maybe a factory pattern? I am not sure how to limit other developers to create a class, or create it with certain criteria.
I want to have a program in C# that object A can't be created without object B
that part is easy - just make A have a constructor that takes B.
and also when object B is destroyed, object A would be demolished automatically.
that part is not so trivial; in particular, you don't control garbage collection, nor do you control what references an A instance, so the most you can do is make B dispose A.
Indeed, if an A depends on a B, then A probably has a reference to a B. That means that if A is reachable (not garbage), then neither is B (because it can be reached via A), thus B wouldn't be collected in the first place. And if B is being destroyed, then we already know that the A is toast, by the same reasoning. From a certain perspective, this actually achieves everything you wanted.
The only sensible time to actually do something here (rather than just relying on the reachable graph) is in disposal of B. So at that point you've got something like:
class A : IDisposable {
B _b;
A(B b) { b.Register(this); _b = b; }
void IDisposable.Dispose() { _b = null; }
void CheckDisposed() { if(_b == null) throw new ObjectDisposedException(nameof(A)); }
public void ActualMethod() {
CheckDisposed();
// do stuff
}
}
class B : IDisposable {
A _a;
internal void Register(A a) {
if(a == null) throw new ArgumentNullException(nameof(a));
if(_a != null) throw new InvalidOperationException("already has an A");
_a = a;
}
void IDisposable.Dispose() {
var tmp = _a;
_a = null;
tmp?.Dispose();
}
}
However, at this point the types are so inter-dependent that I question the purpose of having two types. There are ways to loosen this dependency and allow 0-* mapping, but it is such an odd requirement that I don't see the purpose of it.

Related Links

How do I serve a static page as the default action of a MVC application? [closed]
How to implement comet or reverse ajax on MVC application Razor view [closed]
What is my mistake in implementing Foreign Key with EF 6?
Is it necessary to mock all and every dependency in Unit Tests?
EF Code First many-to-many: how to specify name of th third table and add some other properties to it?
BackgroundTaskRegistration internal exception in wp8.1
How do you ignore Whitespace when using CsvHelper, CsvReader.Read()?
WepApi Route and RoutePrefix Confusion
Could not load file or assembly, system could not find the file specified
DataTemplate is not drawn, wpf
How to navigate from ContentPage to ContentPage?
using foreach and multiple insert rows?
ASP.NET: The server tag is not well formed
Cannot find Front, Back Camera in windows 8.1 store app
Populate a ComboBox on MainForm via Logic.cs
Dependency Property on a usercontrol that is bound to another VM

Categories

HOME
asp.net
pact
http
twitter-bootstrap
intellij-idea
postsharp
x86-64
assign
redhawksdr
hiveql
psexec
flowtype
python-3.4
cracking
rename
velocity
ng2-charts
swift3.1
civicrm
computation-theory
vala
procedural-generation
pypy
sphinx4
edirectory
foxpro
lines
multilanguage
ng2-bootstrap
powerbi-embedded
tunnel
dom4j
motherboard
android-databinding
pygraphviz
iterm2
jslider
vertical-alignment
ape-phylo
vertex
dynamic-linking
critical-section
zedgraph
tsc
msiexec
sqlproj
qudpsocket
android-tabs
tinybox2
system-integration
avi
laterjs
chomsky-normal-form
methodology
instabug
google-app-engine-php
dynamics-nav
wiql
tvp
notice
vspackage
sicstus-prolog
skview
google-experiments
fiware-health
os.walk
time-frequency
visual-c#-express-2010
rssi
new-operator
acrofields
node-amqp
grunt-wiredep
mosix
nsautoreleasepool
pageload
freeimage
correctness
netbiscuits
nsscrollview
bash4
helicontech
ruby-1.9
datejs
visual-leak-detector
designated-initializer
response-time
azure-appfabric
miniport
moores-law

Resources

Encrypt Message