How can I expose all entities decorated with #Entity in a specific package as rest services without creating an interface public interface PersonRepository extends PagingAndSortingRepository for each such entity. In this doable?
You cant.
It's like asking a person to do a car. You cannot do a car. you can do some functions in the car like, accelerate or break.
According to spring-data-rest you have to create the repositories for the framework to identify the rest resources.
Refer http://spring.io/guides/gs/accessing-data-rest/
Further more, an entity is a data wrapper, while the repository is a function wrapper for that entity. You can only expose a set of functions (ie like CRUD). You cannot expose a bold entity by itself. In other words, what actually can you expose in an entity via a REST.
Related
I have many models that defines all my db tables; I wondering which is the best way to create one single CRUD ServiceStack interface for all these models without write the same code for each one.
I'd like to keep it DRY to ease future maintaining.
Thank you.
Checkout AutoQuery which lets you expose a rich, queryable API's for each table by just declaring its Request DTO:
[Route("/movies")]
public class FindMovies : QueryBase<Movie> {}
You want a typed Request DTO for each Service, but other than that you can use a base class, shared extension or utility methods to execute common logic as you would in normal C#. The built-in Auto Mapping also reduces the boilerplate for populating a Table POCO from a request DTO.
How to maintain the custom methods while using generated classes using RIA services and entity models ?
Everytime the model changes, the only way to generate methods for these entities is to regenerate the DomainService class. But if we do that, the custom methods that we have created in the DomainService class gets overwritten. Is there a better way to do this?
You can use partial keyword for YourDomainService class and define another class for writing your own code. By regenerating the YourDomainService class, you can save your codes!
I am trying to build a generic repository that allows querying against domain classes.
My Repository interface looks like the following:
public interface IRepository<T>
{
T Get(int id);
IQueryable<T> Query();
void Add(T model);
void Remove(T model);
}
Given I have an UserEntity entity framework class and a User domain class, I want to query against the User. The UserEntity should not be exposed to other services, because it should be internal to the Entity Framework layer.
A query like userRepository.Query().Single(user => user.UserName == "Toni") should return a User domain class. However internally it should query against an IDbSet<UserEntity> returned from my entity framework. The Expression Tree (which contains the Single query operation) should be attached to a query against IDbSet<UserEntity>. After querying against IDbSet<UserEntity> I want to convert the UserEntity to a User domain class. Is this possible?
I have in mind to cerate an IQueryable implementation for my User class that internally queries against UserEntity.
public class MappedEntityQuery<TModel, TEntity> : IQueryable<TModel>
{
}
Code First requires the convention to have all IDbSet properties to access the tables to be in the DbContext
That is not true. You don't need to have any set declared in the context if you provide mapping to the entities in the model builder. In your case you should declare mapping through EntityTypeConfiguration<T> and ComplexTypeConfiguration<T> derived classes. You can create any DbSet instance of mapped entity type by calling Set<T>() on the context.
However for this project I am using a Database First approach, which also does not allow to load compose a DbContext using entities from different projects, because you have to specify the Database metadata in one single metadata file (which will be embedded).
That is true only partially. EDMX metadata must be in the single file embedded in the main project but entity classes do not have to if you use your own instead of auto-generated. So your proposed approach should work.
But if you really want to achieve modularity you should not use EDMX. If you decide to add or change any module in the future it would require changing central project as well but that can affect all other modules - it breaks the idea of modularity, doesn't it?
I am utilizing the Entity Framework in order to interface with some WCF services and some MVC3 websites I have built. I am using a database first approach.
What I have created is a library that has a Data, Model, and CRUD folder. Data contains the edmx file and a partial class for my Entities that creates a ToDTO() method. The Model contains a class of each of my entities that I can pass around as an object. The CRUD contains what should be expected - common read, update and delete methods.
My entities are named in the fashion of 'StudentEntity', and my DTO have a simpler name such as 'Student'. So the StudentEntity class has a method ToDTO which returns a Student object.
And my CRUD folder has a class with name Student that contains the CRUD operations for the student entities.
The confusion seems to come when I have another deleloper look over the code, they get confused on which Student they are looking at, the entity, the DTO, or the CRUD class.
How should I change my naming scheme to make it more understandable? Also can you give me any suggestions on cleaning it up a bit. Maybe I do not need the DTO classes and can somehow use extension/reflection to not have a separate class for each ToDTO method.
If I had to keep that same architecture, I would use the following naming conventions:
// Namespaces & folders
// /DTO (Data Transfer Objects classes only)
// /Model (edmx files and utility classes)
// /Data (Repositories)
// /Services (Specialized repositories and business logic classes)
// Naming conventions
StudentDTO // (Data Transfer Object)
Student // (the entity itself)
StudentRepository // (very common and conventional name)
StudentService // (common and conventional name)
My 2 cents,
Sincerely,
Max
I use standard ObjectContext and EntityObjects in my application. Let's say two of my tables are Projects & Services. Projects have Subproject (from Projects table with ParentID == ProjectID) and also Services. So I would have a hierarchy like Projects->Subprojects->Services. But I need to inherit Projects and Services from an abstract base class so I can use any of these entities as a new Task/Job entity in my application. Then, for example I can create a TreeList listing all Tasks (either a Project or Service). Is there anyway in EDMX designer I can create a new type (entity) which is the base calss for two or more concrete types?
It is possible with TPC inheritance but it will include a lot of complication to your design. For example:
you will have to move shared properties to the base class
you will probably have to maintain some mappings manually in EDMX (at least I had when I did the sample on screenshot)
you will have only single ObjectSet<Tasks> and you will have to use OfType to query only Projects or Services
you will have to use unique Id per Task = across both Project and Service tables (can be achieved by correctly configured identities in database)
It will look like:
Another option is using interface on your entity objects instead of parent class. You can define interface in your partial part of entity object and handle retrieving both Projects and Services by yourselves where your UI will expect only list of types implementing your interface.
http://blogs.microsoft.co.il/blogs/gilf/archive/2010/01/25/table-per-concrete-type-inheritance-in-entity-framework.aspx
Since it sounds like your data is coming from 2 separate tables, Projects and Services, no, I don't think you can achieve this in the designer (at least, not without hand-editing the generated edmx). If there were a common table to represent the base class, that could be done in the designer, but that doesn't sound like it fits your situation.
What you may be able to do is use an interface instead of an abstract base class, and use partial classes in your entity model to implement the interface for each of your entities. You can't directly inherit from your abstract base class in your entity model, because all of your entities already derive from EntityObject. If you have a lot of shared implementation that resides in your base class, it might be worthwhile to switch to POCO, where you can define your own inheritance hierarchy.