Repository pattern, different identifiers

TLDR; I have one repo interface and multiple data sources, each with a different data identifier – how can I maintain having only one method in my interface?

I have a need to read a domain object OrderData from two different repositories: one is an outside service and the other one is a local database.
Let’s call them OutsideServiceOrderRepo and LocalDbOrderRepo.
They both implement an interface called IOrderRepoRead:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Interface IOrderRepoRead
+GetOrder(OrderEntityData) : Order
</code>
<code>Interface IOrderRepoRead +GetOrder(OrderEntityData) : Order </code>
Interface IOrderRepoRead
  +GetOrder(OrderEntityData) : Order

I’ll never use both implementations in the same use case, it’s an either/or situation. I have sorted out how to inject one or the other repo at the composition root, depending on the use case.
The problem I have is that each of these data stores has a different identifier for the data I’m after:

  • Local store has a simple OrderId that I have within my system.
  • External service requires me to query using a person’s Tax_number+Name+what_not etc…

The ideas I came up with so far are as follows:

  1. I have a type called OrderEntityData with properties TaxNumber, Name, OrderId. Both implementations can use this type, each implementation works with the properties it requires: OutsideServiceOrderRepo uses TaxNumber, Name, whereas LocalDbOrderRepo uses OrderId.
  2. I drop the OrderEntityData and have two different methods in my IOrderRepoRead.

.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Interface IOrderRepoRead
+GetOrder(taxNumber, name, whatNot) : Order
+GetOrder(orderId) : Order
</code>
<code>Interface IOrderRepoRead +GetOrder(taxNumber, name, whatNot) : Order +GetOrder(orderId) : Order </code>
Interface IOrderRepoRead
  +GetOrder(taxNumber, name, whatNot) : Order
  +GetOrder(orderId) : Order

I’m inclined to go with approach #1, but it still feels like there is a certain amount of coupling among the repo and client code.

How can I efficiently alternate between different identifiers?

8

It seems to me that the two services are not the same thing.

If taxNumber, name, whatNot produce a unique id, then you can join em all together, use that as your orderId and have the same GetOrder(taxNumber, name, whatNot) locally as the service.

If they do not, then the external service and your local repository are not implementing the same interface. One gets a unique record and one searches for matches.

You shouldn’t attempt to join the two, because even if you manage it with some clever code the Business logic isn’t the same. Have:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Interface IOrderSearcher
+GetOrder(taxNumber, name, whatNot) : Order
Interface IOrderRepository
+GetOrder(orderId) : Order
</code>
<code>Interface IOrderSearcher +GetOrder(taxNumber, name, whatNot) : Order Interface IOrderRepository +GetOrder(orderId) : Order </code>
Interface IOrderSearcher
  +GetOrder(taxNumber, name, whatNot) : Order

Interface IOrderRepository
  +GetOrder(orderId) : Order

Your local repository can implement both if required.

2

