在更多与行业或自动化相关的应用程序(主要依赖于他们必须管理的外部组件)中,您经常会遇到这样的情况,即域包含的模型不仅仅是对实际问题的抽象,还包括表示和指向域外物理存在的事物的指针。
以这个代表网络设备的域实体为例:
public class NetworkDevice {
public IPAddress IpAddress { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
应用程序可能需要根据它们在域内的表示来管理外部组件,而不是仅仅存储或验证此类实体或对实体更改采取行动。现在,DDD 甚至适合这种情况吗?那些经理是域服务吗?
Eric Evans 在他著名的蓝皮书中描述,域服务需要是一个无状态 模型,实现取自ubiquitos 语言的方法,以完成实体或存储库无法自行处理的请求。但是如果一个服务需要有状态呢?
一个简单的例子:一个应用程序需要监控网络内配置的IP 设备,以便将状态事件通知域内的其他应用程序。如果 IP 设备在应用程序中注册(例如存储在数据库中),“ping-service”会收到通知并开始监视设备。
public class PingMonitor : IDisposable,
IHandle<DeviceRegisteredEvent>,
IHandle<DeviceRemovedEvent>
{
public List<NetworkDevice> _devices = new List<NetworkDevice>();
public void Handle(DeviceRegisteredEvent @event) {
_devices.Add(@event.Device);
}
public void Handle(DeviceRemovedEvent @event) {
_devices.Remove(@event.Device);
}
public void PingWorker() {
foreach(var device in _devices) {
var status = Ping(device.IpAddress);
if(status …Run Code Online (Sandbox Code Playgroud) c# design-patterns domain-driven-design cqrs onion-architecture
Error compiling template: views/devices.cshtml
Errors:
[CS0234] Line: 3 Column: 27 - The type or namespace name 'Services' does
not exist in the namespace 'Rioxo.Companion'
(are you missing an assembly reference?)
Details:
@using System
@using System.Collections.Generic
@using Rioxo.Companion.Services <---
Run Code Online (Sandbox Code Playgroud)
<razor disableAutoIncludeModelNamespace="false">
<assemblies>
<add assembly="Server32" />
<add assembly="Rioxo.Companion.Services" />
</assemblies>
<namespaces>
<add namespace="Rioxo.Companion.Server" />
<add namespace="Rioxo.Companion.Services" />
</namespaces>
</razor>
Run Code Online (Sandbox Code Playgroud)
这可能是什么问题?
我最近发现了杰弗里·巴勒莫的洋葱建筑模式,并决定出于好奇而将它用于当前的项目.之后我不得不用另一个(Ninject)替换我的IoC框架(Unity),因为它使我的项目完全无法使用它,我注意到我做出了正确的决定,因为我只需要更改我的一些部分应用程序,它仍然像一个魅力.
但是,我还没有弄清楚在这种模式下某些与Entity Framework和IoC相关的接口属于哪个界面.假设我有一个用于数据库上下文的接口:
public interface IDatabaseContext : IDisposable {
IDbSet<T> Set<T>() where T: class;
}
Run Code Online (Sandbox Code Playgroud)
这现在是域接口吗?还是基础设施界面?需要IDbSet <>'1类型的接口成员将接口与Entity Framework紧密耦合,但我需要一种方法将数据库上下文的瞬态实例注入存储库.IoC甚至适合洋葱建筑吗?您如何看待这种结构:
MyProject.Domain
- Entities
- MyEntity.cs
MyProject.Domain.Interfaces
- IDatabaseContext.cs // that doesn't fit here, right?
- IMyEntityRepository.cs
MyProject.Infrastructure
- Repositories
- MyEntityRepository.cs
- DatabaseContext.cs
- Migrations
...
MyProject.Web
- ... // web stuff
- NancyBootstrapper.cs
MyProject.Application
- Services
- ISnmpClientService.cs
- SnmpClientService.cs
MyProject.Server
- KernelHelper.cs
- IKernelResolver.cs
Run Code Online (Sandbox Code Playgroud) c# design-patterns domain-driven-design dependency-injection inversion-of-control
伪示例
public HttpResponseMessage GetAll() {
List<MyEntity> before = MyEntityRepository.GetAll();
return Request.CreateResponse(HttpStatusCode.OK,
new IEnumerable<MyExtendedEntity>({
before.ForEach(x =>
yield return new ExtendedEntity {
Property1 = x.Property1,
Property2 = x.Property2,
ExtendedProp = ExtendedPropProvider.getExtended(x)
})
);
Run Code Online (Sandbox Code Playgroud) LINQ扩展List.ForEach()不返回任何内容(void),但我想为列表中的所有对象分配属性值,然后将其中一些返回为IEnumerable.
例如
return myCollection
.ForEach(x => x.ToBeDetermined = determine(x))
.Where(x => x.ToBeTermined == true);
Run Code Online (Sandbox Code Playgroud) 简单的问题,多种可能性:
ICollection<T>IEnumerable<T>List<T>IList<T>Array相互之间有什么优缺点?是否存在性能/安全问题?
我最近经常看到这个 - 这是什么意思?
public void IPersistenceRepository<TEntity> : IDisposable {
|
v
public IEnumerable<TEntity> Where(Expression<Func<TEntity, bool>> @predicate);
}
Run Code Online (Sandbox Code Playgroud)
要么
public IDomainEventHandler<TEvent> : IEventHandler {
|
v
public void Handle(TEvent @event);
}
Run Code Online (Sandbox Code Playgroud) 我现在正在桌子上敲打几个小时,但是在C#中实现树形结构似乎太愚蠢了.
Node和NodeCollection.Node并NodeCollection可以有一个父NodeCollectionNodeCollection可以有一个子节点集合,它们是Node或者NodeCollectionNode不能有任何孩子.Node或NodeCollection不具有父节点被认为是根节点A Node具有任何任意类型的值,使用泛型
是否有BCL的集合类型用于此目的?到目前为止我所拥有的:
public abstract class NodeBase {
protected NodeCollection Parent { get; set; }
}
public class Node<T> : NodeBase {
public string Key { get; set; }
public T Value { get; set; }
}
public class NodeCollection : NodeBase …Run Code Online (Sandbox Code Playgroud)