implement N-Tier Entity Framework 4.0 with DTOs - entity-framework

I'm currently building a web based system and trying to implement N-Tier Entity Framework 4.0 with DTOs in a SOA Architecture. I am having a problem understanding how I should implement the Data Access Layer (DAL) , the Business Logic Layer (BLL) and the Presentation Layer.
Let’s suppose that I have a “useraccount” entity has the following :
Id
FirstName
LastName
AuditFields_InsertDate
AuditFields_UpdateDate
In the DAL I created a class “UserAccountsData.cs” as the following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrderSystemDAL
{
public static class UserAccountsData
{
public static int Insert(string firstName, string lastName, DateTime insertDate)
{
using (OrderSystemEntities db = new OrderSystemEntities())
{
return Insert(db, firstName, lastName, insertDate);
}
}
public static int Insert(OrderSystemEntities db, string firstName,
string lastName, DateTime insertDate)
{
return db.UserAccounts_Insert(firstName, lastName, insertDate, insertDate).ElementAt(0).Value;
}
public static void Update(int id, string firstName, string lastName,
DateTime updateDate)
{
using (OrderSystemEntities db = new OrderSystemEntities())
{
Update(db, id, firstName, lastName, updateDate);
}
}
public static void Update(OrderSystemEntities db, int id, string firstName,
string lastName, DateTime updateDate)
{
db.UserAccounts_Update(id, firstName, lastName, updateDate);
}
public static void Delete(int id)
{
using (OrderSystemEntities db = new OrderSystemEntities())
{
Delete(db, id);
}
}
public static void Delete(OrderSystemEntities db, int id)
{
db.UserAccounts_Delete(id);
}
public static UserAccount SelectById(int id)
{
using (OrderSystemEntities db = new OrderSystemEntities())
{
return SelectById(db, id);
}
}
public static UserAccount SelectById(OrderSystemEntities db, int id)
{
return db.UserAccounts_SelectById(id).ElementAtOrDefault(0);
}
public static List<UserAccount> SelectAll()
{
using (OrderSystemEntities db = new OrderSystemEntities())
{
return SelectAll(db);
}
}
public static List<UserAccount> SelectAll(OrderSystemEntities db)
{
return db.UserAccounts_SelectAll().ToList();
}
}
}
And in the BLL I created a class “UserAccountEO.cs” as the following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using OrderSystemDAL;
namespace OrderSystemBLL
{
public class UserAccountEO
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime InsertDate { get; set; }
public DateTime UpdateDate { get; set; }
public string FullName
{
get
{
return LastName + ", " + FirstName;
}
}
public bool Save(ref ArrayList validationErrors)
{
ValidateSave(ref validationErrors);
if (validationErrors.Count == 0)
{
if (Id == 0)
{
Id = UserAccountsData.Insert(FirstName, LastName, DateTime.Now);
}
else
{
UserAccountsData.Update(Id, FirstName, LastName, DateTime.Now);
}
return true;
}
else
{
return false;
}
}
private void ValidateSave(ref ArrayList validationErrors)
{
if (FirstName.Trim() == "")
{
validationErrors.Add("The First Name is required.");
}
if (LastName.Trim() == "")
{
validationErrors.Add("The Last Name is required.");
}
}
public void Delete(ref ArrayList validationErrors)
{
ValidateDelete(ref validationErrors);
if (validationErrors.Count == 0)
{
UserAccountsData.Delete(Id);
}
}
private void ValidateDelete(ref ArrayList validationErrors)
{
//Check for referential integrity.
}
public bool Select(int id)
{
UserAccount userAccount = UserAccountsData.SelectById(id);
if (userAccount != null)
{
MapData(userAccount);
return true;
}
else
{
return false;
}
}
internal void MapData(UserAccount userAccount)
{
Id = userAccount.Id;
FirstName = userAccount.FristName;
LastName = userAccount.LastName;
InsertDate = userAccount.AuditFields_InsertDate;
UpdateDate = userAccount.AuditFields_UpdateDate;
}
public static List<UserAccountEO> SelectAll()
{
List<UserAccountEO> userAccounts = new List<UserAccountEO>();
List<UserAccount> userAccountDTOs = UserAccountsData.SelectAll();
foreach (UserAccount userAccountDTO in userAccountDTOs)
{
UserAccountEO userAccountEO = new UserAccountEO();
userAccountEO.MapData(userAccountDTO);
userAccounts.Add(userAccountEO);
}
return userAccounts;
}
}
}
And in the PL I created a webpage as the following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OrderSystemBLL;
using System.Collections;
namespace OrderSystemUI
{
public partial class Users : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadUserDropDownList();
}
}
private void LoadUserDropDownList()
{
ddlUsers.DataSource = UserAccountEO.SelectAll();
ddlUsers.DataTextField = "FullName";
ddlUsers.DataValueField = "Id";
ddlUsers.DataBind();
}
}
}
Is the above way the right way to Implement the DTOs pattern in n-tier Architecture using EF4 ???
I would appreciate your help
Thanks.