There is nothing wrong with approach number 2, and in fact this is the approach I’ve seen the most. Approach #1 could work as long as you lean on encapsulation to limit how clients can use this “order entity finder object”:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class OrderIdentifier
{
public int OrderId { get; private set; }
public string TaxNumber { get; private set; }
public string Name { get; private set; }
public string WhatNot { get; private set; }
public OrderLookupMethod LookupMethod
{
get => OrderId > 0 ? OrderLookupMethod.ById : OrderLookupMethod.ByTaxInfo;
}
public OrderIdentifier(int orderId)
{
OrderId = orderId;
}
public OrderIdentifier(string taxNumber, string name, string whatNot)
{
// ...
}
}
public enum OrderLookupMethod
{
ById = 0,
ByTaxInfo = 1
}
</code>
<code>public class OrderIdentifier { public int OrderId { get; private set; } public string TaxNumber { get; private set; } public string Name { get; private set; } public string WhatNot { get; private set; } public OrderLookupMethod LookupMethod { get => OrderId > 0 ? OrderLookupMethod.ById : OrderLookupMethod.ByTaxInfo; } public OrderIdentifier(int orderId) { OrderId = orderId; } public OrderIdentifier(string taxNumber, string name, string whatNot) { // ... } } public enum OrderLookupMethod { ById = 0, ByTaxInfo = 1 } </code>
public class OrderIdentifier
{
    public int OrderId { get; private set; }
    public string TaxNumber { get; private set; }
    public string Name { get; private set; }
    public string WhatNot { get; private set; }

    public OrderLookupMethod LookupMethod
    {
        get => OrderId > 0 ? OrderLookupMethod.ById : OrderLookupMethod.ByTaxInfo;
    }

    public OrderIdentifier(int orderId)
    {
        OrderId = orderId;
    }

    public OrderIdentifier(string taxNumber, string name, string whatNot)
    {
        // ...
    }
}

public enum OrderLookupMethod
{
    ById = 0,
    ByTaxInfo = 1
}

And then in your repository:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public Order GetOrder(OrderIdentifier id)
{
Order order = null;
switch (id.LookupMethod)
{
case OrderLookupMethod.ById:
order = // Find by Id
case OrderLookupMethod.ByTaxInfo:
order = // Find by tax info
default:
throw new ArgumentException(...);
}
return order;
}
</code>
<code>public Order GetOrder(OrderIdentifier id) { Order order = null; switch (id.LookupMethod) { case OrderLookupMethod.ById: order = // Find by Id case OrderLookupMethod.ByTaxInfo: order = // Find by tax info default: throw new ArgumentException(...); } return order; } </code>
public Order GetOrder(OrderIdentifier id)
{
    Order order = null;

    switch (id.LookupMethod)
    {
        case OrderLookupMethod.ById:
            order = // Find by Id
        case OrderLookupMethod.ByTaxInfo:
            order = // Find by tax info
        default:
            throw new ArgumentException(...);
    }

    return order;
}

The combination of the two constructors and making the setters private for the OrderIdentifier class ensures clients instantiate this correctly:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>OrderIdentifier byId = new OrderIdentifier(4); // Find by Id
OrderIdentifier byTaxInfo = new OrderIdentifier(taxNumber, name, whatNot); // Find by tax info
</code>
<code>OrderIdentifier byId = new OrderIdentifier(4); // Find by Id OrderIdentifier byTaxInfo = new OrderIdentifier(taxNumber, name, whatNot); // Find by tax info </code>
OrderIdentifier byId = new OrderIdentifier(4); // Find by Id
OrderIdentifier byTaxInfo = new OrderIdentifier(taxNumber, name, whatNot); // Find by tax info

If you’re looking for the simplest solution from your current point, the “two methods and a conditional” option works just fine.

If you’re looking for a more elegant, polymorphic solution, without passing redundant parameters, then here are a couple of approaches you could consider.

What you really have is two different implementations of an IOrderQuery interface (or whatever you prefer to call it). The trouble is that those implementations need different constructor parameters, supplied at request time and your composition is done in your application’s root.

You can solve that problem with a factory.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class InternalOrderQuery : IOrderQuery {
public InternalOrderQuery(string orderId) { ... }
public Order Get() { // fetch order by ID }
}
public class ExternalOrderQuery : IOrderQuery {
public ExternalOrderQuery(string taxNumber, string name, ...) { ... }
public Order Get() { // fetch order by things }
}
public interface IOrderQuery {
Order Get();
}
public class OrderQueryFactory {
public IOrderQuery Create(RequestData stuff) { ... }
}
</code>
<code>public class InternalOrderQuery : IOrderQuery { public InternalOrderQuery(string orderId) { ... } public Order Get() { // fetch order by ID } } public class ExternalOrderQuery : IOrderQuery { public ExternalOrderQuery(string taxNumber, string name, ...) { ... } public Order Get() { // fetch order by things } } public interface IOrderQuery { Order Get(); } public class OrderQueryFactory { public IOrderQuery Create(RequestData stuff) { ... } } </code>
public class InternalOrderQuery : IOrderQuery {
    public InternalOrderQuery(string orderId) { ... }

    public Order Get() { // fetch order by ID }
}

public class ExternalOrderQuery : IOrderQuery {
    public ExternalOrderQuery(string taxNumber, string name, ...) { ... }

    public Order Get() { // fetch order by things }
}

public interface IOrderQuery {
   Order Get();
}

public class OrderQueryFactory {
    public IOrderQuery Create(RequestData stuff) { ... }
}

Inject the factory and, in Create, create the order query as appropriate – the rest of your code doesn’t know the difference.

Alternatively, your first approach works well but can benefit from better encapsulation:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class OrderRequest {
private String name;
private String taxNumber;
private String orderId;
public OrderRequest( ... ) { // assign fields }
public Order Execute(IOrderRequestContext context) {
return context.Get(name, taxNumber, orderId);
}
}
</code>
<code>public class OrderRequest { private String name; private String taxNumber; private String orderId; public OrderRequest( ... ) { // assign fields } public Order Execute(IOrderRequestContext context) { return context.Get(name, taxNumber, orderId); } } </code>
public class OrderRequest {

    private String name;
    private String taxNumber;
    private String orderId;

    public OrderRequest( ... ) { // assign fields }

    public Order Execute(IOrderRequestContext context) {
        return context.Get(name, taxNumber, orderId);
    }
}

Implement IOrderRequestContext twice – once to execute against the remote DB and once against the local.

You could take this further and make the request parameters key/value pairs – that way the context can just read what it needs without needing you to explicitly define every possible parameter for every possible implementation.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật