This project has moved and is read-only. For the latest updates, please go here.

Using Pure poco for model without decorating with Enity attribute

Mar 17, 2014 at 8:34 PM
I'm trying to figure out if there is a way to use my model interface classes without having to apply the Entity attribute. I want to be able to use the model interfaces in other assemblies without referencing the brightstar assemblies.
Mar 18, 2014 at 9:48 AM
Right now that is not really possible without getting deep into the code as the [Entity] attribute is required for both the code generation and the run-time mapping of entity to RDF type to work. I recognize that this is an issue for some people and its on the plan for 1.7 to provide some better support for setting up a context directly from code without having to go down the code generation route.

If I can find a way to do away with requiring any B*-specific attributes in the interface / POCO part of the code I will do that too.
Mar 18, 2014 at 12:20 PM
Actually I think I may have a simple solution right now. It's only going to work if there are no other attributes used other than the entity one. Basically I've written my own version of the reflectionentitymapper class that looks for classes in a specific namespace. I just modified the tt file for code generation. So far my testing seems to indicate it working. I've also had to implement my own entityset (basically just inherits the bs one) so that I could then create my own idatacontext for IOC injection that doesn't require the bs assemblies in other assemblies further up the project. It's not perfect but seems to work. The only other thing I'm having issue with is that it's not possible to use composite keys. I'm trying to find a way to efficiently test before adding or updating an entity against all other.



Mar 18, 2014 at 12:36 PM
Awesome! If you could post some sample code or a writeup of how you got this to work that would be really appreciated - I know that there are some other folks with the same need as you have.
Mar 18, 2014 at 1:11 PM
I'm going to do some more testing today as I know there are some issues still. The biggest one is if my entity interfaces inherit other interfaces - the properties are not mapped from those. For some this might not matter, but in my case it does.
Mar 18, 2014 at 6:00 PM
Ok, so below are some trimmed down classes to show how I used interfaces in a framework assembly that didn't have to reference b*. In my project only my datacontext and model assemblies required b* references. My use of IOC allows all other upper level assemblies to only require interface use and thus no direct references to my datacontext or model assemblies, and thus no b* assemblies.

First, I implemented my own Mapping provider. It's the same as the ReflectionMappingProvider with one small change in the following function. Just create your own class and use it in your datacontext class instead. I just defined my own IEntity interface that all other Interfaces must inherit that I want as part of my b* entity classes.
   private static void AddMappingsForType(EntityMappingStore mappingStore, AssemblyMappingInfo assemblyMappingInfo, Type mappedType)
        {
            if (!typeof (IEntity).IsAssignableFrom(mappedType))
                return;
            ....
        }
The next thing I did was create an interface for my own entityset:
 public interface IMfrEntitySet<TEntity> : IQueryable<TEntity>
    {
        TEntity Create();

        void Add(TEntity item);
    }
It's implementation:
 class MfrEntitySet<T> : BrightstarEntitySet<T>, IMfrEntitySet<T> where T : class, IEntity
    {

        public MfrEntitySet(BrightstarEntityContext context)
            : base(context)
        {

        }

        public MfrEntitySet(IQueryProvider provider,Expression expression)
            : base(provider,expression)
        {

        }       
    }
Then, back in your datacontext class define the entity sets as such:
public IMfrEntitySet<IApprover> Approvers

private void InitializeContext()
{
    Approvers = new MfrEntitySet<IApprover>(this);
}
Lastly, I also have an interface on my datacontext:
public interface IDataContext
    {
        IMfrEntitySet<IApprover> Approvers { get; }

        void SaveChanges();
}
Now, using your choice of IOC container, you can register your datacontext against the IDataContext.


I've not used this in any production application, and has only been minimally tested as proof of concept while deciding if b* is the correct solution for my current project. You may need to expand on some interfaces and some of the implementation, but the general concept should allow you to limit your b* reference requirements in other assemblies. However, remember that you won't be able to use any of the other b* attributes on your entity interfaces. So this is pretty basic.
Marked as answer by techquila on 3/19/2014 at 12:46 AM
Mar 19, 2014 at 8:46 AM
That's really great, thank you. If you don't have any objection I would like to add some of this to the documentation.
Mar 19, 2014 at 12:19 PM
Sure.