Как реализовать сторону запроса CQRS в DDD?

можно добавить атрибут PrinicpalPermission многократно.

[PrincipalPermission(SecurityAction.Demand, Role="Admin")]
[PrincipalPermission(SecurityAction.Demand, Role="AnotherRole")]
16
задан Dave Schweisguth 14 February 2016 в 05:34
поделиться

2 ответа

From my understanding of CQRS you would create a set a DTOs that fulfil the requirements of the user interface screens or applications that may need to consume them.

Where this exists in the project is based on the requirements as it would depend if you were going to expose these DTOs via web services. In which case I wouldn't put it in the Web Layer but rather in the Application layer or a dedicated Façade layer.

Then you would have a read only repository or data access layer which populates the DTOs directly. I think that the Query side of things should be optimized for read performance in which case direct queries/stored procedures on database views or tables and SqlDataReaders would do the best job here. But it would definitely be worth abstracting this access behind an interface so you can add a cached implementation later down the track.

If you're using an ORM and want to map from your Domain Entities to the DTOs then you could have a generic QueryRepository which has methods which take an ISpecification or similar construct for defining your queries then a DtoAssembler object for creating the Dtos from your Domain objects. Then have an implementation has a first class object for each of the queries you are going to perform.

Here's a fairly contrived example but I hope it gives you an idea.

       public interface ISpecification<T>
        {
            Expression<Func<T, bool>> Predicate { get; }

        }

       public class ActiveCustomersSpecification : ISpecification<Customer>
        {
            private Expression<Func<Customer, bool>> predicate;
            public ActiveCustomersSpecification()
            {
                predicate = c => c.IsActive; 
            }
            #region ISpecicfication<Customer> Members

            public Expression<Func<Customer, bool>> Predicate
            {
                get { return predicate; }
            }

            #endregion
        }

        public interface IQueryRepository<T>
        {
            IQueryable<T> GetQuery(ISpecification<T> specification);

            IEnumerable<T> FindAllBy(ISpecification<T> specification); 
        }



public class CustomerDtoAssembler
    {
        public CustomerDto AssembleFrom(Customer customer)
        {
            var customerDto = new CustomerDto
            {
                Id = customer.Id
            };

            return customerDto; 
        }
    }
6
ответ дан 30 November 2019 в 23:18
поделиться

Я думаю, что willbt дал вам действительно хорошую отправную точку .

Я бы добавил, что если вы решите и дальше использовать ORM в качестве стратегии доступа к данным для запросов, вам было бы целесообразно рассмотреть возможность определения стратегии выборки, адаптированной к данным, которые, как вы ожидаете, вам понадобятся (кстати, я думаю конкретно о NHibernate). Это означает, что вы можете решить, загружать ли объекты и коллекции, связанные с определенным объектом Aggregate Root , с отложенной загрузкой или с нетерпением.

Проект NCommon Ритеш Рао предлагает отличную (в процессе разработки) демонстрацию того, как определить другую стратегию выборки для разных целей.

Ритеш действительно объясняет это в своем блоге.

3
ответ дан 30 November 2019 в 23:18
поделиться
Другие вопросы по тегам:

Похожие вопросы: