c# – repository pattern, using the domain object as data access object

I’m very puzzled by a specific part of the repository pattern that seems simple but turn out to be tricky.
I took this great explanation of this topic by Mosh Hamedani; it’s a C# implementation of repository pattern with Entity Framework.

The key points is: repository should return domain objects. Not IQueryable and not other types of objects. Decoupling, fair enough.
An example of a method of a repository that Mosh proposes in that video is this:

public IEnumerable<Course> GetCoursesWithAuthors(int pageIndex, int pageSize)
{
return PlutoContext.Courses
        .Include(c => c.Author)
        .OrderBy(c => c.Name)
        .Skip((pageIndex - 1) * pageSize)
        .Take(pageSize)
        .ToList();
}

So we can see that there is complete separation between repository and the upper levels; they don’t have to know about the Context (provided that Repository gets its own copy at the construction).
The upper levels can query the repository as if it was an in-memory collection: it returns plain IEnumerables.
Everything seems to be decoupled… but one thing. The Domain Object itself, in this case: Course.
The Course is the domain object and is also the object used by the repository as EF Entity.
Actually the repository pattern definition doesn’t mention how the repository should build its entities,
so one can say that it’s fair for the repository to ‘reuse’ the domain object; we can also say that this is just another way of implementing, and any working implementation is ok since it’s encapsulated. That is, the repository’s client doesn’t know the details of how a domain object is build. Nevertheless the choice of using the very same class and instance to be both domain object and entity has non-obvious consequences. I can see the following:

  1. this way you can leverage the EF ability to trace changes on entities to detect changes to domain objects. That would be quite challanging to implement by yourself.
  2. the fact that ‘Course’ class is both an entity and a domain object is something that you have to be aware of when you implement additional methods, let’s say validation code or code related to the domain logic. This seems to me a violation of SRP, which practically means that if the aims of the “two classes merged into one” happen to diverge, you’ve got a problem.
  3. this approach saves from coping values from dao to domain object and vice versa

The important point here is the first; if it’s true, using entities as domain objects is more a necessity than a choice. So even if you wanted to, implementing a domain object ‘Course’ and a data access object (or entity) ‘CourseDAO’ would be quite challenging. And this seems to pose an obstacle to the achievement of a full decoupling.
At the end of the day and after all this work, full decoupling seems always to be a layer away!

This are my conclusions after having read different tutorials (especially the Mosh’s one), so I’m not yet sure if i got the right idea. Or if instead I spotted a ‘gray area’ in the way the repository pattern is implemented in those examples.

oracle 11g r2 – Using PL-SQL ‘Regular Expression’ to replace each ” (space)AND(space) ” pattern with ” * ” in a given string

I have a string like this x=y AND z=y AND p=q , I need replace every (space)AND(space) with * so that string turns into x=y*z=y*p=q. I’m using Regular expression for this cause the important thing is that there might by more that one space before and after each AND .

what I’ve written so far is: regexp_replace('x=y AND z=y AND p=q','((:space:))AND((:space:))','*');

I want to know whether there are better ways to do that .
Are there any conditions in which my query will produce false result?

Thanks in advance

networking – The publish-subscribe pattern: garbage-collecting old subscriptions

I’ve been studying distributed systems design, in particular the Udi Dahan’s class. He talks about the publish-subscribe pattern as a common pattern in messaging-oriented designs. There’s obviously a clear point in time when subscriptions to a publisher happen. However—and I might be missing something—he spent no time discussing unsubscribes.

So my question is—given a distributed system with components that evolve over time, how do popular implementations of this pattern deal with stale subscriptions? I understand that in a optimal situation, there are points in time when the subscriber explicitly declares that it is no longer interested in messages from a given source (maybe on clean shutdown, maybe on some kind of decommissioning of the component)… but given faulty networks, software and hardware, I can imagine the case where the publisher keeps thinking the subscriber still waits for its messages despite not existing for years, and no-longer-necessary messages clogging the messaging system.

java – Query related to combing static factory and singleton pattern

I have a class which I created which contains static factory methods for retrieving and filtering DB entities.. but could you give me some tips on how this can be converted to a singleton class .. so It returns an existing instance for a particular PensionHandlerParty / ListOfPaymentPlans OR creates a new one if not already present ..

Also If there is a better way to implement this , kindly let me know

@AllArgsConstructor(access = AccessLevel.PRIVATE)

