Is there any way to only allow Entity Framework classes to write/read the EmailJson property ? The code works without this but it would be much cleaner if this property could not be altered by any code since having invalid json in it would throw an exception.
I cannot use a backing property because of the generic requirement.
public class EmailEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public string EmailJson { get; set; }
public T GetEmail<T>() where T : BaseEmailModel
{
return JsonConvert.DeserializeObject<T>(EmailJson);
}
public void GetEmail<T>(T emailModel) where T : BaseEmailModel
{
EmailJson = JsonConvert.SerializeObject(emailModel);
}
}
I suggest using a seperate data access tier (for example a class 'EmailEntityService') that is responsible for all access to EmailEntity. All code throughout your solution should only use this service then. If you move EmailEntity to a seperate project you can ensure that access to it is only possible via EmailEntityService (if you keep that service in a project holding a reference to the 'entity project').
So at the end you have (at least) three projects:
1: Entity project
2: Entity services project (with a reference to 1, this project ensures entities cannot be altered directly)
3: Business logic project (with a reference to 2 but not to 1 to prevent direct access to entities)
Since entity classes are just POCOs I don't see any other possibilities to do this on a more technical/basic way.
Related
i'm new to mvc 5, and I would like to build an asp.net application to interact with an existing database. I'm using VIsual studio 2013 and Entity Framework 6.
I've generated an ADO.net Entity Data Model from an existing database and I'm currently trying to find out the best way to make data validations, to avoid wrong inputs (let's take as example the field Email from entity Users).
The right way seems to be to use Annotations on partial classes. But i don't know how to add an annotation (on the new partial class that i created for that) if the original property declaration is on the autogenerated file.
The autogenerated class, looks like:
namespace Test.Models
{
...
public partial class Users
{
public string Email { get; set; }
}
...
}
Following the idea behind [this post] (Add data annotations to a class generated by entity framework), i'm trying to make a partial class to write the annotations there, like that:
namespace Test.Models
{
using System.ComponentModel.DataAnnotations;
[MetadataType(typeof(UsersMetaData))]
public partial class Users
{
[Someanotations]
public string Email { get; set; }
}
}
But on the partial class, i get:
1) Error on the line of "[MetadataType(typeof(UsersMetadata))]", saying that UsersMetadata could not be found, and
2) Error on the line where "public string email", saying that the property is already declared (which sounds logic for me).
How should i annotate on the new partial class the property that is declared on the autogenerated model?
It is possible to define a Regex to be used on the anotation?
Thanks for your time,
John
You're almost there. UserMetadata is actually another class that you apply the annotations to. I usually put both of these in the same file.
[MetadataType(typeof(UsersMetaData))]
public partial class Users
{
}
class UsersMetaData
{
[Someanotations]
public string Email { get; set; }
}
Is it possible to have Code First data classes declared with internal access as shown:
internal class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
I have a requirement that classes and its properties should not be visible outside of the assembly.
As long as your DbContext derived class that exposes your class to EF is in the same assembly, you should be able to. I don't happen to design my apps that way as I prefer more separation. But the context should be able to build the model and it should be able to interact with the classes (e.g. execute queries, save changes etc) if they are in the same assembly since it will have access to the internal class. Even with the various odd things we tried and wrote about in the Code First book, I never happened to try this particular scenario.
A models already exists. They are situated in another project. Where I should put DataAnotations in that project or my one? Should I use partial classes? I would like to put DataAnatation because I want javascript validation to work on client.
You can't use partial classes across assemblies, so that option is out.
You can create DTOs (data transfer objects) that are copies of the ones in the other assembly, annotate them and map.
For easy mapping you can use a tool like auto mapper. If the property names match, it will essentially do all the work for you.
Create a partial class for your Model. like this:
[MetadataType(typeof(Log_Validation))]
public partial class Log : IEntity
{
}
then create a new class Log_Validation which does all the data annotations stuff.
public class Log_Validation
{
[DisplayName("Level")]
[Required(ErrorMessage = "Please provide a level")]
public String Level { get; set; }
}
I'm using Ria service class library. This contains 2 library named RiaClasslibrary RiaClasslibrary.Web.
Riaclasslibrary.Web contains ADO.NET entity data model and named BaseModel. BaseModelcontains tPage class.
My problem is
I'm inserting separated tPage class. This class contains 2 public property. show below
public sealed partial class tPage : EntityObject
{
public List<tPage> Children { get; set; }
public tPage Parent { get; set; }
public Boolean IsSelected { get; set; }
}
After I'm inserting DomainService and building RiaClasslibrary.Web class library. But ria service generated code doesn't contains above properties.
You have a question. Why you separate tPage class. You simply insert those 3 property in Modelbase.Designer code.
My answer is: Database doesn't contain those 3 property and If I'm inserting properties in the code, properties removed after updating Entity Model.
#ebattulga
I don't know if you still have this issue, but I will post the answer because I came to similar issue.
The answer for
After I'm inserting DomainService and
building RiaClasslibrary.Web class
library. But ria service generated
code doesn't contains above
properties.
is quite easy but hard to find.
You can read here in section "Shared Code" http://www.silverlightshow.net/items/WCF-RIA-Services-Part-5-Metadata-and-Shared-Classes.aspx
If you want to see custom properties from partial classes on the Client you have to rename class file name from MyClass.cs to simply MyClass.shared.cs. This will create partial class in the code generated Client side.
HTH
Daniel SkowroĊski
I want a list of different (derived) object types working with the Default Modelbinder in Asp.net MVC 2.
I have the following ViewModel:
public class ItemFormModel
{
[Required(ErrorMessage = "Required Field")]
public string Name { get; set; }
public string Description { get; set; }
[ScaffoldColumn(true)]
//public List<Core.Object> Objects { get; set; }
public ArrayList Objects { get; set; }
}
And the list contains objects of diffent derived types, e.g.
public class TextObject : Core.Object
{
public string Text { get; set; }
}
public class BoolObject : Core.Object
{
public bool Value { get; set; }
}
It doesn't matter if I use the List or the ArrayList implementation, everything get's nicely scaffolded in the form, but the modelbinder doesn't resolve the derived object type properties for me when posting back to the ActionResult.
What could be a good solution for the Viewmodel structure to get a list of different object types handled? Having an extra list for every object type (e.g. List, List etc.) seems to be not a good solution for me, since this is a lot of overhead both in building the viewmodel and mapping it back to the domain model.
Thinking about the other approach of binding all properties in a custom model binder, how can I make use the data annotations approach here (validating required attributes etc.) without a lot of overhead?
Check out the Derived Type ModelBinder in MvcContrib. This allows you to modelbind to derived types through the process of 'typestamping' - which is handled automatically for you when using the RenderTypedPartial(...) helper. MvcContrib partials maintain binding state across partials so the Name/Id prefixes are properly maintained on a deep object graph. If you use other mechanisms like templates, then you'll need to handle the typestamping yourself. This is explained in the documentation page.
Getting back to your questions and how the derived types are resolved with the ModelBinder, you can register the derived type variations with attributes in a mechanism similar to the WCF KnownTypeAttribute or you can do the registration on startup. Either way, these variations are registered once and held onto for performance considerations.
The model binder also solves this problem in a way that does not interfere with data annotation/validation attributes. They will work as you expect them in any other scenario.