Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#对UnitOfWork/通用存储库的思考_C#_Asp.net_.net_Reflection - Fatal编程技术网

C#对UnitOfWork/通用存储库的思考

C#对UnitOfWork/通用存储库的思考,c#,asp.net,.net,reflection,C#,Asp.net,.net,Reflection,这似乎是一个由来已久的问题,但根本找不到我要找的东西。这是我目前的代码和我尝试过的东西 private async Task<T> HandleFileCreate<T>(Guid tableId, IFormFile file, DocumentType documentType) where T : DocumentLibrary { // This works fine and gets the cor

这似乎是一个由来已久的问题,但根本找不到我要找的东西。这是我目前的代码和我尝试过的东西

private async Task<T> HandleFileCreate<T>(Guid tableId, IFormFile file, DocumentType documentType)
            where T : DocumentLibrary
        {
            // This works fine and gets the correct type
            Type repoType = _unitOfWork.GetType().GetProperty(typeof(T).Name + "Repository").PropertyType;

            // This works fine and creates an instance of my document
            T document = (T)Activator.CreateInstance(typeof(T));

            // Throws up error: "No parameterless constructor defined for this object."
            object instance = Activator.CreateInstance(repoType);

            // Throws up error: "Object does not match target type."
            repoType.GetMethod("Create").Invoke(repoType, new object[] { document });
        }
private async Task HandleFileCreate(Guid tableId、ifformfile文件、DocumentType DocumentType)
where T:DocumentLibrary
{
//这可以很好地工作并得到正确的类型
类型repoType=\u unitOfWork.GetType().GetProperty(typeof(T).Name+“Repository”).PropertyType;
//这可以很好地工作并创建我的文档的一个实例
T document=(T)Activator.CreateInstance(typeof(T));
//抛出错误:“没有为此对象定义无参数构造函数。”
对象实例=Activator.CreateInstance(repoType);
//抛出错误:“对象与目标类型不匹配。”
调用(repoType,新对象[]{document});
}
这看起来有点像鸡和蛋,因为如果没有CreateInstance,我就无法为“创建”开具发票,因为IoC的原因,在CreateInstance中我无法这样做


这真的很烦人,因为我在unitOfWork中已经有一个实例化的属性,它直接与我的相关GenericRepository相关,我就是不知道如何访问它?我甚至不需要重新实例化它。

Rader然后使用反射(这总是个坏主意),我建议这样做

public static T Create<T>(IDocumentLibraryCreator<T> repo)// You can add you params here 
            where T:  DocumentLibrary, new()
        {
            var newItem = new T();

            repo.Create(newItem);
//Do your stuff here
            return newItem;
        }


        public interface IDocumentLibraryCreator<T> where T : DocumentLibrary
        {
            Task Create(T document);
        } 

        public abstract class DocumentLibrary
        {

        }
publicstatict-Create(idocumentlibrarycreatorrepo)//您可以在这里添加参数
其中T:DocumentLibrary,new()
{
var newItem=newt();
回购创建(新项目);
//在这里做你的事
返回新项目;
}
公共接口IDocumentLibraryCreator,其中T:DocumentLibrary
{
任务创建(T文档);
} 
公共抽象类文档库
{
}
因为通过反射得到的只是运行时异常,而不是编译时异常。不需要反省。您的回购需要实现
IDocumentLibraryCreator
接口,但如果您以良好的方式实现了IRepository,您应该已经有了类似的功能。
如果您需要在单个工作单元中完成工作,则需要传递在uof类中创建的实例。

Rader然后使用反射(总是一个坏主意),我建议您这样做

public static T Create<T>(IDocumentLibraryCreator<T> repo)// You can add you params here 
            where T:  DocumentLibrary, new()
        {
            var newItem = new T();

            repo.Create(newItem);
//Do your stuff here
            return newItem;
        }


        public interface IDocumentLibraryCreator<T> where T : DocumentLibrary
        {
            Task Create(T document);
        } 

        public abstract class DocumentLibrary
        {

        }
publicstatict-Create(idocumentlibrarycreatorrepo)//您可以在这里添加参数
其中T:DocumentLibrary,new()
{
var newItem=newt();
回购创建(新项目);
//在这里做你的事
返回新项目;
}
公共接口IDocumentLibraryCreator,其中T:DocumentLibrary
{
任务创建(T文档);
} 
公共抽象类文档库
{
}
因为通过反射得到的只是运行时异常,而不是编译时异常。不需要反省。您的回购需要实现
IDocumentLibraryCreator
接口,但如果您以良好的方式实现了IRepository,您应该已经有了类似的功能。
如果需要在单个工作单元中完成工作,则需要传递在uof类中创建的实例。

您的存储库是通用的吗?你的IoC容器是什么?如果您试图进行DI,为什么不将IRepository作为输入参数传递呢?如果您真的需要,我不会尝试这样做。请使用您的服务定位器不要使用Activator.CreateInstanceYep,我的存储库是通用的,接受ApplicationDbContext。。IoC由MVC核心处理。发送整个T存储库并不理想,因为此函数是在控制器级别调用的。将查看服务定位器!发送整个T存储库并不理想是什么意思?不管怎样,你只是在传递一个推荐人。。。如果您有一个
存储库
,您应该将它通过
服务传递到at
ConfigureServices
。AddSingleton
如果两个人在这里说相同的话,那么至少尝试一下是有意义的。我会打一针,然后回来报告。谢谢大家。@Andre Andersen如果他使用的是“unitOfWork”,那么这个实例就不应该是单例实例,而且这个实例可能与控制器中的HttpContext.RequestServices中的实例也不一样。您的存储库是通用的吗?你的IoC容器是什么?如果您试图进行DI,为什么不将IRepository作为输入参数传递呢?如果您真的需要,我不会尝试这样做。请使用您的服务定位器不要使用Activator.CreateInstanceYep,我的存储库是通用的,接受ApplicationDbContext。。IoC由MVC核心处理。发送整个T存储库并不理想,因为此函数是在控制器级别调用的。将查看服务定位器!发送整个T存储库并不理想是什么意思?不管怎样,你只是在传递一个推荐人。。。如果您有一个
存储库
,您应该将它通过
服务传递到at
ConfigureServices
。AddSingleton
如果两个人在这里说相同的话,那么至少尝试一下是有意义的。我会打一针,然后回来报告。谢谢大家。@Andre Andersen如果他使用的是“unitOfWork”,那么这个实例就不应该是一个单实例,而且这个实例可能与来自控制器的HttpContext.RequestServices中的实例也不一样。最后,我接受了你们的建议,将通用的Repo发送给了这个函数。工作很愉快。谢谢最后,我采纳了你的建议,将通用回购协议发送给了该职能部门。工作很愉快。谢谢