How would you prevent a client from directly instantiating your concrete classes?
For example, you have a Cache interface and two implementation classes MemoryCache and DiskCache, How do you ensure there is no object of these two classes is created by the client using the new() keyword.
Thanks
Related
It is an abstract API upon which more domain specific APIs are based for querying URLs. Should the abstract version (which contains the networking functions, and the data structure) be written as a Class, Struct, or Protocol?
Given your requirements, it should be either a class, or a combination of a class and a protocol.
You cannot use protocol by itself, because it is incapable of holding data
Structs are a poor fit for anything abstract, because Swift structs are good for small types that have value semantic.
One approach that is good for data hiding is to expose a protocol, along with a method to obtain an instance of that protocol, but make the class implementing the protocol private to your implementation. This way the users would have to program to interface, because they have no access to the class itself.
I am about to start new java project and I consider GWT as my framework for it.
I've checked a lot of articles on the internet (main documentation also) and I am quite confused.
So I ask You:
Can I use jpa Entities to work with DAO, and to be sended over network to client?
I really don't understand the concept of DTO (writing almost the same but more poor class of Model)
If I will about to send Entities over network to client, how to use Serializable Transient annotation to NOT serialize and send methods, parameters of class? Is it even possible?
How all of this will work with Generic Typed Superclass extended by right class implementation?
f.e:
public class GenericModel extends GenericModel<T> {
//some generic code
}
public class RightModel extends GenericModel<RightModel> {
// some right class code
}
the same goes with DAO...
Please help.
I count on Yours experience.
Not that I was an expert at that time but when I started to combine Hibernate (not JPA as such) and GWT, it was a pain.
The reason that you can't just send over Hibernate managed objects (and I guess same counts for JPA, regardless of the underlying technology), is that they contain bytecode-manipulation stuff like javassist. GWT doesn't like this at all, and you can't send those objects over the GWT RPC wire.
Ofcourse also it doesn't make sense: you can't expect your Javascript (client-side) to invoke SQL to lazy-load collections when you invoke a getter on your DAO (because that's what happens server-side with these DAO objects, that's what the javassist magic is doing behind your back).
I'm not sure that even if all collections were eagerly loaded, your objects would be free of javassist stuff, and could be serialized over GWT-RPC. That leaves you with one alternative, which is to have POJO objects tailor-made to contain only those parts (properties/collections) that you need on the client at that moment - the DTO.
Since then a lot has happened in GWT, and I know of something completely different from RPC, which is RequestFactory (see http://www.gwtproject.org/doc/latest/DevGuideRequestFactory.html). I haven't used that myself, but it claims to make it easy to build data-oriented (CRUD) apps with an ORM-like interface on the client. So this may be the way to go if you don't want to build custom classes for wiring RPC.
Here is my set up thus far:
Database-First Entity Framework (generates a EDMX)
Separated POCOs into a different project
Left Custom DbContext under EDMX to be used as a bounded Countext
Created a UOW
Created a generic Repository (IRepository)
Mapped each entity into the UOW through concrete implementations of IRepository
UOW instantiates the Custom DbContext created by the EDMX and passes it along to each Repository
So far all is well.
Now, the problem stems out of that last part. The UOW instantiates the Custom DbContext, injects it into the Repositories and everything works... for the most part.
Each Repository implementation takes the DbContext, creates a DbSet and exposes the usual CRUD operations. All Entities are accessible through generics, so if I want to implement say GetAll() I will simply return DbSet and it will already be mapped to the Entity .
BUT, when I try to access a Function Import from DbContext within the Repository... I CAN'T.
It makes sense that I can't of course: the generic repository takes a DbContext as input, but it knows nothing of the Custom DbContext created by the EDMX, thus, all such functions added into the Custom DbContext are not known to the DbContext within the repository.
In other words:
I can access the Function Imports from the Custom DbContext while in the UOW
I pass the Custom DbContext to the constructor of each Entity Repository
The Repository expects any class that derives from DbContext, so the Custom DbContext does the trick
When the Repository tries to access the Function Import through the DbContext is has access to, it can't, because it has no knowledge of them
Obviously I can't just use the Custom DbContext everywhere, because I would be marrying the Repository to a specific Custom DbContext, and I'll be needing more than one since I'm created several Bounded Context.
Alas, THE QUESTION:
How could I call the Function Import from within the Repository without marrying it to a specific Custom DbContext?
Workarounds:
I know I could use reflection, but I'm trying to stir away from it for performance reasons (I know it is not that terrible, but still... the present question is about finding out a better way).
I have managed to get what I needed using DbContext.SqlQuery() to execute the Stored Procedure (which was mapped to the Function Import in the EDMX). Yet again, since I can easily swap Function Imports in the EDMX, I would like to find a way to access it within the Repository.
Hope it all makes sense. I appreciate any light anyone can shed onto the matter.
It's not clear to me how would you know what function to invoke or what parameters it takes. However you can take a look at the code generated for function imports and it basically looks like this:
((IObjectContextAdapter)this).ObjectContext
.ExecuteFunction<Customer>("Customers_With_Recent_Orders", customerId)
Therefore (instead of using reflection) you can just do the same thing dynamically - given that even with reflection you would have to know what function to invoke, you should know the type of the entity you expect and you can use params to pass any number of parameters it should be doable.
** EDIT **
You can also execute the function as no tracking by using the other overload:
((IObjectContextAdapter)this).ObjectContext
.ExecuteFunction<Customer>(
"Customers_With_Recent_Orders",
MergeOption.NoTracking,
customerId)
My application is broken down into several assemblies.
The MyProject.Infrastructure assembly contains all of the Domain objects such as Person and Sale as well as interfaces repositories such as IPersonRepository and ISaleRepository.
The MyProject.Data assembly contains concrete implementations of these repositories.
The repositories pull data from a database and instantiate new domain classes. For example, IPersonRepository.GetPersonByNumber(customerNumber) will read a customer from the data source, create a new Person class, populate it and return to the caller.
I'm now starting to see cases where adding some methods to my Domain classes might make sense, such as Person.UpdateAddress(address).
Is it ok to put this method on my Person class as a virtual method, and then create derived classes inside my Data layer which override those methods to provide the desired functionality?
I want to make sure I'm not going breaking any DDD conventions.
I know I also have the option of putting these methods on the repository - e.g. IPersonRepository.UpdatePersonAddress(person, address).
Person.UpdateAddress should definitely be in your domain, not in your Repository. UpdateAddress is logic and you should try to avoid logic inside your repository. If you're working with Entity framework there is no need for 'data classes'.
Most ORMs have change trackers which will persist related entities automatically when you persist the main one (provided you declared the right relations in the mapping configuration), so you don't need UpdatePersonAddress() on your Repository. Just do whatever you want to do at the object level in Person.UpdateAddress(address) without thinking about persistence, this is not the place for that.
What you need though is an object that will be called in execution context-aware code to flush changes to the persistent store when you think it's time to save these changes. It might be a Unit of Work that contains the Entity Framework DbContext, for instance.
Scala's collection library contains the forwarders IterableForwarder, TraversableForwarder, SeqForwarder and proxies like IterableProxy, MapProxy, SeqProxy, SetProxy, TraversableProxy, etc. Forwarders and proxies both delegate collection methods to an underlying collection object. The main difference between these two are that forwarders don't forward calls that would create new collection objects of the same kind.
In which cases would I prefer one of these types over the other? Why and when are forwarders useful? And if they are useful why are there no MapForwarder and SetForwarder?
I assume proxies are most often used if one wants to build a wrapper for a collection with additional methods or to pimp the standard collections.
I think this answer provides some context about Proxy in general (and your assumption about wrapper and pimping would be correct).
As far as I can tell the subtypes of Proxy are more targeted to end users. When using Proxy the proxy object and the self object will be equal for all intent and purposes. I think that's actually the main difference. Don't use Proxy if that assumption does not hold.
The Forwarder traits only seems to be used to support ListBuffer and may be more appropriate if one needs to roll out their own collection class built on top of the CanBuildFrom infrastructure. So I would say it's more targeted to library writers where the library is based on the 2.8 collection design.