I'm trying to model the following situation into an XSD schema, but kinda stuck and not sure if XSD supports what I'm trying to achieve.
I have two complex elements, which both should be abstract:
Session
SessionResult
Their relationship: one Session can contain zero or more SessionResults
Then I have these other complex types (non-abstract):
RaceSession (inherits from Session)
TimedSession (inherits from Session)
RaceSessionResult (inherits from SessionResult)
TimedSessionResult (inherits from SessionResult)
What I want to achieve is that, when a Session is of type RaceSession, only RaceSessionResult elements are allowed as SessionResult. Subsequently, when a Session is defined as a TimedSession, its SessionResult elements should be of TimedSessionResult.
Right now I'm not able to achieve this in XSD. For now as a workaround, I did not define SessionResult as a subelement-list of Session. Session serves as base for TimedSession and RaceSession, and these latter two just have TimeSessionResult or RaceSessionResult elements respectively.
Is there any way to achieve what I described above in XSD ? The purpose is then to generate c# classes out of them.
Here's the C# equivalent of what I'm trying to do.
public class Event
{
public List<Session> Sessions { get; set; }
}
public abstract class Session
{
public string Weather { get; set; }
public List<SessionResult> SessionResults { get; set; }
}
public abstract class SessionResult
{
public int Position { get; set; }
}
public class RaceSession : Session
{
public new List<RaceSessionResult> SessionResults { get; set; }
}
public class TimedSession : Session
{
public new List<TimedSessionResult> SessionResults { get; set; }
}
public class RaceSessionResult : SessionResult
{
public int StartPosition { get; set; }
}
public class TimedSessionResult : SessionResult
{
public decimal BestLapTime { get; set; }
}
Thanks a ton for any input !
Related
I'm having an issue with the ServiceStack implementation of Swagger with regards to the documentation of generic-typed response objects. Strongly-typed response objects are correctly documented and displayed, however once a generic-typed object is used as a response, the documentation is inaccurate and misleading.
Request DTO
[Route("/users/{UserId}", "GET", Summary = "Get a specific User Profile")]
public class GetUser : IReturn<ServiceResponse<UserProfile>>
{
[ApiMember(Description = "User Id", ParameterType = "path", IsRequired = true)]
public int UserId { get; set; }
}
Response DTO
public class ServiceResponse<T> : IServiceResponse<T>
{
public IList<string> Errors { get; set; }
public bool Successful { get; set; }
public string Message { get; set; }
public string StackTrace { get; set; }
public T Data { get; set; }
public ServiceResponse()
{
Errors = new List<string>();
}
}
Response DTO Type
public class UserProfile : RavenDocument
{
public UserProfile()
{
Races = new List<UserRace>();
Workouts = new List<Workout>();
}
public string FirstName { get; set; }
public string LastName { get; set; }
public string DisplayName { get; set; }
public DateTime? BirthDate { get; set; }
public Gender? Gender { get; set; }
public string UltracartPassword { get; set; }
public string UltracartCartId { get; set; }
[UniqueConstraint]
public string Email { get; set; }
public string ImageUrl { get; set; }
public FacebookUserInfo FacebookData { get; set; }
public GoogleUserInfo GoogleData { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime? LastUpdated { get; set; }
public UserAddress ShippingAddress { get; set; }
public UserAddress BillingAddress { get; set; }
public IList<UserRace> Races { get; set; }
public IList<Workout> Workouts { get; set; }
}
The examples are pretty straight forward. Nothing really hacky or clever going on, however this is the sample documentation I get from Swagger out of the box:
As you can see, the generic type isn't documented correctly and some other type is used instead. As I am using this same ServiceResponse wrapper for all my responses, this is happening across the board.
As you have found, the ServiceStack swagger plugin does not currently attempt to handle generic types cleanly. A simple alternative that should work better is to make concrete subclasses of the generic types. e.g.:
public class UserProfileResponse : ServiceResponse<UserProfile> { ... }
public class GetUser : IReturn<UserProfileResponse> ...
This should be handled properly by Swagger.
I've found generic types aren't always a great fit for ServiceStack DTOs. You'll find many discussions (for example here, here and here) on StackOverflow that discuss this, and the reasons why concrete types and generally avoiding inheritance is a good idea for ServiceStack DTOs.
It takes effort to overcome the temptation to apply the DRY principle to request/respone DTOs. The way I think about it is that generics and inheritance are language features that facilitate implementation of algorithms in generic, reusable ways, where the generic method or base class doesn't need to know about the details of the concrete type. While DTOs may superficially have common structures that look like opportunities for inheritance or generics, in this case the implementation and semantics of each DTO is different for each concrete usage, so the details of each request/response message deserve to be defined explicitly.
Consider the following:
public class VideoContainer<T>
{
public string Name { get; set; }
//public List<VideoContainer<T>> VideoContainers { get; set; }
}
public class Perspective : VideoContainer<Perspective>
{
public List<VideoContainer<SourceContainer>> VideoContainers { get; set; }
}
I want to ensure VideoContainer<Perspective>.VideoContainers can only contain VideoContainer<SourceContainer> types.
I add a new Perspective object to a List<Perspective> with three VideoContainers. The problem is that when I add a new Perspective to the list, the previously-added Perspective.VideoContainers is null.
Why is this happening?
It sounds like you need two generic types:
public class VideoContainer<T, U>
{
public string Name { get; set; }
public List<VideoContainer<U>> VideoContainers { get; set; }
}
public class Perspective : VideoContainer<Perspective, SourceContainer>
{
// No longer declare the list, just use it... it's now:
// public List<VideoContainer<SourceContainer>> VideoContainers { get; set; }
}
public class PageRoleService
{
public void SetRoles(Page page, User activeUser)
{
var rb = page.Project.ProjectType.GetRoleFor(activeUser.UserType);
page.RolesForPage.Add(activeUser, rb);
var managers = GetAllManagersOf(activeUser);
foreach (var m in managers)
{
page.RolesForPage.Add(m, rb);
}
}
}
public class Project : Entity
{
public ProjectType ProjectType { get; set; }
public IList<Page> Pages { get; set; }
}
public class Page : Entity
{
public string Name { get; set; }
public Project Project { get; set; }
public IDictionary<User, RoleBehaviour> RolesForPage { get; set; }
}
public class ProjectType : Entity
{
public IQueryProcessor QueryProcessor { get; set; }
public IList<RoleBehaviour> RoleBehaviours { get; set; }
public RoleBehaviour GetRoleFor(USerType userType)
{
var behaviour = return QueryProcessor.Execute(new GetRolesByUserAndProjectTypeQuery() {
ProjectType = this,
UserType = userType
});
// Filter behaviour attributes for project type properties, business rules, etc...
// FilterBehaviour(behaviour);
return behaviour;
}
}
public class GetRolesByUserAndProjectTypeQuery
{
public UserType UserType { get; set; }
public ProjectType ProjectType { get; set; }
}
public class GetRolesByUserAndProjectTypeQueryHandler
{
public Db Db { get; set; }
public RoleBehaviour Execute(GetRolesByUserAndProjectTypeQuery query)
{
return Db.FirstOrDefault(r => r.UserType == query.UserType && r.ProjectType == query.projectType);
}
}
public class RoleBehaviour : Entity
{
public Role ROleForArea1 { get; set; }
public Role ROleForArea2 { get; set; }
public UserType UserType { get; set; }
public ProjectType ProjectType { get; set; }
public IDictionary<string, string> Attributes { get; set; }
}
public enum UserType
{
A,
B,
C,
D
}
public class Role : Entity
{
public IList<string> Permissions { get; set; }
}
I don't use repository, no need data abstraction, I use CQRS for crud operations. (CreateProjectCommand, GetRolesByUserAndProjectTypeQuery, etc..)
Users related a lot of project and page. Users have more than role for each Page Entity and is dynamically created when user (client) request to fetch All projects page or single page item.
My Page Role Service determinates page roles for active user and its managers. My MVC Controller use PageRoleService.
PageRoleService is Application Service or Domain Service or .....?
QueryProcessor in Entity (ProjectType) is invalid approach? How can handle this/their problems without lazy or eager loading?
RoleBehaviour is Entity or Value Object?
PageRoleService is a service or business logic in domain?
I know that I'm some years later, but:
I would put away the base class Entity, because it looks that this are just Dtos returned by the queryhandler (infact GetRolesByUserAndProjectTypeQueryHandler.Execute returns a RoleBehaviour).
Given this, I think that:
PageRoleService is a simple service that completes a Dto, hence it looks a kind of factory
Given that ProjectType here has two different roles (a Dto and Entity, and this is against CQRS), if:
it's a Dto, then use a service/factory/ORM to load extra data on it
it's an Entity, try to load all the data that's needed by it. This because there're great changes that you'll need it on the way to execute your command (great explanation about DDD and entities).
The object has it's own identity? Has it an Id that, even if things will change, remains the same? Looking at it, it looks just a Dto, with nothing really interesting (at business level).
see 1.
I have the following in my domain classes ( simplified )
public enum JobType
{
SalesOrder = 1,
StockOrder = 2
}
public class SalesOrder : LoggedEntity
{
public string Name { get; set; } // and other fields
}
public class StockOrder : LoggedEntity
{
public string Name { get; set; } // and other fields
}
public class Job : LoggedEntity
{
public int JobType { get; set; } // jobtype enum
public virtual LoggedEntity LinkedEntity { get; set; }
}
My context is as follows;
public class Context : DbContext
{
public DbSet<Job> Jobs { get; set; }
public DbSet<StockOrder> StockOrders { get; set; }
public DbSet<SalesOrder> SalesOrders { get; set; }
}
When I run the migration i get the error described [here][1] So using an abstract entity appears not to work.
My question was, how do I create a navigation property that can navigate to more than one entity type?
If JobType = SalesOrder then I want to navigate to sales order, if JobType = StockOrder then I want to navigate to stock order.
I wanted to use a Table Per Heirarchy Strategy [see TPH here][2]
The trick is to keep EF oblivious of the LoggedEntity class. Remodel your entities according to this example:
public enum JobType
{
SalesOrder = 1,
StockOrder = 2
}
public abstract class LoggedEntity
{
public int Id { get; set; }
public string Name { get; set; } // and other fields
}
public abstract class BaseOrder : LoggedEntity // New base class for orders!!
{ }
public class SalesOrder : BaseOrder
{ }
public class StockOrder : BaseOrder
{ }
public class Job : LoggedEntity
{
public JobType JobType { get; set; } // jobtype enum
public virtual BaseOrder Order { get; set; }
}
public class Tph2Context : DbContext
{
public DbSet<Job> Jobs { get; set; }
public DbSet<BaseOrder> Orders { get; set; }
}
You will see that the migration creates two tables, Jobs and BaseOrders (name to be improved). Job now has a property Order that can either be a SalesOrder or a StockOrder.
You can query specific Order types by
contex.Orders.OfType<StockOrder>()
And you will notice that EF doesn't know LoggedEntity, because
context.Set<LoggedEntity>()
will throw an exception
The entity type LoggedEntity is not part of the model for the current context.
how do I create a navigation property that can navigate
to more than one entity type?
You cannot do so. atleast not now. navigational properties are way of describing relationship between entities. at most, they represent, some sort of sql relationship. so you cannot alter or define such a relationship on the fly. you have to define it before hand.
Now in order to do that, you have to define separate navigational property for your separate conditions i.e.
public class Job : LoggedEntity
{
public int JobTypeSales { get; set; }
public int JobTypeStock { get; set; }
public virtual SalesOrder SalesOrder { get; set; }
public virtual StockOrder StockOrder { get; set; }
}
and then link them in configuration in modelbuilder through fluent API.
HasRequired(s => s.SalesOrder)
.WithMany()
.HasForeignKey(s => s.JobTypeSales).WillCascadeOnDelete(true);
HasRequired(s => s.StockOrder)
.WithMany()
.HasForeignKey(s => s.JobTypeStock).WillCascadeOnDelete(true);
and
as for your error "Sequence Contains No Elements"
this error comes, when the Linq query that you specified, is using either .First() or .Single(), or .ToList() and query returned no data.
so to avoid it use, .FirstOrDefault() or SingleOrDefault().
obviously with proper null check.
I have a flat domain class like this:
public class ProductDomain
{
public int ID { get; set; }
public string Manufacturer { get; set; }
public string Model { get; set; }
public string Description { get; set; }
public string Price { get; set; }
}
I have two DTO classes like this:
public class ProductInfoDTO
{
public int ID { get; set; }
public string Manufacturer { get; set; }
public string Model{ get; set; }
}
public class ProductDTO : ProductInfoDTO
{
public string Description { get; set; }
public string Price { get; set; }
}
Now the problem is:
Scenario #1:
Mapper.CreateMap<ProductDomain, ProductInfoDTO>() // this mapping works fine
Scenario #2:
Mapper.CreateMap<ProductDomain, ProductDTO>() // this mapping is not working and throws System.TypeInitializationException
So my question is how to create mapping between ProductDomain and ProductDTO (which inherits ProductInfoDTO) without breaking the definition of both source and destination classes. Also I dont want to introduce any new inheritance for the domain class ProductDomain.
Thanks
You can build your own custom TypeConverter like this
public class ProductDomainToProductDTOConverter : ITypeConverter<ProductDomain, ProductDTO>
{
public ProductDTO Convert(ProductDomain source)
{
ProductDTO product = new ProductDTO();
product.Price = source.Price;
...
return product;
}
}
And then create a map with your custom TypeConverter like this
Mapper.CreateMap<ProductDomain, ProductDTO>().ConvertUsing<ProductDomainToProductDTOConverter>();