Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 面向对象建模中的一对一关系_.net_Oop - Fatal编程技术网

.net 面向对象建模中的一对一关系

.net 面向对象建模中的一对一关系,.net,oop,.net,Oop,在软件系统中,一对一关系必须在两个对象之间建模,其中关系的活动状态存在 换句话说,ObjectA与ObjectB有一对一的关系(没有反向指针),其中关系可以是活动的,也可以是非活动的。还可能存在与关系关联的其他属性 在.NET中使用面向对象建模此场景的最佳方法是什么?包含ObjectB实例的ObjectA。Inactive表示为null,active表示为active。这取决于关系的激活是否是要建模的系统行为的一部分 “可能还有其他与关系关联的属性。” 所以听起来你不只是想建立一个参考模型。 包

在软件系统中,一对一关系必须在两个对象之间建模,其中关系的活动状态存在

换句话说,ObjectA与ObjectB有一对一的关系(没有反向指针),其中关系可以是活动的,也可以是非活动的。还可能存在与关系关联的其他属性


在.NET中使用面向对象建模此场景的最佳方法是什么?

包含ObjectB实例的ObjectA。Inactive表示为null,active表示为active。

这取决于关系的激活是否是要建模的系统行为的一部分

“可能还有其他与关系关联的属性。”

所以听起来你不只是想建立一个参考模型。 包含a的b实例将在简单的情况下工作,其中关系不是系统行为的一部分,而只是技术实现

如果要激活/停用/激活(不知道/记住已激活的objectB),则“空建模”将不起作用(因为您将丢失引用,并且将丢失有关关系的信息)。或者,例如,在稍后的阶段,您希望重新激活关系,而不同时引用a和b,而是引用关系。 因此,将这种关系视为模型中的一等公民

然后,您可以这样做(但这可能是完全过分的,只是为了显示作为域一部分的关系建模):


(来源:)

objectA使用一个ObjectB代理,该代理将充当ObjectB,但它实际上引用了到ObjectB的单向关系。 ObjectB代理可以实现在关系未激活时执行的操作

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ActivatableRelationshipExample
{
    public interface IActivatable
    {
        void Activate();
        void Deactivate();
        bool IsActive { get; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ActivatableRelationshipExample
{
    public interface IRelationShip<TSource,TDestination> : IActivatable
    {
        TDestination Destination { get; }
        TSource Source { get; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ActivatableRelationshipExample
{
    public class ObjectA
    {
        // you could also have an ObjectB reference here
        // and fill it with an ObjectBProxy instance
        // in the constructor. thought this would
        // explain the example a bit better
        private readonly ObjectBProxy proxy;

        public ObjectA(ObjectB b)
        {
            proxy = new ObjectBProxy(b);
        }
        public void AMethodUsingB()
        {
            proxy.BMethod();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ActivatableRelationshipExample
{
    public class ObjectB
    {
        public virtual void BMethod()
        {

        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ActivatableRelationshipExample
{
    public class ObjectBProxy : ObjectB, IActivatable
    {
        private readonly UniDirectionalRelationship<ObjectB> relation;

        public ObjectBProxy(ObjectB to)
        {
            this.relation = new UniDirectionalRelationship<ObjectB>(to);
        }

        public override void BMethod()
        {
            if (relation.IsActive)
            {
                relation.Destination.BMethod();
            } else
            {
                // do some specific logic here
            }
        }

        public void Activate()
        {
            relation.Activate();
        }

        public void Deactivate()
        {
            relation.Deactivate();
        }

        public bool IsActive
        {
            get { return relation.IsActive; }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ActivatableRelationshipExample
{
    public class RelationshipNotActiveException : Exception
    {
        public RelationshipNotActiveException(string message) : base(message)
        {
        }

        public RelationshipNotActiveException(string message, Exception innerException) : base(message, innerException)
        {
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ActivatableRelationshipExample
{
    public class UniDirectionalRelationship<TDestination> : IRelationShip<object,TDestination> where TDestination :class
    {
        private bool active = true;
        private readonly TDestination destination;

        public UniDirectionalRelationship(TDestination destination)
        {
            if(destination == null)
            {
                throw new ArgumentNullException("destination","destination cannot be null");
            }
            this.destination = destination;
        }
        public void Activate()
        {
            active = true;
        }
        public void Deactivate()
        {
            active = false;
        }

        public virtual bool IsActive
        {
            get { return active; }
        }

        public virtual TDestination Destination
        {
            get
            {
                if (active)
                {
                    return destination;
                } 
                throw new RelationshipNotActiveException("the relationship is not active");
            }
        }

        public virtual object Source
        {
            get { throw new NotSupportedException("Source is not supported"); }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间ActivatableRelationshipExample
{
公共接口IActivatable
{
无效激活();
无效停用();
布尔是活动的{get;}
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间ActivatableRelationshipExample
{
公共接口关系:IActivatable
{
TDestination目的地{get;}
t源代码{get;}
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间ActivatableRelationshipExample
{
公共类对象
{
//您也可以在此处使用ObjectB引用
//并用ObjectBProxy实例填充它
//在构造函数中。我想这会
//更好地解释一下这个例子
私有只读ObjectBProxy代理;
公众反对A(反对b)
{
代理=新的ObjectBProxy(b);
}
公共空间使用b()
{
proxy.BMethod();
}
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间ActivatableRelationshipExample
{
公共类对象B
{
公共虚拟空间b方法()
{
}
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间ActivatableRelationshipExample
{
公共类ObjectBProxy:ObjectB,IActivatable
{
私有只读单向关系;
公共对象代理(对象B到)
{
this.relation=新的单向关系(to);
}
公共覆盖无效BMethod()
{
if(relation.IsActive)
{
relation.Destination.BMethod();
}否则
{
//在这里做一些具体的逻辑
}
}
公开作废激活()
{
关系。激活();
}
公开作废
{
关系。停用();
}
公共福利活动
{
获取{return relation.IsActive;}
}
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间ActivatableRelationshipExample
{
公共类关系NotActivieException:异常
{
public RelationshipNotActivieException(字符串消息):基本(消息)
{
}
public RelationshipNotActivieException(字符串消息,异常innerException):基(消息,innerException)
{
}
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间ActivatableRelationshipExample
{
公共类单向关系:i关系,其中TDestination:类
{
private bool active=true;
专用只读目的地;
公共单向关系(TDestination-destination)
{
如果(目标==null)
{
抛出新ArgumentNullException(“目的地”,“目的地不能为空”);
}
this.destination=目的地;
}
公开作废激活()
{
主动=真;
}
公开作废
{
主动=假;
}
公共虚拟图书馆是活动的
{
获取{return active;}
}
公共虚拟目的地
{
得到
{
如果(活动)
{
返回目的地;
} 
抛出新关系NotActivieException(“关系未激活”);
}
}
公共虚拟对象源
{
获取{抛出新的NotSupportedException(“不支持源”);}
}
}
}

我想你想要的是所谓的对象化关系