Add domain classes (model.cs?) to other project than Context classes

Nov 28, 2010 at 8:47 PM

I like the approach of this implementation, but I do have a question about the use of it.

I want to separate the domain classes in their own assembly and the Context and other persistence-related classes in a second assembly. Is it possible to do it with the code generated using these templates? If so, is there a quick overview somewhere.


Nov 28, 2010 at 9:51 PM

It is possible, just take the models.cs or models.vb into another project and reference them.

Nov 29, 2010 at 1:58 AM

Thank you for the response. This works indeed. I have a suggestion for improving the template's support for this scenario.

If the classes will live in different assemblies, they may (will likely...) live in different namespaces also. I would suggest modifying as follows (which would not break existing uses):

  1. Define two new string properties on the EntityGenerator class:
    1. DomainNamespaceName: to enter the namespace for the domain classes (model.cs)
    2. DalNamespaceName to enter the namespace for the data access classes (all other files)
    3. Both initialized to string.Empty.
  2. Modify Context.cs.ttinclude, Context.Extensions.cs.ttinclude, FakeContext.cs.ttinclude and IContext.cs.ttinclude (23) to read
    <#=EfUtils.WriteBeginNamespace(code.EscapeNamespace(EntityGenerator.NamespaceName + DalNamespaceName))#>
  3. Modify Model.cs.ttinclude line 24 to read
    <#=EfUtils.WriteBeginNamespace(code.EscapeNamespace(EntityGenerator.NamespaceName + EntityGenerator.DomainNamespaceName))#>

That way, if people put their classes in one assembly/namespace, no changes are required. For people who want to put their model in a different namespace (the NamespaceName could be completely empty if required), it is also possible.

Finally, perhaps adding a way to choose not to link .cs by default could be handy also.

Thanks for the efforts on this project!


Nov 29, 2010 at 3:17 AM

Validation.cs also has to be moved, I think. In addition, the same modification as in Model.cs.ttinclude has to be made in the validation template file (use the DomainNamespaceName property).

Nov 29, 2010 at 5:46 AM

You're welcome! You are free to copy the output to other projects and change the templates to your liking in your project... :)

Jan 4, 2012 at 12:14 PM

Repo.cs.ttinclude should also have the namespace changed by adding DomainNamespaceName property in line 77.