This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi, Using interfaces, I'm trying to abstract my application logic and tests away from the Lightspeed ORM. This is so that if I want to change ORM I can do it without rewriting my entire application and tests. I want to put my domain objects that inherit from Entity<Tid> each behind a custom interface that exposes each object's methods/properties. This way in my application logic, a statement that reads:
List<DomainObject> domainObjects = LightspeedORMEntity.Find<DomainObject>();
can be:
List<IDomainObject> domainObjects = LightspeedORMEntity.Find<IDomainObject>();
where:
DomainObject implements IDomainObject and inherits from Entity<Tid>.
This way I could create AnotherDomainObject that also implements IDomainObject and inherits from Entity<Tid> and my application code would still work.
(Please note LightspeedORMEntity is wrapped in an interface - so that's not a dependency, I'm just calling it that for the example).
The following method works and compiles because "SalesChannel" implements IEntity (my custom interface) and inherits from Entity<Tid>. EntityFactory is wrapping the LightspeedFactory.
public IEntity CreateEntity<T>(int id) where T : IEntity {return EntityFactory.Create<SalesChannel>(Mindscape.LightSpeed.EntityState.New, id);}
What I want to do is: public IEntity CreateEntity<T>(int id) where T : IEntity {return EntityFactory.Create<T>(Mindscape.LightSpeed.EntityState.New, id);} Whereby the method definition is completely independent of lightspeed. This means that I can pass though any object that implements IEntity and inherits Lightspeed. Here is the error:
The type 'T' cannot be used as type parameter 'TEntity' in the generic type or method 'Mindscape.LightSpeed.Testing.EntityFactory.Create<TEntity>(Mindscape.LightSpeed.EntityState, object)'. There is no boxing conversion or type parameter conversion from 'T' to 'Mindscape.LightSpeed.Entity'.
Is what I want to achieve possible with Lightspeed? I essentially want to encapsulate repositories and domain objects behind interfaces and not reference lightspeed from anywhere else but inside concrete implementations of those interfaces. At runtime I can use IOC to instantiate the concrete lightspeed types, or any other type I want. Thanks in advance. |
|
|
<Posting again due to bad formatting..> Hi, Using interfaces, I'm trying to abstract my application logic and tests away from the Lightspeed ORM. This is so that if I want to change ORM I can do it without rewriting my entire application and tests. I want to put my domain objects that inherit from Entity<Tid> each behind a custom interface that exposes each object's methods/properties. This way in my application logic, a statement that reads:
List<DomainObject> domainObjects = LightspeedORMEntity.Find<DomainObject>();
can be:
List<IDomainObject> domainObjects = LightspeedORMEntity.Find<IDomainObject>();
where:
DomainObject implements IDomainObject and inherits from Entity<Tid>.
This way I could create AnotherDomainObject that also implements IDomainObject and inherits from Entity<Tid> and my application code would still work.
(Please note LightspeedORMEntity is wrapped in an interface - so that's not a dependency, I'm just calling it that for the example). The following method works and compiles because "SalesChannel" implements IEntity (my custom interface) and inherits from Entity<Tid>. EntityFactory is wrapping the LightspeedFactory. public IEntity CreateEntity<T>(int id) where T : IEntity What I want to do is: public IEntity CreateEntity<T>(int id) where T : IEntity Whereby the method definition is completely independent of lightspeed. This means that I can pass though any object that implements IEntity and inherits Lightspeed. Here is the error:
Thanks in advance. |
|
|
EntityFactory is a testing API which you can use to create concrete LightSpeed entities in specific known states for test purposes. There's no point using it with interfaces because if you're already using mock or other implementations of the interface, then you don't need EntityFactory -- you can set the state yourself because you control the test double. So if you're using IoC, you should probably forget about EntityFactory. Instead, in your CreateEntity<T> method, use your IoC container to obtain an instance of the T interface. Then configure your IoC container so that in real code it obtains this instance by new-ing up an entity of the appropriate concrete entity type, and in test code it obtains it by creating the required test double. (Alternatively, if you still want the test environment to operate with real LightSpeed entities rather than mocks, then the IoC container could be configured to use EntityFactory in the test code. But then you'll need to tell the IoC container how to map interfaces to concrete types that EntityFactory can create.) |
|