Should I use Public setter for events/messages in Axon? - domain-driven-design

Can we have public setters for the member variables in events/messages created and used through in Axon Framework?
As per my knowledge, events are something that have happened in the past, making them conceptually immutable. Hence, we should not have public setter.
Can someone please confirm this for me?

As you stated correctly, Events are "things from the past" which means they already happened and you should keep them and their contents immutable.
Since they are Java classes, you can create setters but on an Event Sourcing perspective you shouldn't.

Related

confuse methods in association class diagram

I have above class diagram. I am very confused on whether the above product methods should place in Product class or the user class. If I am right in my diagram, so I should only place product's setters and getters method on it?
Yes, your approach with placing the addProduct, deleteProduct etc on NormalUser is correct.
You might still have for example edit operation on Product to handle calls from (for example) NormalUser depending on your functionality/project/design/... .
Also don't use getters and setters (or at least make them private) unless you really know what you're doing. Providing public accessors to all attributes works in (almost) exactly the same way as making all attributes public effectively breaking the principle of hermetization/encapsulation.

Passing DB context as parameter in mvc

Is it the best way to pass context object as parameter to another C# method?
Can somebody advise, passing it this way will lead to any issues ??
Thanks in advance..
Passing a DbContext as a parameter isn't a problem at all -- there's nothing particularly special about it. It's just another class.
The only issue that jumps to mind is one that would be the same for any IDisposable -- the .NET developer guidelines recommend that only the class responsible for creating the IDisposable should dispose of it.
...that can be tricky to determine if you are using a dependency injection framework (eg Ninject) as it is sort of a mystery to any of the code written by the application where the objects are created.
To that end, you should never bind an IDisposable object in TransientScope:
Guidelines For Dispose() and Ninject

How do I force usage of a CDI producer method?

Part of my problem here is using the proper vocabulary, so I apologize in advance for what might be a simple matter of terminology.
Suppose I have a Person interface, and a PersonBean class that implements that interface.
Suppose further I have a producer method somewhere (annotated #Produces) that returns a Person. Internally it returns a new PersonBean, but that's neither here nor there.
Finally, suppose I have another CDI bean somewhere with an injection point defined like this:
#Inject
private Person person;
Assuming I have all my beans.xml files in place etc. and have bootstrapped Weld or another CDI-1.0-compliant environment, as this all stands I will get an ambiguous definition error. This makes sense: Weld will find my PersonBean as a candidate for injection (it could just call the constructor) and will find the output of my producer method as a candidate for injection.
What I'd like to do is somehow force the production of Person instances in this application to always route through the producer method.
I understand I could invent some qualifier somewhere and make the producer method produce Person instances that are qualified by that qualifier. If I do that, and change my injection point to include the qualifier, then obviously there's only one source of these qualified injectables (namely my producer method), so voila, problem solved.
But suppose I don't want to invent some bogus qualifier. (I'm not saying this is the case; just trying to more deeply understand the issues.) What are my options? Do I have any? I suppose I could put #Typed(Object.class) on the PersonBean to make it so that it was not seen as a Person by CDI....
Any ideas welcomed, including pointers to documentation, or better ways to understand this. Thanks.
Annotate you PersonBean as #Alternative then it will use the producer method.
From digesting several different answers here and elsewhere, the solution I've adopted is to use the #Typed annotation with a value of Object.class on my bean. This means that it will only be eligible to be injected into fields that are declared like this:
#Inject
private Object something;
...which thankfully prove to be pretty much nonexistent. :-)
What I'd like to do is somehow force the production of Person
instances in this application to always route through the producer
method.
Seam solder has a solution for this.
I'm not 100% sure how this will develop with the merge of Seam 3 and Deltaspike (the page is so 90s, but the content rocks :-), but putting Solder in your classpath is certainly a safe bet.
Oh, and as far as I know a comparable mechanism made it into the CDI 1.1 spec.

How to run aspect advice for a method which is called by another method in the same class

I am having a throuble about Spring AOP. I am trying to trigger a method using aspect but the method that will trigger the aspect is also the method of the same class and aspect is not working(No errors by the way).Like this
class A extends Runnable{
public void write(){
System.out.println('Hi');
}
public void run(){
this.write();
}
}
<aop:after-returning method="anyMethod" pointcut="execution(* A.write(..))"/>
Any ideas will be appreciated
Thanks
The fact that the advised method is called in a different thread doesn't make any difference. Just make sure the instance that you pass to the thread is created by the spring application context and not by your application code.
Also, since you're advising a method declared in a class, not an interface -- write() -- you'll need to perform load-time weaving (and have cglib in your classpath).
This is because Spring AOP is proxy based. You use a proxy to delegate calls to the underlying object. However, when an underlying object's method makes a call to another method inside it, of the same class (your use case) then proxy does not come into picture and hence what you are trying to achieve is not possible. There are some work arounds, but they kill the very purpose of AOP.
You can refer more information here.
http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/aop.html#aop-understanding-aop-proxies
As Abhishek Chauhan said, Spring AOP is proxy-based and thus cannot intercept direct calls to this.someMethod(). But the good news is that you can also use full-blown AspectJ within Spring applications via load-time weaving as described in the Spring manual. This way you can get rid of the limitation and even of the whole proxy overhead because AspectJ does not need any proxies.

Using object's setter to trigger data updates, best practices

I have an object that gets instantiated in a linq to sql method. When the object fields are being assigned, i want to check a date field and if it is an old date, retrieve data from another table and perform calculations before continuing with assigning this object.
Is there anything wrong with triggering such an event through the property setter Or should I independently check the date through some service and make the changes if necessary at some point aftwewards?
There's nothing wrong with doing some logic from within your setters, but you should be careful about just how much logic you put within your setters. One of the fundamental problems of setters is that since they act like attributes, but have backing code, it's easy to forget that there are potentially some non-trivial actions going on behind the scenes.
This sort of thing can cause problems if you have accessors which use accessors which use accessors; you can rapidly end up causing unexpected performance problems. Generally, it's a good idea to keep the actions of setters (or getters, for that matter) to a relatively small set of actions. For example, validation can work perfectly fine in a setter, but I'd generally advise against doing validation against external resources, because of two things: first, resource delays can cause problems with expected access speed, and secondly, the number of external resource accesses can destroy your performance.
Generally, the rule is this: keep it simple. It's not unreasonable to do complicated things in a setter, but if you do, it's really important to understand the consequences of all of the actions you'll be causing, and it's EXTREMELY important to document what it does extremely well, so the next guy (or girl) to use the code doesn't just try to naively use the accessor and end up causing massive resource contention issues unexpectedly.
Half the point of using setters instead of, say, public fields, is to be able to trigger events associated with setting certain data.
Keyword: associated. If you're just using the setter as a "convenient" time to do some other stuff because it happens to work, you're doing it wrong. If setting this value requires other work to be done, then by all means, use the setter to do it.

Resources