- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 ValueInjector(NuGet 的最新版本)将来自 EntityFramework Code First 对象的数据注入(inject)我的 View 模型。我也想做相反的事情,但这是我开始的尝试。
我研究了各种映射并尝试了很多。我还没有找到一个可以满足我需要的东西,我认为我的需求是一个相当基本的问题(SO 上有几个这样的问题,每个问题似乎都有不同的答案,这很好。)。
我真的尝试过对此进行尽职调查。我用谷歌搜索和浏览了 CodePlex 并下载了 ProDinner 应用程序(启动并运行它并逐步调试代码)。
我现在有自己的 ValueInjection 类来处理注入(inject)。我将以一种非常友好的方式粘贴我的代码和对象,以便您可以获取并运行它们。请注意,我有一个使用基本 LoopValueInjection 的解决方案,但我不喜欢它,因为它需要手动管道进行从实体到映射然后映射到实体的注入(inject)。 ProDinner 示例采用了一种更加模板化的方法,我喜欢这种方法,但我无法根据自己的需要对其进行调整。
我认为我的代码在逻辑上困惑的地方在于,如果源属性类型不是简单对象,我不明白如何强制进行递归注入(inject)。在此示例中,Person.Address.* 属性将按 PersonViewModel 类中的名称和类型进行匹配;然而,注入(inject)循环遍历 Person 的属性并尝试匹配 Person.Address 属性的名称和类型。
我以为行
Object result = Activator.CreateInstance(c.SourceProp.Type)
.InjectFrom<CloneInjection>(c.SourceProp.Value);
会执行此递归,但我认为不会。
那么...谁能告诉我如何解决这个问题?
////// ENTITY MODELS
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual Address Address { get; set; }
}
public class Address
{
public int Id { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
////// VIEW MODEL
public class PersonViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int PersonId { get; set; }
public int AddressId { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
////// CUSTOM INJECTOR
public class EntityToViewModel : ConventionInjection
{
protected override bool Match(ConventionInfo c)
{
//ignore int = 0 and DateTime = to 1/01/0001
if (c.SourceProp.Type == typeof(DateTime) && (DateTime)c.SourceProp.Value == default(DateTime) ||
(c.SourceProp.Type == typeof(int) && (int)c.SourceProp.Value == default(int)))
return false;
if (c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.Value != null)
return true;
if (c.SourceProp.Type == typeof(int) &&
c.TargetProp.Type == typeof(int) )
{
if( "id".Equals(c.SourceProp.Name.ToLower()) &&
c.TargetProp.Name.ToLower().EndsWith("id") &&
c.TargetProp.Name.StartsWith(c.Source.Type.Name))
return true;
}
//Transition logic to SetValue for value types. This should
//allow Address values on Person to be set.
if (!c.SourceProp.Type.IsPrimitive || c.SourceProp.Type.Equals(typeof(string)))
return true;
return false;
//put id logic matching here
//return c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.Value != null;
}
protected override object SetValue(ConventionInfo c)
{
//If type is primative or string return the value as is
if (c.SourceProp.Type.IsPrimitive || c.SourceProp.Type.Equals(typeof(string)))
return c.SourceProp.Value;
Object result = Activator.CreateInstance(c.SourceProp.Type)
.InjectFrom<CloneInjection>(c.SourceProp.Value);
//for simple object types create a new instace and apply the clone injection on it
return result;
}
}
////// Program.cs
public class Program
{
public static void Main(string[] args)
{
Person defaultPerson = getDefaultPerson();
//Throws an error. I'm not sure where in the pipeline this occurs, but
//it seems to happen somewhere other than the Match & SetValue method of
//my EntityToViewModel injection
PersonViewModel pvm = CreateFromEntity(defaultPerson);
//Works, but want it more generic & without having to
//include hardcoded prefix for every non-simple object on my EF Model
pvm = CreateFromEntityWorking(defaultPerson);
Console.ReadLine();
}
private static PersonViewModel CreateFromEntity(Person person)
{
PersonViewModel pvm = new PersonViewModel();
pvm.InjectFrom<EntityToViewModel>(person);
return pvm;
}
///WORKING MAPPING BUT SEEMS TOO HARDCODED
private static PersonViewModel CreateFromEntityWorking(Person person)
{
PersonViewModel personvm = new PersonViewModel();
//Fill out view model properties with the same name as those on Person
personvm.InjectFrom(new LoopValueInjection().TargetPrefix("Person"), person);
if (person != null && person.Address != null)
{
//Fill out view model properties for the Address
personvm.InjectFrom(new LoopValueInjection().TargetPrefix("Address"), person.Address);
}
return personvm;
}
public static Person getDefaultPerson()
{
Person p = new Person();
Address a = new Address();
p.Id = 1;
p.FirstName = "John";
p.LastName = "McClain";
a.City = "New York";
a.State = "New York";
a.Zip = "55555";
a.Id = 2;
p.Address = a;
return p;
}
}
最佳答案
终于找到了我需要的例子。可悲的是,尽管我一直在寻找我才刚刚登陆这些。这不是我自己的错;但是,我的脚因为一直在踢它而感到酸痛。
相关网址:
文档 (RTFM) http://valueinjecter.codeplex.com/documentation
扁平化示例和约定 http://valueinjecter.codeplex.com/wikipage?title=flattening&referringTitle=Documentation
Unflattening 示例和约定 http://valueinjecter.codeplex.com/wikipage?title=unflattening&referringTitle=Documentation
我的问题的解决方案:
必须在 View 模型上使用的约定是顶级属性是没有以它们的属性名称为前缀,因为它存在于实体中。因此,在我下面的类中,FirstName、LastName 和 Id 都作为我的 Person 实体的顶级属性存在,它们在我的 View 模型类中没有前缀。 Person.Address 属性的城市、州、邮政编码和 ID 确实有前缀。
奇怪的是,我在尝试实现自己的注入(inject)时想到了这个解决方案。我得到了一个从 Entity 到 ViewModel 的工作......只是不是相反。
public class PersonViewModelPrefixed
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Id { get; set; }
public int AddressId { get; set; }
public string AddressCity { get; set; }
public string AddressState { get; set; }
public string AddressZip { get; set; }
}
public static void Main(string[] args)
{
Person defaultPerson = getDefaultPerson();
PersonViewModelPrefixed defaultPrefixedVm = getDefaultPrefixedViewModel();
//flatten - Entity to View Model
PersonViewModelPrefixed pvm = Flatten(defaultPerson);
//unflatten - View Model to Entity
Person person2 = Unflatten(defaultPrefixedVm);
Console.ReadLine();
}
//unflatten - View Model to Entity
private static Person Unflatten(PersonViewModelPrefixed personViewModel)
{
Person p = new Person();
p.InjectFrom<UnflatLoopValueInjection>(personViewModel);
return p;
}
//flatten - Entity to View Model
private static PersonViewModelPrefixed Flatten(Person person)
{
PersonViewModelPrefixed pvm = new PersonViewModelPrefixed();
pvm.InjectFrom<FlatLoopValueInjection>(person);
return pvm;
}
那么,鉴于这些细节,是否有人对如何使 Flatten 和 Unflatten 方法更通用有一些建议?这就是我接下来要做的。
关于c# - 如何创建自定义值注入(inject)以将我的实体映射到我的 View 模型?包括尝试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12824836/
我正在尝试测试依赖于其他服务 authService 的服务 documentViewer angular .module('someModule') .service('docu
如果我的网站上线(不要认为它会,目前它只是一个学习练习)。 我一直在使用 mysql_real_escape_string();来自 POST、SERVER 和 GET 的数据。另外,我一直在使用 i
我有以下代码,它容易受到 SQL 注入(inject)的攻击(我认为?): $IDquery = mysqli_query($connection, "SELECT `ID` FROM users W
我一直在自学如何创建扩展,以期将它们用于 CSS 注入(inject)(以及最终以 CSS 为载体的 SVG 注入(inject),但那是以后的问题)。 这是我当前的代码: list .json {
这个简单的代码应该通过 Java Spring 实现一个简单的工厂。然而结果是空指针,因为 Human 对象没有被注入(inject)对象(所以它保持空)。 我做错了什么? 谢谢 配置 @Config
我正在编写一个 ASP.NET MVC4 应用程序,它最终会动态构建一个 SQL SELECT 语句,以便稍后存储和执行。动态 SQL 的结构由用户配置以用户友好的方式确定,具有标准复选框、下拉列表和
首先让我说我是我为确保 SQL 注入(inject)攻击失败而采取的措施的知己。所有 SQL 查询值都是通过事件记录准备语句完成的,所有运算符(如果不是硬编码)都是通过数字白名单系统完成的。这意味着如
这是 SQL 映射声称可注入(inject)的负载: user=-5305' UNION ALL SELECT NULL,CONCAT(0x716b6b7071,0x4f5577454f76734
我正在使用 Kotlin 和 Android 架构组件(ViewModel、LiveData)构建一个新的 Android 应用程序的架构,并且我还使用 Koin 作为我的依赖注入(inject)提供
假设 RequestScope 处于 Activity 状态(使用 cdi-unit 的 @InRequestScope) 给定 package at.joma.stackoverflow.cdi;
我有一个搜索表单,可以在不同的提供商中搜索。 我从拥有一个基本 Controller 开始 public SearchController : Controller { protected r
SQLite 注入 如果您的站点允许用户通过网页输入,并将输入内容插入到 SQLite 数据库中,这个时候您就面临着一个被称为 SQL 注入的安全问题。本章节将向您讲解如何防止这种情况的发生,确保脚
我可以从什么 dll 中获得 Intercept 的扩展?我从 http://github.com/danielmarbach/ninject.extensions.interception 添加了
使用 NInject 解析具有多个构造函数的类似乎不起作用。 public class Class1 : IClass { public Class1(int param) {...} public
我有一个 MetaManager 类: @Injectable() export class MetaManager{ constructor(private handlers:Handler
我是 Angular 的新手,我不太清楚依赖注入(inject)是如何工作的。我的问题是我有依赖于服务 B 的服务 A,但是当我将服务 A 注入(inject)我的测试服务 B 时,服务 B 变得未定
我正在为我的项目使用 android 应用程序启动、刀柄和空间。我在尝试排队工作时遇到错误: com.test E/WM-WorkerFactory: Could not instantiate co
我不确定这是什么糖语法,但让我向您展示问题所在。 def factors num (1..num).select {|n| num % n == 0} end def mutual_factors
简单的问题,我已经看过这个了:Managing imports in Scalaz7 ,但我不知道如何最小化注入(inject) right和 left方法到我的对象中以构造 \/ 的实例. 我确实尝
在我的 Aurelia SPA 中,我有一些我想在不同模块中使用的功能。它依赖于调用时给出的参数和单例的参数。有没有办法创建一个导出函数,我可以将我的 Auth 单例注入(inject)其中,而不必在
我是一名优秀的程序员,十分优秀!