public class GetPaymentPeriods {

private List<PaymentPeriod> paymentPeriods;

public static GetPaymentPeriods from(PensionHandlerParty pensionHandlerParty) {
    return new GetPaymentPeriods(pensionHandlerParty.getPaymentPlans().stream()
            .flatMap(pp -> pp.getPaymentPeriods().stream())
            .collect(Collectors.toList()));
}

public static GetPaymentPeriods from(List<PaymentPlan> paymentPlans) {
    return new GetPaymentPeriods(paymentPlans.stream()
            .flatMap(pp -> pp.getPaymentPeriods().stream())
            .collect(Collectors.toList()));
}

public GetPaymentPeriods forMonthsEqualOrGreaterThan(int month) {
    paymentPeriods = paymentPeriods.stream()
            .filter(pp -> pp.getStartDate().getMonth().getValue() <= month)
            .collect(Collectors.toList());
    return this;
}

public Optional<PaymentPeriod> havingOldestStartDate() {
    return paymentPeriods.stream()
            .min(Comparator.comparing(PaymentPeriod::getStartDate));
}


public long getTotalPensionAmount() {
    return paymentPeriods.stream()
            .mapToLong(this::totalAmountPaidPerPeriod)
            .sum();
}

private Long totalAmountPaidPerPeriod(PaymentPeriod paymentPeriod) {
    java.time.Period period = java.time.Period.between(paymentPeriod.getStartDate(), paymentPeriod.getEndDate());
    return paymentPeriod.getPensionAmount() * (period.toTotalMonths());
}

c++ – What design pattern to be used for more duplicate elements

I have to create a class named Phone, which can be any phone like Android, iOS and Windows. Again in Android, I will have various types of phones. For me challenge is, I have to maintain more than 100 properties of each phone.
Some features are common in all the three phones, some are common between only Android and iOS not in Windows. Some features in Android may get added in iOS(vice versa) in future. Some features in iOS can be deprecated.
How should I design by following Open-close principle?
If I go by strategy design pattern, I have to create interface for each property, which I feel doesn’t make sense.

Can some one guide what is the best way to create Phone class?

php – Identify design pattern of project

I need to verify what design pattern is used in this PHP project; I cannot identify which one from this List is. I leave the link of the project and the List of patterns.

Please download the project and check this Code running.

And I need to know the name of this type of syntax; is a call to a function:

Core_App::$obj_class('GEN')('VARSMANAGER')->VarExport();

domain driven design – Pattern to combine orders from different systems

We have a legacy order system and a new order system that are both currently active. The legacy order system takes ‘offline’ orders still, the new order system handles ‘online’ orders but uses the legacy order system for operations, so the legacy system has both offline and online orders. Eventually, the new system will handle all orders, but won’t have historical orders in it.

I need a solution that can source order information from both systems and display in a single ‘my orders’ view for customers. There are a number of cases/rules where the source of the data depends on how the order was created, e.g. the delivery address will come from the legacy system if it was an offline order, but the new system if it was an online order, the billing entity will always come from the legacy system.

As we move functionality these rules will change, i.e. for certain types of offline orders, the delivery address needs to come from the new system regardless of origin.

In some cases, the data may not be in the legacy system, so will be blank, but populated in the new system, so we need defaults and/or fallback (“if not in legacy system, find in new system”) behaviour for some data, e.g. products.

Is there a design pattern for this type of aggregation of two sources of data that would minimise future development required as we shift the ‘source of truth’?

object oriented – Design Patterns: Factory Pattern Vs. getInstance Inside Abstract Class

I’m working on an app where we need to use different authentication flows depending on how the user is accessing the app. I want to make sure that there is only one instance of the authentication service at a given time (singleton).

There aren’t that many auth services types and I don’t expect there to be more than 3-4 types in the future.

Here are the two approaches that I can think of:

Approach A (Factory pattern):

export class AuthFactory {
  public instance

  public getInstance(): AuthService {
    if (this.instance) {
      return this.instance
    } else {
      this.instance = fooConditon ? new authServiceA() : new authServiceB() 
      return this.instance
    }
  }
}

export abstract class AuthService {
  ...
}

export class AuthServiceA implements AuthService {
  ...
}

export class AuthServiceB implements AuthService {
  ...
}

Approach B

Completely skip the factory and have a getInstance() method inside the abstract class

export abstract class AuthService {
  public instance

  public getInstance(): AuthService {
    if (this.instance) {
      return this.instance
    } else {
      this.instance = fooConditon ? new authServiceA() : new authServiceB() 
      return this.instance
    }
  }
  ...
}

export class AuthServiceA implements AuthService {
  ...
}

export class AuthServiceB implements AuthService {
  ...
}

Which approach is better and why? For me, I feel like having the factory is overkill because there are very few sub classes of AuthService but I wonder if I’m overlooking something.

I was also wondering, if the factory approach is the way to go, is creating a singleton factory (one that only creates one instance of the product sub classes and not one of each product subclass) common? Is there a better way to do it?

Why association pattern unmatched when using variable

I have association and want to replace integer in specific element in this association. I’m trying to build pattern to be used by ReplaceAll command. When number is explicitly provided in pattern match occurs, but when I put number in a variable and provide number from variable match fails.

In[1]:= MatchQ[<|id -> 6, dur -> 10, skill -> 1, succ -> {7}, 
  dl -> 250|>, <|la___, succ -> {l___, 7, r___}, ra___|>]

Out[1]= True

In[2]:= last = 7
last[[0]]

Out[2]= 7

Out[3]= Integer

In[4]:= MatchQ[<|id -> 6, dur -> 10, skill -> 1, succ -> {7}, 
  dl -> 250|>, <|la___, succ -> {l___, last, r___}, ra___|>]

Out[4]= False

As shown in this example first match occurs, but second fails with number in variable.