DTO's should only have properties, not methods like UserAccountEO. You should separate the DTO code from the logic that maps the entities to DTOs.
Everything else seems correct.
BTW: EntitiesToDTOs is a tool that generates DTOs from your Entity Framework EDMX file, it can help you a lot to save development time and effort.

Related

context.ObjectStateManager, assembly reference is missing in Entity Framework

I cant figure out what assembly refrence am i missing? To evade this error for my upsert logic in entity framework? Might be an old question but unable to find a solution for my project.
- Am following a Code First Approach in EF6.2.0
Please refer the picture i have attached for your reference.
/*Code Attached for reference as well */
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Net;
using System.Net.Http;
using System.Configuration;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
namespace ServerlessCoding
{
public static class EF6AddModifyLogic
{
[FunctionName("EF6AddModifyLogic")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Admin, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
string name = data?.name;
var connectionString = ConfigurationManager.AppSettings["SqlConnection"];
// var connectionString = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString; // Azure Serverless Code
using (var context = new PersonDbContext(connectionString))
{
// If you can't decide existance of the object by its Id you must exectue lookup query:
var person = new Person { Id = 1, Name = "Foo", Age = 32 };
var idVar = person.Id;
if (await context.Persons.AnyAsync(e => e.Id == idVar))
{
context.Persons.Attach(person); // you can now attach your person object to this context
context.ObjectStateManager.ChangeObjectState(person, System.Data.EntityState.Modified);
}
else
{
context.Persons.Add(person);
}
context.SaveChanges();
}
return name == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}
// POCO Class - This should match the SQL table definition.
public class Person
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
// Context Class for creating the tables.
public partial class PersonDbContext : DbContext
{
public PersonDbContext(string cs) : base(cs) { }
public DbSet<Person> Persons { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
}

entity framework core orderby guid failing to order properly

I'm trying to order by a Guid in EF Core with a relational database and its not ordering. Is there something I'm doing wrong or could this be an issue with EF Core?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;
namespace TestName
{
public class BoxDbContext : DbContext
{
public BoxDbContext(
DbContextOptions<BoxDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Box>().HasKey(x => x.Id);
modelBuilder.Entity<Box>().Property(t => t.Id).ValueGeneratedOnAdd();
base.OnModelCreating(modelBuilder);
}
}
public class Box
{
public Guid Id { get; set; }
public Guid SubId { get; set; }
}
[TestFixture]
public class TestClass
{
private SqliteConnection SqliteConnection { get; set; }
private DbContextOptions<BoxDbContext> Options => new DbContextOptionsBuilder<BoxDbContext>()
.UseSqlite(SqliteConnection)
.EnableSensitiveDataLogging()
.Options;
private DbContext GetDbContext()
{
BoxDbContext dbContext = new BoxDbContext(Options);
dbContext.Database.EnsureCreated();
return dbContext;
}
[SetUp]
public void DbSetup()
{
SqliteConnectionStringBuilder sqliteConnectionStringBuilder = new SqliteConnectionStringBuilder
{
Mode = SqliteOpenMode.Memory,
Cache = SqliteCacheMode.Private
};
SqliteConnection = new SqliteConnection(sqliteConnectionStringBuilder.ToString());
SqliteConnection.Open();
}
[TearDown]
public void DbTearDown()
{
SqliteConnection.Close();
}
[Test]
public async Task OrderByGuid()
{
List<Guid> subIds = new List<Guid>
{
Guid.Parse("901CAB07-315F-4594-A5C6-C37725643DB8"),
Guid.Parse("FA1760E7-27F4-4F8B-9205-44ACF2358044"),
Guid.Parse("0C434803-0004-4894-8E29-597AA8BCF8E2"),
Guid.Parse("C7E76CF2-35D1-4CF8-8A67-83F41842F052"),
Guid.Parse("1D6F9038-B5B3-4559-9480-3A2651E52623"),
};
using (DbContext dbContext = GetDbContext())
{
foreach (Guid subId in subIds)
{
dbContext.Set<Box>().Add(new Box {SubId = subId});
}
await dbContext.SaveChangesAsync();
}
IList<Box> boxs;
using (DbContext approvalsDbContext = GetDbContext())
{
boxs = await approvalsDbContext
.Set<Box>()
.OrderByDescending(x => x.SubId)
.ToListAsync();
}
Assert.That(boxs.Count, Is.EqualTo(subIds.Count));
Assert.That(boxs.ToArray()[0].SubId, Is.EqualTo(subIds[1]));
Assert.That(boxs.ToArray()[1].SubId, Is.EqualTo(subIds[3]));
Assert.That(boxs.ToArray()[2].SubId, Is.EqualTo(subIds[0]));
Assert.That(boxs.ToArray()[3].SubId, Is.EqualTo(subIds[4]));
Assert.That(boxs.ToArray()[4].SubId, Is.EqualTo(subIds[2]));
}
}
}
Thanks,
Chris
So I raised this with the EF Core team who said this was intended behaviour for SQLite as it doesn’t have a representation for Guid. https://github.com/aspnet/EntityFrameworkCore/issues/10198#issuecomment-340930189

linq to entity on dbcontext.set<>

I have multiple databases, 1 common and n company db. I use code first with one migration for common and one for the companies db.
I have a base context this is inherit to 2 contexts (common, company). I try to use only the base context and remove the specified contexts, so far no problem.
My problem is following, if i try to use linq on context.Set<> then I get an InvalidOperationException "The entity type NOCompany is not part of the model for the current context".
using (NOContext db = new NOContext(connection)) {
var dbset = db.Set<NOCompany>()
.Where(company => (company.Deleted == null) || (company.Deleted == false));
foreach (var item in dbset) {
System.Diagnostics.Debug.WriteLine(item.Matchcode);
}
}
if I use this
using (NOContext db = new NOCommonContext(connection)) {
var dbset = db.Set<NOCompany>()
.Where(company => (company.Deleted == null) || (company.Deleted == false));
foreach (var item in dbset) {
System.Diagnostics.Debug.WriteLine(item.Matchcode);
}
}
then it works fine. Where is the problem?
Following excerpt from the classes
public class NOContext : DbContext, INOContext
{
public NOContext() { }
public NOContext(string connection) : base(connection) { }
#region - override DbContext -
public override int SaveChanges()
{
foreach (var entry in ChangeTracker.Entries<EntityBase>()) {
DateTime currentDateTime = DateTime.Now;
var entity = entry.Entity;
if (entry.State == EntityState.Added) {
entity.CreateDate = currentDateTime;
entity.CreateId = NOEngine.SessionInfo.CurrentUserId;
} else if (entry.State == EntityState.Deleted) {
entry.State = EntityState.Modified;
entity.Deleted = true;
entity.DeletedDate = currentDateTime;
entity.DeletedId = NOEngine.SessionInfo.CurrentUserId;
}
entity.ModifiedDate = currentDateTime;
entity.ModifiedId = NOEngine.SessionInfo.CurrentUserId;
}
return base.SaveChanges();
}
#endregion
//database methods
}
Then I have 2 specified context
public class NOCommonContext : NOContext
{
public const string CommonCatalog = "NOCommonDb";
public NOCommonContext() { }
public NOCommonContext(string connection) : base(connection) { }
#region - DbSets -
public virtual DbSet<NOUser> Users { get; set; }
public virtual DbSet<NOCompany> Companies { get; set; }
public virtual DbSet<NOConfig> AppConfiguration { get; set; }
#endregion //DbSets
}
and
public partial class NOAppContext : NOContext
{
public NOAppContext() { }
public NOAppContext(string connection) : base(connection) { }
#region - DbSets -
public virtual DbSet<BPCard> BPCards { get; set; }
public virtual DbSet<BPContact> BPContacts { get; set; }
public virtual DbSet<HRCard> HRCards { get; set; }
#endregion //DbSets
}

Best Practices to localize entities with EF Code first

I am developing a domain model using EF Code First to persist the data. I have to add support for multilanguage and I would like not to contaminate the domain model with location concepts.
I like that in database exists a ProductTranslate table with title and Language fields but in my domain title belongs to the Product entity.
Someone knows how to get this?
Here is what I use and works well with code first.
Define a base Translation class:
using System;
public abstract class Translation<T> where T : Translation<T>, new()
{
public Guid Id { get; set; }
public string CultureName { get; set; }
protected Translation()
{
Id = Guid.NewGuid();
}
}
Define a TranslationCollection class:
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
public class TranslationCollection<T> : Collection<T> where T : Translation<T>, new()
{
public T this[CultureInfo culture]
{
get
{
var translation = this.FirstOrDefault(x => x.CultureName == culture.Name);
if (translation == null)
{
translation = new T();
translation.CultureName = culture.Name;
Add(translation);
}
return translation;
}
set
{
var translation = this.FirstOrDefault(x => x.CultureName == culture.Name);
if (translation != null)
{
Remove(translation);
}
value.CultureName = culture.Name;
Add(value);
}
}
public T this[string culture]
{
get
{
var translation = this.FirstOrDefault(x => x.CultureName == culture);
if (translation == null)
{
translation = new T();
translation.CultureName = culture;
Add(translation);
}
return translation;
}
set
{
var translation = this.FirstOrDefault(x => x.CultureName == culture);
if (translation != null)
{
Remove(translation);
}
value.CultureName = culture;
Add(value);
}
}
public bool HasCulture(string culture)
{
return this.Any(x => x.CultureName == culture);
}
public bool HasCulture(CultureInfo culture)
{
return this.Any(x => x.CultureName == culture.Name);
}
}
You can then use those classes in your entities, e.g.:
using System;
using System.Globalization;
public class HelpTopic
{
public Guid Id { get; set; }
public string Name { get; set; }
public TranslationCollection<HelpTopicTranslation> Translations { get; set; }
public string Content
{
get { return Translations[CultureInfo.CurrentCulture].Content; }
set { Translations[CultureInfo.CurrentCulture].Content = value; }
}
public HelpTopic()
{
Id = Guid.NewGuid();
Translations = new TranslationCollection<HelpTopicTranslation>();
}
}
With HelpTopicTranslation defined as:
using System;
public class HelpTopicTranslation : Translation<HelpTopicTranslation>
{
public Guid Id { get; set; }
public Guid HelpTopicId { get; set; }
public string Content { get; set; }
public HelpTopicTranslation()
{
Id = Guid.NewGuid();
}
}
Now, for the code first specific side of things, use the following configuration:
using System.Data.Entity.ModelConfiguration;
using Model;
internal class HelpTopicConfiguration : EntityTypeConfiguration<HelpTopic>
{
public HelpTopicConfiguration()
{
Ignore(x => x.Content); // Ignore HelpTopic.Content since it's a 'computed' field.
HasMany(x => x.Translations).WithRequired().HasForeignKey(x => x.HelpTopicId);
}
}
And add it to your context configurations:
public class TestContext : DbContext
{
public DbSet<HelpTopic> HelpTopics { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new HelpTopicConfiguration());
}
}
When all of this is done, the following migration is generated:
using System.Data.Entity.Migrations;
public partial class AddHelpTopicTable : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.HelpTopics",
c => new
{
Id = c.Guid(false),
Name = c.String(),
})
.PrimaryKey(t => t.Id);
CreateTable(
"dbo.HelpTopicTranslations",
c => new
{
Id = c.Guid(false),
HelpTopicId = c.Guid(false),
Content = c.String(),
CultureName = c.String(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.HelpTopics", t => t.HelpTopicId, true)
.Index(t => t.HelpTopicId);
}
public override void Down()
{
DropForeignKey("dbo.HelpTopicTranslations", "HelpTopicId", "dbo.HelpTopics");
DropIndex("dbo.HelpTopicTranslations", new[] { "HelpTopicId" });
DropTable("dbo.HelpTopicTranslations");
DropTable("dbo.HelpTopics");
}
}
Any comments and/or improvements are welcome...

Entity framework Generic query in Nongeneric Property

In Entity framework I have objectsets like
public partial class Building
{
public int BuildingID { get; set; }
public string BuildingName { get; set; }
}
public partial class Town
{
public int TownID { get; set; }
public string TownName { get; set; }
}
I want to create a generic query like
T.OrderBy(o=>o.Id).Skip(maxDispItem * (page - 1)).Take(maxDispItem).ToList();
T is generic class can be Building or Town but problem is BuildingId and TownId has different name.I don't want to change their name as Id and create interface IIdentity.
Maybe you could try something like this:
var query = (typeof(T) == typeof(Building) ?
context.Buildings.Select(b => new { Id = b.BuildingId, Name = b.BuildingName }) :
context.Towns.Select(t => new { Id = t.TownId, Name = b.TownName }))
.OrderBy(o => o.Id)...
Not tested but that's worth a test...
You can create generic method which find a field decorated with KeyAttribute, and then performs sorting by found key field. I have tested your model, works perfectly. Look at code snippet.
DbContext:
using System.Collections.Generic;
using System.Data.Entity;
namespace ConsoleApplication28.Entities
{
public class AppDbContext : DbContext
{
public AppDbContext()
{
Database.Connection.ConnectionString = #"Data Source=NOTEBOOK-PC;Initial Catalog=StackOverflowTest;Integrated Security=True";
Database.SetInitializer(new AppDbInitializer());
}
public DbSet<Town> Towns { get; set; }
public DbSet<Building> Buildings { get; set; }
}
public class AppDbInitializer : DropCreateDatabaseIfModelChanges<AppDbContext>
{
protected override void Seed(AppDbContext context)
{
context.Buildings.AddRange(new List<Building>
{
new Building {BuildingName = "Building1"},
new Building {BuildingName = "Building2"},
});
context.Towns.AddRange(new List<Town>
{
new Town {TownName = "Town1"},
new Town {TownName = "Town2"},
});
context.SaveChanges();
base.Seed(context);
}
}
}
Building
using System.ComponentModel.DataAnnotations;
namespace ConsoleApplication28.Entities
{
public class Building
{
[Key]
public int BuildingID { get; set; }
public string BuildingName { get; set; }
}
}
Town
using System.ComponentModel.DataAnnotations;
namespace ConsoleApplication28.Entities
{
public class Town
{
[Key]
public int TownID { get; set; }
public string TownName { get; set; }
}
}
Program
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using ConsoleApplication28.Entities;
using System.ComponentModel.DataAnnotations;
namespace ConsoleApplication28
{
class Program
{
static void Main(string[] args)
{
const int maxDispItem = 10;
const int page = 1;
var db = new AppDbContext();
var towns = db.Towns.OrderByKey().Skip(maxDispItem * (page - 1)).Take(maxDispItem).ToList();
var buildings = db.Buildings.OrderByKey().Skip(maxDispItem * (page - 1)).Take(maxDispItem).ToList();
}
}
public static class Extensions
{
/// <summary>
/// Sorts the elements of a sequence in ascending order according to a key specified using KeyAttribute
/// </summary>
public static IOrderedQueryable<T> OrderByKey<T>(this IQueryable<T> source, bool isAsc = true)
{
var type = typeof(T);
var keyProperty = type.GetProperties().Single(x => x.GetCustomAttributes(typeof(KeyAttribute)).Any());
return source.OrderBy(keyProperty.Name, isAsc);
}
#region COPIED FROM THERE http://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property, bool isAsc)
{
return isAsc ? source.OrderBy(property) : source.OrderByDescending(property);
}
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderBy");
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderByDescending");
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenBy");
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenByDescending");
}
static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
{
string[] props = property.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach (string prop in props)
{
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
object result = typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] { source, lambda });
return (IOrderedQueryable<T>)result;
}
#endregion
}
}