New answers tagged

0

Yes. No mediator is required. Assume the following interface: public interface ICommandHandler<in TCommand> where TCommand : ICommand { void Execute(TCommand command); } Now you have the following command: public class CreateCustomer : ICommand { /* ... */ } And a controller (example from ASP.NET MVC): public CustomerController { ...


-1

DTO is really powerful. One use is to compose data from multiple objects to be transported. The second use is to hide your data object details away from the rest of the application and this can give you the ability to change the data objects. For example if you were using mysql database then you changed to mongodb your data object can change as result. to ...


0

The best I've seen (if we do not go into GraphQL and graph-based formats) is a mechanism for deferred entries in the OData. Use query string to select what needs to be fetched in full (there can be custom filters, pagination, etc). For the rest - only URIs are fetched (this also provides for HATEOAS: Application will not need to construct URLs). This way an ...


0

I can put myself in your shoes, and also in the contractor's. Ask yourself the following questions: First of all, how long will it take to change the design? Is it really worth it? Effort in adding new functionality to the product. Good points of the design as it is (this is the key as he has widely more experience than you, it could be designed for ...


3

Why not use the "classic" approach to the strategy pattern with a base class EmailTemplateStrategy, a pure virtual method buildTemplate, and derivated classes like BuildReceiptTemplateStrategy, BuildVoidTemplateStrategy, BuildRefundTemplateStrategy? This is technically not much different from what you have now, but it has the following advantages A ...


1

Yes, Yes, and Yes You have struck on all the ways for passing information around. Construct a container and refer to it everywhere Pass many, many arguments through the function signature Construct an Agent and have them solve retrieving the information based on a request. There really isn't a best way. What might be reasonable will depend on the ...


1

I use this same style with my video game development with Unity 3D. I create components like Health, Input, Stats, Sound, etc. and add them to an Game Object to build up what that game object is. Unity already has mechanics to add components to game objects. However, what I found was most everyone was querying for components or directly referencing ...


5

I think you are making a common junior-level programmer error in your judgement here. Because the solution is not the way you would have done it, you want to redesign it. One of the most important things you need to become comfortable with is that whether the design is the way you think it should be doesn't matter. It really, truly doesn't matter at all. ...


15

Your first problem is that you think of the design as "wrong." That's really not the right way to consider things. Rather, different designs make different design trade-offs. Any design has pros and cons that have weighed against each other. If there is a problem with the design its not that is "wrong" but rather that it makes poor choice of trade offs. Don'...


1

Your code is contradictory. Here, you state that the content of your ITypeAData object is intended as a read-only value: public interface ITypeAData { int Value { get; } } Here, you clearly intend to be able to set the value of your ITypeAData content: (dataManager.TypeAData as TypeAData).Value = 0; This is contradictory. Either it's intended to ...


0

I think you captured the trade-offs pretty well. To make a good decision, we need to look into why we follow (anti)patterns like god class and separation of concerns. Your API client is a proxy, it doesn't do any work on it's own, it forwards the calls to another entity for processing. It's own concerns are centered around a REST API. Hence the reasons we ...


0

You should do what's right for your object design. The web api doesn't care about inheritance, it produces JSON containing a bunch of fields (or XML). If the fields are part of the same class or a parent class doesn't change the JSON. Hence you can change the service code at your leisure...


1

When it comes to business logic we agree that DRY principle is a cornerstone of structuring it well. However, this is not always true for the database world. The main reason is that assembling needed information from different sources requires additional costs. If we talk about the same database-scenario this is what denormalization is for: sometimes you ...


0

I would try to limit how far the concept of tenancy spreads throughout the application design. Many (micro)services can be designed without the need to have a tenantID. They may not need to know this information or whatever information is needed for them to process the task can be included in the message. For example a PDF conversion service may not need ...


1

First choice is better for security and independence between tenants, like the choice about database.But you have to build tools to handle it. Failure of one queue, don't expose failure to other tenants. First is not simple technically, but have functionals advantages : limited failure independence between tenants/confidentiality, eg: data cannot be ...


0

You might use Activator (if the performance hit is acceptable) or ORM approach, e.g. - use Activator, yet cache the result; - expect a particular constructor, throw if not present, cache after first found; - expect a static private field of particular type that holds (inter alia) constructor method, throw if not present, cache after first found etc. All the ...


3

Api.EmployeeController.Update(Api.EmployeeUpdateDto) => Services.EmployeeService.Update(Service.EmployeeUpdateDto) => Data.EmployeeRepository.Update(Entities.Employee) => Data.EfDbContext.Employees.Update(Entities.Employee Let's look at it this way: what if you were to tell your business counterpart this? Just the above word-for-word. I would wager he/she ...


0

As others have said The [Facade pattern][1] is an obvious way to go. To add to the answers thought I'd like to say you need to think carefully about the semantics of the API you are trying to wrap when creating the simpler Facade. Syntax is the easy part. For example your API may require an initialisation function followed by functions which actually do the ...


1

Having a time dimension table (in this case "dim.date") with a TimeStamp column holding the time stamp of each record of the fact table at the finest possible granularity (like miliseconds or finer) is not very sensible for a data warehouse. In the worst case, this would lead to a number of records in that dim table which is almost equal to the number of ...


1

I would model this with a state change request table. It would refer to the object and the kind of change (publish, delete, replace content). If the request is approved, the state change is executed. Special consideration would be needed when multiple state change requests are in flight.


1

If instead of: $result = (object) ['isfound' => null, 'issubscribed' => null]; the initial values were: $result = (object) ['isfound' => false, 'issubscribed' => false]; most of the other code could be simplified. E.g. if (!($found->data->recipient_count > 0)) { $result->isfound = false; $result->issubscribed = false; ...


2

A null object, in the context of the Null Object Pattern, is simply an object that does nothing when some operation is invoked.1 The idea is to avoid dealing with special cases - this includes, but is not limited to, avoiding null checks; e.g., you may want to do nothing if a certain set of conditions is not met. The calling code (client code) is written ...


4

Best way would be to have a type system that doesn't have "implicit null" for reference types. That means, that all references are non-nullable by default and can only contain null when explicitly defined. This is possible in C# 8 with never version of a compiler using Nullable reference types. This way, the client knows if returned reference value can or ...


1

This is a good use case for using Exception, if the language supports exception. You can just write: public function getSubscriberStatus(string $email): object { $endpoint = "contactdb/recipients/search?email=$email"; $found = $this->urlget($endpoint, 200); // this validation is probably unnecessary, looping over empty object has the same ...


-2

try this instead. I would use the additional status field and i'll create dropdown so that maker can select "submit" and approver can select"approve" so that the same update will be done on my database


2

Yes, it is very common to have a separate database for a data warehouse. There are two main drivers behind that: Protecting the performance and throughput of the transactional system since heavy user queries could affect its performance; The need for a different data schema for that is not necessarily aligned with the best fit for your transactional ...


3

Is this really common practice to have storages side-by-side like this? Absolutely. For many businesses, the relational DB is used for "online transaction processing" (OLTP), which holds the "current live data". The data warehouse infrastructure however is founded on things like "OLAP cubes", which may utilize different DBMS products and/or technologies ...


2

There's an equivalence between your getSomething method and an abstract class. Both are examples of the Strategy Pattern. public abstract class SomethingGetter<R, MSGIN, MSGOUT> { abstract MSGOUT query(MSGIN in); abstract List<R> getResults(MSGOUT out); public SomeClass<R> run(MSGIN query) { //some logic here ...


0

One use I haven't seen in these answers that's really common but you are kind of looking at the problem backwards (Your example code is both consuming and creating the factory, which isn't generally useful). A good use for it is where there are 2+ sets of code interacting (maintained by completely different teams). Let's take something like a log library ...


-2

Your RandomNumberOfAnimalsGenerator doesn't make a random number of animals. It makes precisely zero animals, in fact. What it does do is create a new list, containing several references to the same animal. For a RandomNumberOfAnimalsGenerator, I should be able to pass it "dog" and get back a list: Fido, Spot, Max. If I then feed the dogs: foreach(IAnimal ...


3

In the GoF you will see that: the Decorator inherits from Component in order to offer the same interface the Decorator uses composition of a Component object in order to be able to dynamically decorate/undecorate a component. the Decorator therefore implements all the methods of the Component interface by forwarding the call to the component. In other ...


1

This is one of those things where I both understand it and I don't at the same time. There is something to be said for not tying yourself to a given library, especially if it looks likely (or even just somewhat possible) that that library will stop being maintained in the future or that there is a reasonable chance you would need to change. It does give ...


8

I'm not sure yet another answer is needed here, but you asked for your code to be modified to explain the purpose of factories. So perhaps the following will help. Firstly, I'm going to change your IAnimalFactory to be an interface, rather than an abstract class. It serves no useful purpose being the latter. Also, we likely will have some criteria around ...


0

You asked for an example of why you'd ever need a factory but you haven't really been given one yet. Suppose you have a piece of code that creates a random number of animals. How do you decouple this from the details of how to create a specific type of animal? The answer: a factory. public class RandomNumberOfAnimalsGenerator { private readonly Func&...


4

There are different types of factories that serve different use cases. Often, you want to create objects of different (sub-) classes depending on runtime arguments. For this case, a simple function or a non-polymorphic class can be sufficient: public class AnimalFactory { public static IAnimal Create(String typeName) { if (typeName == "Tiger"...


4

The benefit of the Factory Method pattern is in expressing the fact that a piece of code does not want/need to know how to obtain an IAnimal. In fact, interfaces are often viewed from the wrong perspective: you can never force client code to use your interface. Rather, the client code can specify that it requires an interface, in this case, "a way to ...


-2

Your code is not an example of factory method. Factory method is just one single method, it is usually a static (in case of java). Having abstract method which is overloaded in different derivations doesn't make sense because in that case you need to create real single factory method to instantiate entities of implementation. I suppose your code is part of ...


7

Factory methods have a number of advantages. Mainly, they avoid the inbuilt limitations of constructors (can only have one name, cannot use caching, etc.). Entire Factories are used mainly to decouple the client code from the concrete types used to instantiate the interface. But your example doesn't do this. Your client doesn't have to know the Tiger type, ...


0

I'd say the problem here is the concept of the FieldGrid as an array of Tiles. You limit yourself to a classic implementation of the game where the UI, an ascii display, is tightly coupled to the game itself Fine if that what you want, but then you don't really need the Snake class. The snake is just the collection of SnakeTiles. A more modern design ...


2

You are at the right place for a review of your design. But this is not a code review site, so don't expect an in-depth inspection here. Overview What strikes me first looking at the classes is that: there is no relation between the Snake and either FieldGrid or Tile: it feels hard to believe that both could be as independent as this diagram suggests, ...


28

The problem The purpose of having these result classes derive from the same interface is so that the interface becomes what the consumer knows and works with. The consumer doesn't care about the specific implementing classes. However, your interface doesn't contain anything. You're using it as a marker interface. If I, as a consumer, receive an ...


5

This is a long but very interesting question, where you have exposed your step by step research. To summarize the problem: IService does some processing and returns an IOperationResult Different post-result actions need to be performed depending on IOperationResult Actions cannot be implemented polymorphically in IOperationResult because concrete ...


5

It seems to me that you are massively over complicating your life in this example by not using Exceptions instead of your IOperationResult interface. Simply throw a ValidationException or APIErrorException in your Service and handle with try catch as usual. Furthermore, say you did that, I would then say you should not be exposing the internals of your ...


0

As far as I know there are two things your should consider by choosing aggregates. The first is your invariants. For example you have 2 properties x and y and the sum of them must be always 6. Obviously you cannot change them independently, because you would end up in invalid states. After identifying your invariants you will know you consistency boundaries ...


1

I would recommend against thread local storage. That bypasses the normal rules around object lifetime (how long it exists) and scope (where it can be seen). It will potentially lead to very hard to debug situations, for example when your code runs into an exception and there is stale data in the thread context. Also, I am not certain there is a promise for ...


-1

This cannot be more straightforward. There are only 3 types of code: Code that never throws an exception. Code that you know will generate exceptions. For example, connecting to an external RPC server, it is expected that sometimes it will timeout. You should add try...catch for this type of code. Code that you didn't expect, but does throw errors. Since ...


0

Thank you very much for feedback, everyone. Based on answers I came up with a minimal example that works for me. I suppose we can call it a kind of combination of multi-stage construction and Factory. template<class C, class Cargs> class ArgFactory{ // Here is factory. The class C shall have C(Cargs a) // and ...


2

This will not work in view of C++ object lifecycle and the construction logic. Here is what happens: you construct a MyClassDerived Before the (empty) MyClassDerived constructor body is invoked, first the base object is constructed and then the member variables (with their default constructor unless you use a mem-initializer). The MyClass constructor is ...


0

I'm not an expert in C++ and I do not know what aspect you want to simplify. But I guess you could use some factory classes in order to pull out the initialization logic from your classes:


0

There is the Factory Method pattern. Other Creational Patterns may help as well.


Top 50 recent answers are included